Fix GC tests that contain GC.Collect() used in wrong scope (#17680)
authorSung Yoon Whang <suwhang@microsoft.com>
Fri, 20 Apr 2018 22:30:56 +0000 (15:30 -0700)
committerGitHub <noreply@github.com>
Fri, 20 Apr 2018 22:30:56 +0000 (15:30 -0700)
* 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

37 files changed:
tests/src/GC/API/GC/Finalize.cs
tests/src/GC/API/GC/GetTotalMemory.cs
tests/src/GC/API/GC/KeepAlive.cs
tests/src/GC/API/GC/KeepAliveNull.cs
tests/src/GC/API/GC/KeepAliveRecur.cs
tests/src/GC/API/GC/SuppressFinalize.cs
tests/src/GC/API/GCHandle/HandleCopy.cs
tests/src/GC/API/GCHandle/Normal.cs
tests/src/GC/API/GCHandle/Weak.cs
tests/src/GC/API/GCHandleCollector/Usage.cs
tests/src/GC/API/WeakReference/Finalize.cs
tests/src/GC/Features/Finalizer/finalizeio/finalizeio.cs
tests/src/GC/Features/Finalizer/finalizeother/finalizearray.cs
tests/src/GC/Features/Finalizer/finalizeother/finalizearraysleep.cs
tests/src/GC/Features/Finalizer/finalizeother/finalizedest.cs
tests/src/GC/Features/Finalizer/finalizeother/finalizeexcep.cs
tests/src/GC/Features/Finalizer/finalizeother/finalizeinherit.cs
tests/src/GC/Features/Finalizer/finalizeother/finalizenested.cs
tests/src/GC/Features/Pinning/PinningOther/PinnedInt.cs
tests/src/GC/Performance/Tests/GCLarge.cs
tests/src/GC/Performance/Tests/GCMicroBench.cs
tests/src/GC/Scenarios/BaseFinal/basefinal.cs
tests/src/GC/Scenarios/DoublinkList/dlbigleak.cs
tests/src/GC/Scenarios/DoublinkList/doublinknoleak2.cs
tests/src/GC/Scenarios/FinalNStruct/finalnstruct.cs
tests/src/GC/Scenarios/FinalNStruct/nstructtun.cs
tests/src/GC/Scenarios/FragMan/fragman.cs
tests/src/GC/Scenarios/LeakGen/leakgen.cs
tests/src/GC/Scenarios/NDPin/ndpinfinal.cs
tests/src/GC/Scenarios/ReflectObj/reflectobj.cs
tests/src/GC/Scenarios/Resurrection/continue.cs
tests/src/GC/Scenarios/Rootmem/rootmem.cs
tests/src/GC/Scenarios/SingLinkList/singlinkgen.cs
tests/src/GC/Scenarios/WeakReference/weakreffinal.cs
tests/src/GC/Stress/Tests/LargeObjectAlloc2.cs
tests/src/GC/Stress/Tests/PlugGaps.cs
tests/src/GC/Stress/Tests/doubLinkStay.cs

index f4fc9b9..b8a853f 100644 (file)
@@ -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)
         {
index eedafab..f62fafb 100644 (file)
@@ -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!" );
index 3174f89..c5dd55d 100644 (file)
@@ -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
index 2091a1b..dcda501 100644 (file)
@@ -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
         }
index abe28d5..2e2d1c8 100644 (file)
@@ -28,6 +28,7 @@ public class Test
 
         GC.Collect();
         GC.WaitForPendingFinalizers();
+        GC.Collect();
 
         foo(o);     //Recursive call
 
index b8c3aa8..9edeb96 100644 (file)
@@ -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!");
index ccc124c..250ee9c 100644 (file)
@@ -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;
index 6fc46ee..ffa7eaa 100644 (file)
@@ -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;
                }
index 8bcedd2..870625c 100644 (file)
@@ -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;
index f346966..54fe32e 100644 (file)
@@ -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
index d873230..aa541e8 100644 (file)
@@ -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;
         }
index 9f07860..256ed99 100644 (file)
@@ -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;
         }
index 373bf83..7b540c4 100644 (file)
@@ -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;
         }
 
-       }
+    }
 }
index caf2be0..0d35ecd 100644 (file)
@@ -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;                          
+    }
 }
index 5e7cbe2..a736ff9 100644 (file)
@@ -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;
index c08cf6d..eed1395 100644 (file)
@@ -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;
         }
-               
-               
-       }
+
+
+    }
 }
index cfce1f6..c24f0be 100644 (file)
@@ -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;
index 2eb6719..5f22b5a 100644 (file)
@@ -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;
index e8e38ed..afa9a0f 100644 (file)
@@ -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);
index 18284a4..d70e208 100644 (file)
@@ -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;
index c15daa4..377c98c 100644 (file)
@@ -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();
             }
 
         }
index 5f61f2e..b15a9a7 100644 (file)
@@ -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();
index eee0b74..355335b 100644 (file)
@@ -107,6 +107,7 @@ namespace DoubLink {
         }
 
 
+        [MethodImpl(MethodImplOptions.NoInlining)]
         public void MakeLeak(int iRep)
         {
 
index 12f9948..272a130 100644 (file)
@@ -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();
index dec9926..c5547e5 100644 (file)
 
 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 !" );
index 2523fcc..48885fe 100644 (file)
@@ -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();
index dcd330c..c258f9a 100644 (file)
@@ -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];
index 8ec6531..fbc3795 100644 (file)
@@ -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;
index 1cb5e0e..5bd478a 100644 (file)
@@ -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 )
             {
index b3cc55b..5a4f5ce 100644 (file)
@@ -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];
index f071a55..f2bfeaf 100644 (file)
@@ -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()
         {
 
index b11d6d5..5b78025 100644 (file)
@@ -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<iSize; i++ )
             {
                 root[i].Free();
             }
-            //Console.WriteLine("After free root and before GCed: "+GC.GetTotalMemory(false) );
+
             GC.Collect();
             GC.WaitForPendingFinalizers();
             GC.Collect();
-            //Console.WriteLine("After free root and after GCed: "+GC.GetTotalMemory(false) );
+
             try
             {
                 for( int i=0; i<iSize; i++ )
index db68ab7..0abb05e 100644 (file)
@@ -93,7 +93,10 @@ namespace SingLink {
                 //Console.WriteLine("after number {0} singlink is set: {1}", i, GC.GetTotalMemory(false) );
 
                 Delete();
+
+                GC.Collect();
                 GC.WaitForPendingFinalizers();
+                GC.Collect();
 
             }
             //Console.WriteLine("total allocated memory: {0}", GC.GetTotalMemory(false));
index 754e88d..5e7b26a 100644 (file)
@@ -5,6 +5,7 @@
 namespace DefaultNamespace {
     using System;
     using System.Collections.Generic;
+    using System.Runtime.CompilerServices;
 
     internal class CreateObj
     {
@@ -31,6 +32,7 @@ namespace DefaultNamespace {
             return result;
         }
 
+        [MethodImplAttribute(MethodImplOptions.NoInlining)]
         public void DeleteObj(int iObj,int iSwitch)
         {
             for( int i= 0; i< iObj; i++ )
index d559319..86a27d9 100644 (file)
@@ -9,6 +9,7 @@
 
 
 using System;
+using System.Runtime.CompilerServices;
 
 namespace LargeObjectTest
 {
@@ -54,10 +55,29 @@ namespace LargeObjectTest
     {
         public static int ExitCode = -1;
 
+        [MethodImplAttribute(MethodImplOptions.NoInlining)]
+        public static bool AllocAndCollect(int loop)
+        {
+            LargeObject largeobj;
+            try
+            {
+                largeobj = new LargeObject();
+                TestLibrary.Logging.WriteLine("Allocated LargeObject");
+            }
+            catch (Exception e)
+            {
+                TestLibrary.Logging.WriteLine("Failure to allocate at loop {0}\n", loop);
+                TestLibrary.Logging.WriteLine("Caught Exception: {0}", e);
+                return false;
+            }
+
+            largeobj = null;
+            return true;
+        }
+
         public static int Main()
         {
             int loop = 0;
-            LargeObject largeobj;
 
             TestLibrary.Logging.WriteLine("Test should pass with ExitCode 100\n");
 
@@ -66,24 +86,22 @@ namespace LargeObjectTest
             {
                 loop++;
                 TestLibrary.Logging.Write(String.Format("LOOP: {0}\n", loop));
-                try
-                {
-                    largeobj = new LargeObject();
-                    TestLibrary.Logging.WriteLine("Allocated LargeObject");
-                }
-                catch (Exception e)
+
+                if (!AllocAndCollect(loop))
                 {
-                    TestLibrary.Logging.WriteLine("Failure to allocate at loop {0}\n", loop);
-                    TestLibrary.Logging.WriteLine("Caught Exception: {0}", e);
                     return 1;
                 }
-                largeobj = null;
+
                 GC.Collect();
                 GC.WaitForPendingFinalizers();
+                GC.Collect();
+
                 TestLibrary.Logging.WriteLine("LargeObject Collected\n");
             }
+
             TestLibrary.Logging.WriteLine("Test Passed");
             GC.Collect();
+            GC.WaitForPendingFinalizers();
 
             return ExitCode;
         }
index e354d89..8ba0fc8 100644 (file)
@@ -6,6 +6,7 @@
 //
 
 using System;
+using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 using System.Collections.Generic;
 
@@ -34,6 +35,7 @@ public class GCUtil
     }
 
 
+    [MethodImplAttribute(MethodImplOptions.NoInlining)]
     public static void FreePins()
     {
         foreach (GCHandle gch in list)
@@ -45,6 +47,7 @@ public class GCUtil
     }
 
 
+    [MethodImplAttribute(MethodImplOptions.NoInlining)]
     public static void FreeNonPins()
     {
         blist.Clear();
@@ -70,6 +73,7 @@ public class GCUtil
 
 
 
+    [MethodImplAttribute(MethodImplOptions.NoInlining)]
     public static void FreePins2()
     {
         foreach (GCHandle gch in list2)
@@ -82,6 +86,7 @@ public class GCUtil
 
 
 
+    [MethodImplAttribute(MethodImplOptions.NoInlining)]
     public static void FreeNonPins2()
     {
         blist2.Clear();
index 7b01916..78e81f4 100644 (file)
@@ -4,6 +4,7 @@
 
 
 using System;
+using System.Runtime.CompilerServices;
 
 
 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
@@ -75,7 +76,6 @@ namespace DoubLink
                 MakeLeak(iRep);
             }
 
-
             GC.Collect();
             GC.WaitForPendingFinalizers();
 
@@ -87,6 +87,7 @@ namespace DoubLink
             return (DLinkNode.FinalCount == iRep * iObj * 20);
         }
 
+        [MethodImplAttribute(MethodImplOptions.NoInlining)]
         public void SetLink(int iRep, int iObj)
         {
             for (int i = 0; i < iRep; i++)
@@ -96,6 +97,7 @@ namespace DoubLink
         }
 
 
+        [MethodImplAttribute(MethodImplOptions.NoInlining)]
         public void MakeLeak(int iRep)
         {
             for (int i = 0; i < iRep; i++)