From 312caa957410a4b65f38ae028719dd11f7632912 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 18 Jun 2019 18:08:20 -0400 Subject: [PATCH] Try to make MonitoringIsEnabled more stable (dotnet/corefx#38651) 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 --- .../tests/System/AppDomainTests.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Runtime.Extensions/tests/System/AppDomainTests.cs b/src/libraries/System.Runtime.Extensions/tests/System/AppDomainTests.cs index 3268a04..9df6aca 100644 --- a/src/libraries/System.Runtime.Extensions/tests/System/AppDomainTests.cs +++ b/src/libraries/System.Runtime.Extensions/tests/System/AppDomainTests.cs @@ -391,11 +391,13 @@ namespace System.Tests Assert.Throws(() => { 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 -- 2.7.4