@@ -52,7 +52,7 @@ public class Engine : IEngine
5252 // These must be static since more than one Engine is used.
5353 private static long survivedBytes ;
5454 private static bool survivedBytesMeasured ;
55- private Func < long > GetTotalBytes { get ; }
55+ private static Func < long > GetTotalBytes { get ; set ; }
5656
5757 internal Engine (
5858 IHost host ,
@@ -93,9 +93,11 @@ internal Engine(
9393 actualStage = new EngineActualStage ( this ) ;
9494
9595 random = new Random ( 12345 ) ; // we are using constant seed to try to get repeatable results
96- if ( includeSurvivedMemory && ! survivedBytesMeasured )
96+
97+ if ( includeSurvivedMemory && GetTotalBytes is null )
9798 {
98- GetTotalBytes = GetTotalBytesFunc ( ) ;
99+ // CreateGetTotalBytesFunc enables monitoring, so we only call it if we need to measure survived memory.
100+ GetTotalBytes = CreateGetTotalBytesFunc ( ) ;
99101
100102 // Necessary for CORE runtimes.
101103 // Measure bytes to allow GC monitor to make its allocations.
@@ -106,9 +108,8 @@ internal Engine(
106108 }
107109 }
108110
109- private Func < long > GetTotalBytesFunc ( )
111+ private static Func < long > CreateGetTotalBytesFunc ( )
110112 {
111- // Only enable monitoring if memory diagnoser with survived memory is applied.
112113 // Don't try to measure in Mono, Monitoring is not available, and GC.GetTotalMemory is very inaccurate.
113114 if ( RuntimeInformation . IsMono )
114115 return ( ) => 0 ;
0 commit comments