GcAdapter.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. namespace AIDiagnosisDemo.GC
  3. {
  4. internal class GcAdapter : IGcAdapter
  5. {
  6. private static GcAdapter _instance;
  7. public bool IsManualGc { get; set; }
  8. public static GcAdapter Instance
  9. {
  10. get
  11. {
  12. if (_instance == null)
  13. {
  14. _instance = new GcAdapter();
  15. }
  16. return _instance;
  17. }
  18. }
  19. private GcAdapter()
  20. {
  21. IsManualGc = true;
  22. }
  23. public void SuppressFinalize(object obj)
  24. {
  25. if (IsManualGc)
  26. {
  27. System.GC.SuppressFinalize(obj);
  28. }
  29. }
  30. public void RegisterForFullGCNotification(int maxGenerationThreshold, int largeObjectHeapThreshold)
  31. {
  32. if (IsManualGc)
  33. {
  34. try
  35. {
  36. System.GC.RegisterForFullGCNotification(maxGenerationThreshold, largeObjectHeapThreshold);
  37. }
  38. catch (Exception e)
  39. {
  40. // ignored
  41. }
  42. }
  43. }
  44. public GCNotificationStatus WaitForFullGCApproach()
  45. {
  46. if (IsManualGc)
  47. {
  48. return System.GC.WaitForFullGCApproach();
  49. }
  50. return GCNotificationStatus.Canceled;
  51. }
  52. public GCNotificationStatus WaitForFullGCComplete()
  53. {
  54. if (IsManualGc)
  55. {
  56. return System.GC.WaitForFullGCComplete();
  57. }
  58. return GCNotificationStatus.Canceled;
  59. }
  60. public void WaitForPendingFinalizers()
  61. {
  62. if (IsManualGc)
  63. {
  64. System.GC.WaitForPendingFinalizers();
  65. }
  66. }
  67. public int CollectionCount(int generation)
  68. {
  69. if (IsManualGc)
  70. {
  71. return System.GC.CollectionCount(generation);
  72. }
  73. return 0;
  74. }
  75. public void Collect()
  76. {
  77. if (IsManualGc)
  78. {
  79. System.GC.Collect();
  80. }
  81. }
  82. public void Collect(int generation)
  83. {
  84. if (IsManualGc)
  85. {
  86. System.GC.Collect(generation);
  87. }
  88. }
  89. public void CancelFullGCNotification()
  90. {
  91. if (IsManualGc)
  92. {// TODO for net standard,this method is not implemented !!!!
  93. try
  94. {
  95. System.GC.CancelFullGCNotification();
  96. }
  97. catch (Exception e)
  98. {
  99. // ignored
  100. }
  101. }
  102. }
  103. public void EndNoGCRegion()
  104. {
  105. if (IsManualGc)
  106. {
  107. System.GC.EndNoGCRegion();
  108. }
  109. }
  110. }
  111. }