From 8d30abbe2b556da423cc0a020370181ba3c0aec1 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Fri, 20 Apr 2018 15:30:56 -0700 Subject: [PATCH] Fix GC tests that contain GC.Collect() used in wrong scope (#17680) * Fix GC tests that contain GC.Collect() used in wrong scope * Fix GCLarge * fix typos/build errors * More test failures... * more ci failure fix * Fix all the build errors * Fix test failure --- tests/src/GC/API/GC/Finalize.cs | 8 +- tests/src/GC/API/GC/GetTotalMemory.cs | 37 +++++---- tests/src/GC/API/GC/KeepAlive.cs | 3 + tests/src/GC/API/GC/KeepAliveNull.cs | 11 ++- tests/src/GC/API/GC/KeepAliveRecur.cs | 1 + tests/src/GC/API/GC/SuppressFinalize.cs | 16 ++-- tests/src/GC/API/GCHandle/HandleCopy.cs | 11 ++- tests/src/GC/API/GCHandle/Normal.cs | 15 +++- tests/src/GC/API/GCHandle/Weak.cs | 23 +++--- tests/src/GC/API/GCHandleCollector/Usage.cs | 8 +- tests/src/GC/API/WeakReference/Finalize.cs | 40 +++++----- .../GC/Features/Finalizer/finalizeio/finalizeio.cs | 20 ++--- .../Finalizer/finalizeother/finalizearray.cs | 80 +++++++++---------- .../Finalizer/finalizeother/finalizearraysleep.cs | 90 +++++++++++----------- .../Finalizer/finalizeother/finalizedest.cs | 18 +++-- .../Finalizer/finalizeother/finalizeexcep.cs | 79 +++++++++---------- .../Finalizer/finalizeother/finalizeinherit.cs | 9 ++- .../Finalizer/finalizeother/finalizenested.cs | 25 +++--- .../GC/Features/Pinning/PinningOther/PinnedInt.cs | 2 + tests/src/GC/Performance/Tests/GCLarge.cs | 10 +++ tests/src/GC/Performance/Tests/GCMicroBench.cs | 21 +++-- tests/src/GC/Scenarios/BaseFinal/basefinal.cs | 31 +++++--- tests/src/GC/Scenarios/DoublinkList/dlbigleak.cs | 1 + .../GC/Scenarios/DoublinkList/doublinknoleak2.cs | 1 + .../src/GC/Scenarios/FinalNStruct/finalnstruct.cs | 23 +++--- tests/src/GC/Scenarios/FinalNStruct/nstructtun.cs | 12 ++- tests/src/GC/Scenarios/FragMan/fragman.cs | 5 +- tests/src/GC/Scenarios/LeakGen/leakgen.cs | 3 +- tests/src/GC/Scenarios/NDPin/ndpinfinal.cs | 29 +++++-- tests/src/GC/Scenarios/ReflectObj/reflectobj.cs | 4 +- tests/src/GC/Scenarios/Resurrection/continue.cs | 33 +++++--- tests/src/GC/Scenarios/Rootmem/rootmem.cs | 17 ++-- tests/src/GC/Scenarios/SingLinkList/singlinkgen.cs | 3 + .../src/GC/Scenarios/WeakReference/weakreffinal.cs | 2 + tests/src/GC/Stress/Tests/LargeObjectAlloc2.cs | 38 ++++++--- tests/src/GC/Stress/Tests/PlugGaps.cs | 5 ++ tests/src/GC/Stress/Tests/doubLinkStay.cs | 4 +- 37 files changed, 451 insertions(+), 287 deletions(-) diff --git a/tests/src/GC/API/GC/Finalize.cs b/tests/src/GC/API/GC/Finalize.cs index f4fc9b9..b8a853f 100644 --- a/tests/src/GC/API/GC/Finalize.cs +++ b/tests/src/GC/API/GC/Finalize.cs @@ -5,6 +5,7 @@ // Tests Finalize() and WaitForPendingFinalizers() using System; +using System.Runtime.CompilerServices; public class Test { @@ -28,12 +29,10 @@ public class Test obj = new Dummy(); } + [MethodImplAttribute(MethodImplOptions.NoInlining)] public void RunTest() { obj = null; - GC.Collect(); - - GC.WaitForPendingFinalizers(); // makes sure Finalize() is called. } } @@ -42,6 +41,9 @@ public class Test CreateObj temp = new CreateObj(); temp.RunTest(); + GC.Collect(); + GC.WaitForPendingFinalizers(); // makes sure Finalize() is called. + GC.Collect(); if (visited) { diff --git a/tests/src/GC/API/GC/GetTotalMemory.cs b/tests/src/GC/API/GC/GetTotalMemory.cs index eedafab..f62fafb 100644 --- a/tests/src/GC/API/GC/GetTotalMemory.cs +++ b/tests/src/GC/API/GC/GetTotalMemory.cs @@ -4,6 +4,7 @@ namespace DefaultNamespace { using System; + using System.Runtime.CompilerServices; internal class GetTotalMemory { @@ -11,9 +12,27 @@ namespace DefaultNamespace { // a discrepancy of more than 50 bytes should be investigated public const int padding = 50; - public static int Main(String [] args ) + public static bool AllocAndDealloc(int i, int MB, long heapSizeBeforeAlloc) { + byte[] bary = new byte[i*MB]; //allocate iMB memory + bary[0] = 1; + bary[i*MB-1] = 1; + + long heapSizeAfterAlloc = GC.GetTotalMemory(false); + Console.WriteLine( "HeapSize after allocated {0} MB memory: {1}", i, heapSizeAfterAlloc); + if( (heapSizeAfterAlloc - heapSizeBeforeAlloc)+i*padding<= i*MB || (heapSizeAfterAlloc - heapSizeBeforeAlloc) > (i+1)*MB ) + { + Console.WriteLine( "Test Failed" ); + return false; + } + bary[0] = 2; + bary[i*MB-1] = 2; + bary = null; + return true; + } + public static int Main(String [] args ) + { int MB = 1024*1024; int iRep = 0; Console.WriteLine("Test should return with ExitCode 100 ..."); @@ -38,35 +57,25 @@ namespace DefaultNamespace { // clean up memory before measuring GC.Collect(); GC.WaitForPendingFinalizers(); + GC.Collect(); long heapSizeBeforeAlloc = GC.GetTotalMemory(false); Console.WriteLine( "HeapSize before allocating any memory: {0}", heapSizeBeforeAlloc ); - byte[] bary = new byte[1]; for(int i=1; i<=iRep; i++ ) { - bary = new byte[i*MB]; //allocate iMB memory - bary[0] = 1; - bary[i*MB-1] = 1; - - long heapSizeAfterAlloc = GC.GetTotalMemory(false); - Console.WriteLine( "HeapSize after allocated {0} MB memory: {1}", i, heapSizeAfterAlloc); - if( (heapSizeAfterAlloc - heapSizeBeforeAlloc)+i*padding<= i*MB || (heapSizeAfterAlloc - heapSizeBeforeAlloc) > (i+1)*MB ) + if(!AllocAndDealloc(i, MB, heapSizeBeforeAlloc)) { - Console.WriteLine( "Test Failed" ); return 1; } - bary[0] = 2; - bary[i*MB-1] = 2; - bary = null; GC.Collect(); GC.WaitForPendingFinalizers(); + GC.Collect(); heapSizeBeforeAlloc = GC.GetTotalMemory(false); Console.WriteLine( "HeapSize after delete all objects: {0}", heapSizeBeforeAlloc ); - } Console.WriteLine( "Test Passed!" ); diff --git a/tests/src/GC/API/GC/KeepAlive.cs b/tests/src/GC/API/GC/KeepAlive.cs index 3174f89..c5dd55d 100644 --- a/tests/src/GC/API/GC/KeepAlive.cs +++ b/tests/src/GC/API/GC/KeepAlive.cs @@ -16,6 +16,7 @@ */ using System; +using System.Runtime.CompilerServices; public class Test { @@ -47,6 +48,7 @@ public class Test } + [MethodImplAttribute(MethodImplOptions.NoInlining)] public static void RunTest2() { Dummy2 obj2 = new Dummy2(); @@ -66,6 +68,7 @@ public class Test //for (int i=0; i<5; i++) { GC.Collect(); GC.WaitForPendingFinalizers(); + GC.Collect(); //} GC.KeepAlive(obj); // will keep obj alive until this point diff --git a/tests/src/GC/API/GC/KeepAliveNull.cs b/tests/src/GC/API/GC/KeepAliveNull.cs index 2091a1b..dcda501 100644 --- a/tests/src/GC/API/GC/KeepAliveNull.cs +++ b/tests/src/GC/API/GC/KeepAliveNull.cs @@ -5,6 +5,7 @@ // Tests KeepAlive() using System; +using System.Runtime.CompilerServices; public class Test { @@ -27,11 +28,19 @@ public class Test obj = new Dummy(); } - public void RunTest() + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public void DestroyObj() { obj = null; // this will collect the obj even if we have KeepAlive() + } + + public void RunTest() + { + DestroyObj(); + GC.Collect(); GC.WaitForPendingFinalizers(); + GC.Collect(); GC.KeepAlive(obj); // will keep alive 'obj' till this point } diff --git a/tests/src/GC/API/GC/KeepAliveRecur.cs b/tests/src/GC/API/GC/KeepAliveRecur.cs index abe28d5..2e2d1c8 100644 --- a/tests/src/GC/API/GC/KeepAliveRecur.cs +++ b/tests/src/GC/API/GC/KeepAliveRecur.cs @@ -28,6 +28,7 @@ public class Test GC.Collect(); GC.WaitForPendingFinalizers(); + GC.Collect(); foo(o); //Recursive call diff --git a/tests/src/GC/API/GC/SuppressFinalize.cs b/tests/src/GC/API/GC/SuppressFinalize.cs index b8c3aa8..9edeb96 100644 --- a/tests/src/GC/API/GC/SuppressFinalize.cs +++ b/tests/src/GC/API/GC/SuppressFinalize.cs @@ -5,6 +5,7 @@ // Tests SuppressFinalize() using System; +using System.Runtime.CompilerServices; public class Test { @@ -17,16 +18,21 @@ public class Test { } } - public static int Main() { - + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public static void RunTest() + { Dummy obj1 = new Dummy(); - GC.SuppressFinalize(obj1); // should not call the Finalizer() for obj1 obj1=null; - + } + + public static int Main() + { + RunTest(); + GC.Collect(); - GC.WaitForPendingFinalizers(); // call all Finalizers. + GC.Collect(); if(Dummy.visited == false) { Console.WriteLine("Test for SuppressFinalize() passed!"); diff --git a/tests/src/GC/API/GCHandle/HandleCopy.cs b/tests/src/GC/API/GCHandle/HandleCopy.cs index ccc124c..250ee9c 100644 --- a/tests/src/GC/API/GCHandle/HandleCopy.cs +++ b/tests/src/GC/API/GCHandle/HandleCopy.cs @@ -7,6 +7,7 @@ // Also tests the target of the handle. using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; public class Test @@ -37,13 +38,17 @@ public class Test copy = handle; } - public bool RunTest() + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public void DestroyObj() { - // ensuring that GC happens even with /debug mode obj = null; - GC.Collect(); + } + public bool RunTest() + { + GC.Collect(); GC.WaitForPendingFinalizers(); + GC.Collect(); bool ans1 = handle.IsAllocated; bool ans2 = copy.IsAllocated; diff --git a/tests/src/GC/API/GCHandle/Normal.cs b/tests/src/GC/API/GCHandle/Normal.cs index 6fc46ee..ffa7eaa 100644 --- a/tests/src/GC/API/GCHandle/Normal.cs +++ b/tests/src/GC/API/GCHandle/Normal.cs @@ -6,6 +6,7 @@ // should not be collected. using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; public class Test { @@ -19,21 +20,27 @@ public class Test { } } - public static int Main() { - - Dummy obj = new Dummy(); + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public static void RunTest() + { + Dummy obj = new Dummy(); Console.WriteLine("Allocating a normal handle to object.."); GCHandle handle = GCHandle.Alloc(obj,GCHandleType.Normal); // Normal handle // ensuring that GC happens even with /debug mode obj=null; + } + + + public static int Main() { + RunTest(); GC.Collect(); GC.WaitForPendingFinalizers(); + GC.Collect(); if(Dummy.flag == 0) { - Console.WriteLine("Test for GCHandleType.Normal passed!"); return 100; } diff --git a/tests/src/GC/API/GCHandle/Weak.cs b/tests/src/GC/API/GCHandle/Weak.cs index 8bcedd2..870625c 100644 --- a/tests/src/GC/API/GCHandle/Weak.cs +++ b/tests/src/GC/API/GCHandle/Weak.cs @@ -6,6 +6,7 @@ // will be collected. using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; public class Test @@ -31,30 +32,24 @@ public class Test GCHandle handle = GCHandle.Alloc(obj, GCHandleType.Weak); } - public bool RunTest() + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public void RunTest() { // ensuring that GC happens even with /debug mode obj = null; - GC.Collect(); - - GC.WaitForPendingFinalizers(); - - if (Dummy.flag == 99) - { - return true; - } - else - { - return false; - } } } public static int Main() { CreateObj temp = new CreateObj(); + temp.RunTest(); + + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); - if (temp.RunTest()) + if (Dummy.flag == 99) { Console.WriteLine("Test for GCHandleType.Weak passed!"); return 100; diff --git a/tests/src/GC/API/GCHandleCollector/Usage.cs b/tests/src/GC/API/GCHandleCollector/Usage.cs index f346966..54fe32e 100644 --- a/tests/src/GC/API/GCHandleCollector/Usage.cs +++ b/tests/src/GC/API/GCHandleCollector/Usage.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // the class that holds the HandleCollectors @@ -46,6 +47,7 @@ public class Usage private int _numInstances = 100; private const int deltaPercent = 10; + // ensures GC Collections occur when handle count exceeds maximum private bool Case1() { @@ -56,14 +58,16 @@ public class Usage GC.WaitForPendingFinalizers(); GC.Collect(); - HandleCollectorTest h; int original = GC.CollectionCount(0); + HandleCollectorTest h; + // create objects and let them go out of scope for (int i = 0; i < _numInstances; i++) h = new HandleCollectorTest(); h = null; + GC.WaitForPendingFinalizers(); // Collection should not have occurred @@ -96,7 +100,9 @@ public class Usage for (int i = 0; i < _numInstances; i++) { new HandleCollectorTest(); + GC.WaitForPendingFinalizers(); + handleCount = HandleCollectorTest.Count; //Note that the GC should occur when handle count is 101 but it will happen at anytime after a creation and we stick to the previous //count to avoid error diff --git a/tests/src/GC/API/WeakReference/Finalize.cs b/tests/src/GC/API/WeakReference/Finalize.cs index d873230..aa541e8 100644 --- a/tests/src/GC/API/WeakReference/Finalize.cs +++ b/tests/src/GC/API/WeakReference/Finalize.cs @@ -6,12 +6,13 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; public class Test { - public class Dummy { - + public class Dummy + { public static bool visited=false; ~Dummy() { Console.WriteLine("In Finalize() of Dummy"); @@ -20,17 +21,20 @@ public class Test { } } - public class CreateObj { + public class CreateObj + { Dummy dummy1; Dummy dummy2; - public CreateObj() { + public CreateObj() + { dummy1 = new Dummy(); dummy2 = new Dummy(); } - public bool RunTest() { - + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public void RunTest() + { WeakReference weak1 = new WeakReference(dummy1); GCHandle handle = GCHandle.Alloc(dummy1,GCHandleType.Normal); // Strong Reference @@ -39,27 +43,25 @@ public class Test { // ensuring that GC happens even with /debug mode dummy1=null; dummy2=null; - - GC.Collect(); - GC.WaitForPendingFinalizers(); - - if(Dummy.visited == true) - return true; - else - return false; } } - public static int Main() { - + public static int Main() + { CreateObj temp = new CreateObj(); - bool passed = temp.RunTest(); + temp.RunTest(); + + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); - if(passed) { + if (Dummy.visited) + { Console.WriteLine("Test for WeakReference.Finalize() passed!"); return 100; } - else { + else + { Console.WriteLine("Test for WeakReference.Finalize() failed!"); return 1; } diff --git a/tests/src/GC/Features/Finalizer/finalizeio/finalizeio.cs b/tests/src/GC/Features/Finalizer/finalizeio/finalizeio.cs index 9f07860..256ed99 100644 --- a/tests/src/GC/Features/Finalizer/finalizeio/finalizeio.cs +++ b/tests/src/GC/Features/Finalizer/finalizeio/finalizeio.cs @@ -6,6 +6,7 @@ using System; using System.IO; +using System.Runtime.CompilerServices; public class Test { @@ -45,15 +46,9 @@ public class Test { obj = new Dummy(); } - public bool RunTest() { - + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public void RunTest() { obj=null; - GC.Collect(); - - GC.WaitForPendingFinalizers(); // makes sure Finalize() is called. - - return Dummy.visited; - } } @@ -90,8 +85,15 @@ Line 23 ******************* END *****************************"); } + temp.RunTest(); - if (temp.RunTest()) { + GC.Collect(); + GC.WaitForPendingFinalizers(); // makes sure Finalize() is called. + GC.Collect(); + + + if (Dummy.visited) + { Console.WriteLine("Test for Finalize() & WaitForPendingFinalizers() passed!"); return 100; } diff --git a/tests/src/GC/Features/Finalizer/finalizeother/finalizearray.cs b/tests/src/GC/Features/Finalizer/finalizeother/finalizearray.cs index 373bf83..7b540c4 100644 --- a/tests/src/GC/Features/Finalizer/finalizeother/finalizearray.cs +++ b/tests/src/GC/Features/Finalizer/finalizeother/finalizearray.cs @@ -5,46 +5,46 @@ // Tests Finalize() on array of objects using System; +using System.Runtime.CompilerServices; + +public class Test +{ + public class Dummy + { + public static int count=0; + ~Dummy() + { + count++; + } + } + + public class CreateObj + { + public Dummy[] obj; + + public CreateObj() { + obj = new Dummy[10000]; + for(int i=0;i<10000;i++) { + obj[i] = new Dummy(); + } + } + + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public void RunTest() + { + obj=null; // making sure collect is called even with /debug + } + } + + public static int Main() { + CreateObj temp = new CreateObj(); + temp.RunTest(); + + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); -public class Test { - - public class Dummy { - public static int count=0; - ~Dummy() { - count++; - } - } - - public class CreateObj { - public Dummy[] obj; - - public CreateObj() { - - obj = new Dummy[10000]; - for(int i=0;i<10000;i++) { - obj[i] = new Dummy(); - } - } - - public bool RunTest() { - obj=null; // making sure collect is called even with /debug - GC.Collect(); - GC.WaitForPendingFinalizers(); - - if(Dummy.count == 10000) { // all objects in array finalized! - return true; - } - else { - return false; - } - } - } - - public static int Main() { - - CreateObj temp = new CreateObj(); - - if (temp.RunTest()) + if (Dummy.count == 10000) // all objects in array finalized! { Console.WriteLine("Test for Finalize() for array of objects passed!"); return 100; @@ -55,5 +55,5 @@ public class Test { return 1; } - } + } } diff --git a/tests/src/GC/Features/Finalizer/finalizeother/finalizearraysleep.cs b/tests/src/GC/Features/Finalizer/finalizeother/finalizearraysleep.cs index caf2be0..0d35ecd 100644 --- a/tests/src/GC/Features/Finalizer/finalizeother/finalizearraysleep.cs +++ b/tests/src/GC/Features/Finalizer/finalizeother/finalizearraysleep.cs @@ -6,54 +6,50 @@ using System; using System.Threading; +using System.Runtime.CompilerServices; public class Test { - public class Dummy { - public static int count=0; - ~Dummy() { - count++; - Thread.Sleep(1000); - } - } - - public class CreateObj { - public Dummy[] obj; - public int ExitCode = 0; - - public CreateObj() { - obj = new Dummy[10]; - - for(int i=0;i<10;i++) { - obj[i] = new Dummy(); - } - } - - public void RunTest() { - - obj=null; // making sure collect is called even with /debug - GC.Collect(); - GC.WaitForPendingFinalizers(); - - if(Dummy.count == 10) { // all objects in array finalized! - ExitCode = 100; - //Console.WriteLine("Test for Finalize() for array of objects passed!"); - } - else { - ExitCode = 1; - //Console.WriteLine("Test for Finalize() for array of objects failed!"); - } - } - } - - public static int Main() { - CreateObj temp = new CreateObj(); - temp.RunTest(); - - if(temp.ExitCode==100) - Console.WriteLine("Test for Finalize() for array of objects passed!"); - else - Console.WriteLine("Test for Finalize() for array of objects failed!"); - return temp.ExitCode; - } + public class Dummy { + public static int count=0; + ~Dummy() { + count++; + Thread.Sleep(1000); + } + } + + public class CreateObj { + public Dummy[] obj; + public int ExitCode = 0; + + public CreateObj() { + obj = new Dummy[10]; + + for(int i=0;i<10;i++) { + obj[i] = new Dummy(); + } + } + + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public void RunTest() { + obj=null; // making sure collect is called even with /debug + GC.Collect(); + GC.WaitForPendingFinalizers(); + } + } + + public static int Main() { + CreateObj temp = new CreateObj(); + temp.RunTest(); + + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + + if (Dummy.count == 10) + Console.WriteLine("Test for Finalize() for array of objects passed!"); + else + Console.WriteLine("Test for Finalize() for array of objects failed!"); + return temp.ExitCode; + } } diff --git a/tests/src/GC/Features/Finalizer/finalizeother/finalizedest.cs b/tests/src/GC/Features/Finalizer/finalizeother/finalizedest.cs index 5e7cbe2..a736ff9 100644 --- a/tests/src/GC/Features/Finalizer/finalizeother/finalizedest.cs +++ b/tests/src/GC/Features/Finalizer/finalizeother/finalizedest.cs @@ -5,6 +5,7 @@ // Tests Finalize() and WaitForPendingFinalizers() using System; +using System.Runtime.CompilerServices; public class Test { @@ -12,7 +13,7 @@ public class Test public class Dummy { - public static bool visited; + public static bool visited=false; ~Dummy() { @@ -33,22 +34,23 @@ public class Test obj = new Dummy(); } - public bool RunTest() + [MethodImpl(MethodImplOptions.NoInlining)] + public void RunTest() { obj=null; - GC.Collect(); - - GC.WaitForPendingFinalizers(); // makes sure Finalize() is called. - - return Dummy.visited; } } public static int Main() { CreateObj temp = new CreateObj(); + temp.RunTest(); + + GC.Collect(); + GC.WaitForPendingFinalizers(); // makes sure Finalize() is called. + GC.Collect(); - if (temp.RunTest()) + if (Dummy.visited) { Console.WriteLine("Test Passed"); return 100; diff --git a/tests/src/GC/Features/Finalizer/finalizeother/finalizeexcep.cs b/tests/src/GC/Features/Finalizer/finalizeother/finalizeexcep.cs index c08cf6d..eed1395 100644 --- a/tests/src/GC/Features/Finalizer/finalizeother/finalizeexcep.cs +++ b/tests/src/GC/Features/Finalizer/finalizeother/finalizeexcep.cs @@ -5,58 +5,53 @@ // Tests Exception handling in Finalize() using System; +using System.Runtime.CompilerServices; public class Test { - public class List { - public int val; - public List next; - } - public class Dummy { + public class List { + public int val; + public List next; + } - public static bool visited; - - ~Dummy() { - List lst = new List(); - Console.WriteLine("In Finalize() of Dummy"); - try { - Console.WriteLine(lst.next.val); // should throw nullreference exception - } catch(NullReferenceException) { + public class Dummy { + public static bool visited; + + ~Dummy() { + List lst = new List(); + Console.WriteLine("In Finalize() of Dummy"); + try { + Console.WriteLine(lst.next.val); // should throw nullreference exception + } catch(NullReferenceException) { Console.WriteLine("Caught NullReferenceException in Finalize()"); visited=true; - } - - - } - } + } + } + } - public class CreateObj { - public Dummy obj; + public class CreateObj { + public Dummy obj; - public CreateObj() { - obj = new Dummy(); - } + public CreateObj() { + obj = new Dummy(); + } - public bool RunTest() { - obj=null; - GC.Collect(); - - GC.WaitForPendingFinalizers(); // makes sure Finalize() is called. + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public void RunTest() { + obj=null; + } + } - if(Dummy.visited == true) { - return true; - } - else { - return false; - } - } - } + public static int Main() { - public static int Main() { + CreateObj temp= new CreateObj(); + temp.RunTest(); - CreateObj temp= new CreateObj(); + GC.Collect(); + GC.WaitForPendingFinalizers(); // makes sure Finalize() is called. + GC.Collect(); - if (temp.RunTest()) + if (Dummy.visited) { Console.WriteLine("Test for Exception handling in Finalize() passed!"); return 100; @@ -66,7 +61,7 @@ public class Test { Console.WriteLine("Test for Exception handling in Finalize() failed!"); return 1; } - - - } + + + } } diff --git a/tests/src/GC/Features/Finalizer/finalizeother/finalizeinherit.cs b/tests/src/GC/Features/Finalizer/finalizeother/finalizeinherit.cs index cfce1f6..c24f0be 100644 --- a/tests/src/GC/Features/Finalizer/finalizeother/finalizeinherit.cs +++ b/tests/src/GC/Features/Finalizer/finalizeother/finalizeinherit.cs @@ -5,6 +5,7 @@ // Tests Finalize() with Inheritance using System; +using System.Runtime.CompilerServices; namespace One { @@ -61,6 +62,7 @@ namespace Three { d = new D(); } + [MethodImplAttribute(MethodImplOptions.NoInlining)] public bool RunTest() { A a = c; @@ -83,7 +85,12 @@ namespace Three { { CreateObj temp = new CreateObj(); - if (temp.RunTest()) + temp.RunTest(); + + GC.Collect(); + GC.WaitForPendingFinalizers(); + + if (C.count == 2) { Console.WriteLine("Test Passed"); return 100; diff --git a/tests/src/GC/Features/Finalizer/finalizeother/finalizenested.cs b/tests/src/GC/Features/Finalizer/finalizeother/finalizenested.cs index 2eb6719..5f22b5a 100644 --- a/tests/src/GC/Features/Finalizer/finalizeother/finalizenested.cs +++ b/tests/src/GC/Features/Finalizer/finalizeother/finalizenested.cs @@ -6,6 +6,7 @@ using System; using System.Threading; +using System.Runtime.CompilerServices; public class Test { @@ -99,22 +100,28 @@ public class Test { obj=new Dummy(); } - public bool RunTest() + public void RunTest() { obj=null; - GC.Collect(); - - GC.WaitForPendingFinalizers(); // makes sure Finalize() is called. - - return Dummy.visited; } } - public static int Main() { - + [MethodImpl(MethodImplOptions.NoInlining)] + public static void AllocAndDealloc() + { CreateObj temp = new CreateObj(); + temp.RunTest(); + } + + public static int Main() + { + AllocAndDealloc(); + + GC.Collect(); + GC.WaitForPendingFinalizers(); // makes sure Finalize() is called. + GC.Collect(); - if (temp.RunTest()) + if (Dummy.visited) { Console.WriteLine("Test Passed"); return 100; diff --git a/tests/src/GC/Features/Pinning/PinningOther/PinnedInt.cs b/tests/src/GC/Features/Pinning/PinningOther/PinnedInt.cs index e8e38ed..afa9a0f 100644 --- a/tests/src/GC/Features/Pinning/PinningOther/PinnedInt.cs +++ b/tests/src/GC/Features/Pinning/PinningOther/PinnedInt.cs @@ -19,8 +19,10 @@ public class Test temp1 = GCUtil.GetTarget(handle); Console.WriteLine(temp1); + GC.Collect(); GC.WaitForPendingFinalizers(); + GC.Collect(); temp2 = GCUtil.GetTarget(handle); Console.WriteLine(temp2); diff --git a/tests/src/GC/Performance/Tests/GCLarge.cs b/tests/src/GC/Performance/Tests/GCLarge.cs index 18284a4..d70e208 100644 --- a/tests/src/GC/Performance/Tests/GCLarge.cs +++ b/tests/src/GC/Performance/Tests/GCLarge.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Runtime.CompilerServices; internal class List { @@ -24,23 +25,27 @@ internal class List CreateLargeObjects(); GC.Collect(); GC.WaitForPendingFinalizers(); + GC.Collect(); } //Large Object Collection (half array) CreateLargeObjectsHalf(); GC.Collect(); GC.WaitForPendingFinalizers(); + GC.Collect(); for(long i = 0; i < iterations; i++) { CreateLargeObjectsHalf(); GC.Collect(); GC.WaitForPendingFinalizers(); + GC.Collect(); } //Promote from Gen1 to Gen2 SmallGC [] sgc; sgc = new SmallGC [LOOP]; + for (int j = 0; j < LOOP; j++) sgc[j] = new SmallGC(0); @@ -51,6 +56,7 @@ internal class List GC.Collect(); GC.WaitForPendingFinalizers(); + GC.Collect(); for(long i = 0; i < iterations; i++) { @@ -110,6 +116,7 @@ internal class List } } + [MethodImplAttribute(MethodImplOptions.NoInlining)] public static List PopulateList(int len) { if (len == 0) return null; @@ -127,6 +134,7 @@ internal class List } return Node; } + public static int ValidateList(List First, int len) { List tmp1 = First; @@ -151,6 +159,7 @@ internal class List } + [MethodImplAttribute(MethodImplOptions.NoInlining)] public static void CreateLargeObjects() { LargeGC [] lgc; @@ -159,6 +168,7 @@ internal class List lgc[i] = new LargeGC(); } + [MethodImplAttribute(MethodImplOptions.NoInlining)] public static void CreateLargeObjectsHalf() { LargeGC [] lgc; diff --git a/tests/src/GC/Performance/Tests/GCMicroBench.cs b/tests/src/GC/Performance/Tests/GCMicroBench.cs index c15daa4..377c98c 100644 --- a/tests/src/GC/Performance/Tests/GCMicroBench.cs +++ b/tests/src/GC/Performance/Tests/GCMicroBench.cs @@ -4,6 +4,7 @@ using System; using System.Diagnostics; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Collections.Generic; @@ -252,25 +253,31 @@ namespace GC_Microbenchmarks } - // releases references to allocated objects - // times GC.Collect() - // if objects are finalizable, also times GC.WaitForPendingFinalizers() - public void Deallocate() + public bool ClearList() { - bool finalizable = false; - if (m_list != null) { - finalizable = ( (m_list.Count > 0) && (m_list[0] is FNode)); m_list.Clear(); m_list = null; + return ( (m_list.Count > 0) && (m_list[0] is FNode)); } + return false; + } + + + // releases references to allocated objects + // times GC.Collect() + // if objects are finalizable, also times GC.WaitForPendingFinalizers() + public void Deallocate() + { + bool finalizable = ClearList(); GC.Collect(); if (finalizable) { GC.WaitForPendingFinalizers(); + GC.Collect(); } } diff --git a/tests/src/GC/Scenarios/BaseFinal/basefinal.cs b/tests/src/GC/Scenarios/BaseFinal/basefinal.cs index 5f61f2e..b15a9a7 100644 --- a/tests/src/GC/Scenarios/BaseFinal/basefinal.cs +++ b/tests/src/GC/Scenarios/BaseFinal/basefinal.cs @@ -10,6 +10,7 @@ namespace DefaultNamespace { using System; + using System.Runtime.CompilerServices; using System.Collections.Generic; internal class BaseFinal @@ -104,18 +105,15 @@ namespace DefaultNamespace { //Console.WriteLine("after all objects were created, the heapsize is " + GC.GetTotalMemory(false)); } - public bool RunTest() + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public void DestroyNode() { obj = null; + } - GC.Collect(); - GC.WaitForPendingFinalizers(); - GC.Collect(); - - Console.Write(BNode.icFinalNode); - Console.WriteLine(" Nodes were finalized and resurrected."); - //Console.WriteLine("after all objects were deleted and resurrected in Finalize() , the heapsize is " + GC.GetTotalMemory(false)); - + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public void ResurrectNodes() + { for(int i=0; i< BNode.rlNode.Count; i++) { BNode oldNode = (BNode)BNode.rlNode[ i ]; @@ -126,6 +124,21 @@ namespace DefaultNamespace { oldNode = null; BNode.rlNode[ i ] = null; } + } + + public bool RunTest() + { + DestroyNode(); + + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + + Console.Write(BNode.icFinalNode); + Console.WriteLine(" Nodes were finalized and resurrected."); + //Console.WriteLine("after all objects were deleted and resurrected in Finalize() , the heapsize is " + GC.GetTotalMemory(false)); + + ResurrectNodes(); GC.Collect(); GC.WaitForPendingFinalizers(); diff --git a/tests/src/GC/Scenarios/DoublinkList/dlbigleak.cs b/tests/src/GC/Scenarios/DoublinkList/dlbigleak.cs index eee0b74..355335b 100644 --- a/tests/src/GC/Scenarios/DoublinkList/dlbigleak.cs +++ b/tests/src/GC/Scenarios/DoublinkList/dlbigleak.cs @@ -107,6 +107,7 @@ namespace DoubLink { } + [MethodImpl(MethodImplOptions.NoInlining)] public void MakeLeak(int iRep) { diff --git a/tests/src/GC/Scenarios/DoublinkList/doublinknoleak2.cs b/tests/src/GC/Scenarios/DoublinkList/doublinknoleak2.cs index 12f9948..272a130 100644 --- a/tests/src/GC/Scenarios/DoublinkList/doublinknoleak2.cs +++ b/tests/src/GC/Scenarios/DoublinkList/doublinknoleak2.cs @@ -93,6 +93,7 @@ namespace DoubLink { GC.Collect(); GC.WaitForPendingFinalizers(); + GC.Collect(); //do a second GC collect since some nodes may have been still alive at the time of first collect GC.Collect(); diff --git a/tests/src/GC/Scenarios/FinalNStruct/finalnstruct.cs b/tests/src/GC/Scenarios/FinalNStruct/finalnstruct.cs index dec9926..c5547e5 100644 --- a/tests/src/GC/Scenarios/FinalNStruct/finalnstruct.cs +++ b/tests/src/GC/Scenarios/FinalNStruct/finalnstruct.cs @@ -11,47 +11,42 @@ namespace NStruct { using System; + using System.Runtime.CompilerServices; internal class FinalNStruct { - public static STRMAP[] CreateObj(int iObj) + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public static void CreateObj(int iObj) { - STRMAP []strmap = new STRMAP[iObj]; for (int i=0; i< iObj; i++ ) //allocate 3100KB { strmap[i] = new STRMAP(); } - return strmap; - - } - - public static bool RunTest(int iObj,STRMAP []strmap) - { - for( int i=0; i< iObj; i++ ) { strmap[i] = null; } + } + public static bool RunTest() + { GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); return ( FinalizeCount.icFinal == FinalizeCount.icCreat ); - } public static int Main(String [] args){ - int iObj = 100; - STRMAP []strmaptemp; Console.WriteLine("Test should return with ExitCode 100 ..."); - strmaptemp = CreateObj(iObj); - if (RunTest(iObj,strmaptemp)) + CreateObj(iObj); + + if (RunTest()) { Console.WriteLine( "Created objects number is same with finalized objects." ); Console.WriteLine( "Test Passed !" ); diff --git a/tests/src/GC/Scenarios/FinalNStruct/nstructtun.cs b/tests/src/GC/Scenarios/FinalNStruct/nstructtun.cs index 2523fcc..48885fe 100644 --- a/tests/src/GC/Scenarios/FinalNStruct/nstructtun.cs +++ b/tests/src/GC/Scenarios/FinalNStruct/nstructtun.cs @@ -4,6 +4,7 @@ namespace NStruct { using System; + using System.Runtime.CompilerServices; internal class NStructTun { @@ -14,18 +15,23 @@ namespace NStruct { #pragma warning disable 0414 private STRMAP Strmap; #pragma warning restore 0414 + public CreateObj(int Rep) { for( int i=0; i< Rep; i++ ) { Strmap = new STRMAP(); } - } - - public bool RunTest() + } + + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public void DeleteStrmap() { Strmap=null; + } + public bool RunTest() + { GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); diff --git a/tests/src/GC/Scenarios/FragMan/fragman.cs b/tests/src/GC/Scenarios/FragMan/fragman.cs index dcd330c..c258f9a 100644 --- a/tests/src/GC/Scenarios/FragMan/fragman.cs +++ b/tests/src/GC/Scenarios/FragMan/fragman.cs @@ -11,6 +11,7 @@ namespace DefaultNamespace { using System; + using System.Runtime.CompilerServices; public class FragMan { @@ -26,6 +27,7 @@ namespace DefaultNamespace { GC.Collect(); GC.WaitForPendingFinalizers(); + GC.Collect(); if (FragNode.Finalized == 0) { @@ -45,7 +47,7 @@ namespace DefaultNamespace { public FragMan( ) { - buildTree( ); + buildTree(); fnM = CvA_FNodes[12]; CvA_FNodes = null; enumNode( fnM ); @@ -83,6 +85,7 @@ namespace DefaultNamespace { } + [MethodImplAttribute(MethodImplOptions.NoInlining)] public void buildTree( ) { CvA_FNodes = new FragNode[26]; diff --git a/tests/src/GC/Scenarios/LeakGen/leakgen.cs b/tests/src/GC/Scenarios/LeakGen/leakgen.cs index 8ec6531..fbc3795 100644 --- a/tests/src/GC/Scenarios/LeakGen/leakgen.cs +++ b/tests/src/GC/Scenarios/LeakGen/leakgen.cs @@ -5,6 +5,7 @@ namespace LGen { using System; + using System.Runtime.CompilerServices; public class LeakGen { @@ -52,7 +53,6 @@ namespace LGen { } } - public bool runTest(int iRep, int iObj) { @@ -71,6 +71,7 @@ namespace LGen { } + [MethodImplAttribute(MethodImplOptions.NoInlining)] public void MakeLeak(int iObj) { int [] mem; diff --git a/tests/src/GC/Scenarios/NDPin/ndpinfinal.cs b/tests/src/GC/Scenarios/NDPin/ndpinfinal.cs index 1cb5e0e..5bd478a 100644 --- a/tests/src/GC/Scenarios/NDPin/ndpinfinal.cs +++ b/tests/src/GC/Scenarios/NDPin/ndpinfinal.cs @@ -13,8 +13,10 @@ namespace DefaultNamespace { using System; using System.Collections.Generic; + using System.Runtime.CompilerServices; using System.Runtime.InteropServices; + internal class NDPinFinal { internal Object p; @@ -55,6 +57,24 @@ namespace DefaultNamespace { } } + + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public static void RemoveN() + { + m_n = null; + } + + + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public static void RemovePinList(int iObj) + { + for ( int i=0; i< iObj; i++ ) + { + pinList[i] = null; + } + } + + public static bool RunTest(int iObj) { @@ -64,18 +84,17 @@ namespace DefaultNamespace { return false; } - m_n = null; + RemoveN(); + GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); - for( int i=0; i< iObj; i++ ) - { - pinList[i] = null; - } + RemovePinList(iObj); GC.Collect(); GC.WaitForPendingFinalizers(); + GC.Collect(); if( cFinalObj == cCreatObj ) { diff --git a/tests/src/GC/Scenarios/ReflectObj/reflectobj.cs b/tests/src/GC/Scenarios/ReflectObj/reflectobj.cs index b3cc55b..5a4f5ce 100644 --- a/tests/src/GC/Scenarios/ReflectObj/reflectobj.cs +++ b/tests/src/GC/Scenarios/ReflectObj/reflectobj.cs @@ -16,8 +16,9 @@ namespace App { using System; - using System.Reflection; using System.Collections.Generic; + using System.Reflection; + using System.Runtime.CompilerServices; class ReflectObj { @@ -112,6 +113,7 @@ namespace App { } + [MethodImplAttribute(MethodImplOptions.NoInlining)] public void CreateMoreObj() { rtype = new Type[0]; diff --git a/tests/src/GC/Scenarios/Resurrection/continue.cs b/tests/src/GC/Scenarios/Resurrection/continue.cs index f071a55..f2bfeaf 100644 --- a/tests/src/GC/Scenarios/Resurrection/continue.cs +++ b/tests/src/GC/Scenarios/Resurrection/continue.cs @@ -4,6 +4,7 @@ namespace DefaultNamespace { using System; + using System.Runtime.CompilerServices; internal class Continue @@ -15,7 +16,9 @@ namespace DefaultNamespace { public class CreateObj { BNode obj; + #pragma warning restore 0414 + [MethodImplAttribute(MethodImplOptions.NoInlining)] public CreateObj() { Continue mv_Obj = new Continue(); @@ -30,17 +33,16 @@ namespace DefaultNamespace { Console.WriteLine(" Nodes were created."); } - - public bool RunTest() + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public void DestroyObj() { obj = null; - GC.Collect(); - GC.WaitForPendingFinalizers(); - GC.Collect(); + } - Console.Write(BNode.icFinalNode); - Console.WriteLine(" Nodes were finalized and resurrected."); + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public void ResurrectNodes() + { for (int i = 0; i < BNode.rlNodeCount; i++) { BNode oldNode = (BNode)BNode.rlNode[i]; @@ -51,17 +53,30 @@ namespace DefaultNamespace { oldNode = null; BNode.rlNode[ i ] = null; } + } + + public bool RunTest() + { + DestroyObj(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); - return ( BNode.icCreateNode == BNode.icFinalNode ); + Console.Write(BNode.icFinalNode); + Console.WriteLine(" Nodes were finalized and resurrected."); - } + ResurrectNodes(); + + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + return ( BNode.icCreateNode == BNode.icFinalNode ); + } } + public static int Main() { diff --git a/tests/src/GC/Scenarios/Rootmem/rootmem.cs b/tests/src/GC/Scenarios/Rootmem/rootmem.cs index b11d6d5..5b78025 100644 --- a/tests/src/GC/Scenarios/Rootmem/rootmem.cs +++ b/tests/src/GC/Scenarios/Rootmem/rootmem.cs @@ -10,6 +10,7 @@ namespace DefaultNamespace { using System; + using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal class RootMem @@ -18,6 +19,12 @@ namespace DefaultNamespace { internal static GCHandle [] root; internal static int n; + + [MethodImplAttribute(MethodImplOptions.NoInlining)] + public static void AllocRoot() + { + } + public static int Main( String [] args ) { int iSize = 1000; @@ -32,30 +39,28 @@ namespace DefaultNamespace { rm_obj = new RootMem( n ); root[n] = GCHandle.Alloc(rm_obj ); } - //Console.WriteLine("After save objects to Root and before GCed: "+GC.GetTotalMemory(false) ); + GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); - //Console.WriteLine("After save objects to Root and after GCed: "+GC.GetTotalMemory(false) ); Object v; for( int i=0; i< iSize; i++) { v = ( root[i]) ; } - //Console.WriteLine("After Get objects from root and before GCed: "+GC.GetTotalMemory(false) ); + GC.Collect(); - //Console.WriteLine("After Get objects from root and after GCed: "+GC.GetTotalMemory(false) ); for( int i=0; i