Try to make MonitoringIsEnabled more stable (dotnet/corefx#38651)
authorStephen Toub <stoub@microsoft.com>
Tue, 18 Jun 2019 22:08:20 +0000 (18:08 -0400)
committerGitHub <noreply@github.com>
Tue, 18 Jun 2019 22:08:20 +0000 (18:08 -0400)
Add a GC.Collect call to ensure the GC.GetMemoryInfo sees the object being kept alive.

Commit migrated from https://github.com/dotnet/corefx/commit/471edf259604905535f147ddfb2c8fe5a37ce81d

src/libraries/System.Runtime.Extensions/tests/System/AppDomainTests.cs

index 3268a04..9df6aca 100644 (file)
@@ -391,11 +391,13 @@ namespace System.Tests
             Assert.Throws<ArgumentException>(() => { AppDomain.MonitoringIsEnabled = false; });
             AppDomain.MonitoringIsEnabled = true;
 
-            object o = new object();
-            Assert.InRange(AppDomain.MonitoringSurvivedProcessMemorySize, 1, long.MaxValue);
-            Assert.InRange(AppDomain.CurrentDomain.MonitoringSurvivedMemorySize, 1, long.MaxValue);
-            Assert.InRange(AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize, 1, long.MaxValue);
-            GC.KeepAlive(o);
+            const int AllocationSize = 1_234_567;
+            object o = new byte[AllocationSize];
+            GC.Collect();
+
+            Assert.InRange(AppDomain.MonitoringSurvivedProcessMemorySize, AllocationSize, long.MaxValue);
+            Assert.InRange(AppDomain.CurrentDomain.MonitoringSurvivedMemorySize, AllocationSize, long.MaxValue);
+            Assert.InRange(AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize, AllocationSize, long.MaxValue);
 
             using (Process p = Process.GetCurrentProcess())
             {
@@ -403,6 +405,8 @@ namespace System.Tests
                 TimeSpan monitoringTime = AppDomain.CurrentDomain.MonitoringTotalProcessorTime;
                 Assert.InRange(monitoringTime, processTime, TimeSpan.MaxValue);
             }
+
+            GC.KeepAlive(o);
         }
 
 #pragma warning disable 618