@@ -52,7 +52,7 @@ public class Engine : IEngine
52
52
// These must be static since more than one Engine is used.
53
53
private static long survivedBytes ;
54
54
private static bool survivedBytesMeasured ;
55
- private Func < long > GetTotalBytes { get ; }
55
+ private static Func < long > GetTotalBytes { get ; set ; }
56
56
57
57
internal Engine (
58
58
IHost host ,
@@ -93,9 +93,11 @@ internal Engine(
93
93
actualStage = new EngineActualStage ( this ) ;
94
94
95
95
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 )
97
98
{
98
- GetTotalBytes = GetTotalBytesFunc ( ) ;
99
+ // CreateGetTotalBytesFunc enables monitoring, so we only call it if we need to measure survived memory.
100
+ GetTotalBytes = CreateGetTotalBytesFunc ( ) ;
99
101
100
102
// Necessary for CORE runtimes.
101
103
// Measure bytes to allow GC monitor to make its allocations.
@@ -106,9 +108,8 @@ internal Engine(
106
108
}
107
109
}
108
110
109
- private Func < long > GetTotalBytesFunc ( )
111
+ private static Func < long > CreateGetTotalBytesFunc ( )
110
112
{
111
- // Only enable monitoring if memory diagnoser with survived memory is applied.
112
113
// Don't try to measure in Mono, Monitoring is not available, and GC.GetTotalMemory is very inaccurate.
113
114
if ( RuntimeInformation . IsMono )
114
115
return ( ) => 0 ;
0 commit comments