Declare static field `finalizerCompletedOnce` volatile -- this test has a
side-effect-free busy-loop which checks that static variable for a change it
expects a finalizer to make to it; this static field must be volatile to
ensure the jit doesn't hoist the load from the loop.
Call GC.KeepAlive on the objects constructed in the various allocation
loops in this test, to make sure that the entire allocation isn't
optimized away.
Commit migrated from https://github.com/dotnet/coreclr/commit/
f6b070c69585761996778fa5228b07d0be26be57
do
{
finalizableObject = new BlockingFinalizerOnShutdown();
+ GC.KeepAlive(finalizableObject);
} while (!BlockingFinalizerOnShutdown.finalizerCompletedOnce);
// Start a bunch of threads that allocate continuously, to increase the chance that when Main returns, one of the
{
byte[] b;
while (true)
+ {
b = new byte[1024];
+ GC.KeepAlive(b);
+ }
}
private class BlockingFinalizerOnShutdown
{
- public static bool finalizerCompletedOnce = false;
+ public volatile static bool finalizerCompletedOnce = false;
public bool isLastObject = false;
~BlockingFinalizerOnShutdown()
do
{
o = new object();
+ GC.KeepAlive(o);
} while ((++i & 0xff) != 0 || (elapsed = DateTime.Now - start) < timeout);
Console.WriteLine("Finalizer end");