Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions tests/src/GC/API/GC/Finalize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Tests Finalize() and WaitForPendingFinalizers()

using System;
using System.Runtime.CompilerServices;

public class Test
{
Expand All @@ -28,12 +29,10 @@ public CreateObj()
obj = new Dummy();
}

[MethodImplAttribute(MethodImplOptions.NoInlining)]
public void RunTest()
{
obj = null;
GC.Collect();

GC.WaitForPendingFinalizers(); // makes sure Finalize() is called.
}
}

Expand All @@ -42,6 +41,9 @@ public static int Main()
CreateObj temp = new CreateObj();
temp.RunTest();

GC.Collect();
GC.WaitForPendingFinalizers(); // makes sure Finalize() is called.
GC.Collect();

if (visited)
{
Expand Down
37 changes: 23 additions & 14 deletions tests/src/GC/API/GC/GetTotalMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,35 @@

namespace DefaultNamespace {
using System;
using System.Runtime.CompilerServices;

internal class GetTotalMemory
{
// margin of error, since GetTotalMemory is an approximation
// 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 ...");
Expand All @@ -38,35 +57,25 @@ public static int Main(String [] args )
// 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!" );
Expand Down
3 changes: 3 additions & 0 deletions tests/src/GC/API/GC/KeepAlive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

using System;
using System.Runtime.CompilerServices;

public class Test
{
Expand Down Expand Up @@ -47,6 +48,7 @@ public class Dummy2
}


[MethodImplAttribute(MethodImplOptions.NoInlining)]
public static void RunTest2()
{
Dummy2 obj2 = new Dummy2();
Expand All @@ -66,6 +68,7 @@ public static void RunTest()
//for (int i=0; i<5; i++) {
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
//}

GC.KeepAlive(obj); // will keep obj alive until this point
Expand Down
11 changes: 10 additions & 1 deletion tests/src/GC/API/GC/KeepAliveNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Tests KeepAlive()

using System;
using System.Runtime.CompilerServices;

public class Test
{
Expand All @@ -27,11 +28,19 @@ public CreateObj()
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
}
Expand Down
1 change: 1 addition & 0 deletions tests/src/GC/API/GC/KeepAliveRecur.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static void foo(Object o)

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

foo(o); //Recursive call

Expand Down
16 changes: 11 additions & 5 deletions tests/src/GC/API/GC/SuppressFinalize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Tests SuppressFinalize()

using System;
using System.Runtime.CompilerServices;

public class Test {

Expand All @@ -17,16 +18,21 @@ public class Dummy {
}
}

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!");
Expand Down
11 changes: 8 additions & 3 deletions tests/src/GC/API/GCHandle/HandleCopy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// Also tests the target of the handle.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

public class Test
Expand Down Expand Up @@ -37,13 +38,17 @@ public CreateObj()
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;
Expand Down
15 changes: 11 additions & 4 deletions tests/src/GC/API/GCHandle/Normal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// should not be collected.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

public class Test {
Expand All @@ -19,21 +20,27 @@ public class Dummy {
}
}

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;
}
Expand Down
23 changes: 9 additions & 14 deletions tests/src/GC/API/GCHandle/Weak.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// will be collected.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

public class Test
Expand All @@ -31,30 +32,24 @@ public CreateObj()
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;
Expand Down
8 changes: 7 additions & 1 deletion tests/src/GC/API/GCHandleCollector/Usage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// the class that holds the HandleCollectors
Expand Down Expand Up @@ -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()
{
Expand All @@ -56,14 +58,16 @@ private bool Case1()
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
Expand Down Expand Up @@ -96,7 +100,9 @@ private bool Case2()
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
Expand Down
Loading