-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathTestGC.cs
More file actions
70 lines (56 loc) · 2.06 KB
/
Copy pathTestGC.cs
File metadata and controls
70 lines (56 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using nanoFramework.TestFramework;
namespace NFUnitTestGC
{
[TestClass]
public class TestGC
{
[TestMethod]
public void TestGCStress()
{
int maxArraySize = 1024 * 32;
object[] arrays = new object[600];
for (int loop = 0; loop < 100; loop++)
{
OutputHelper.WriteLine($"Running iteration {loop}");
for (int i = 0; i < arrays.Length - 1;)
{
OutputHelper.WriteLine($"Alloc array of {maxArraySize} bytes @ pos {i}");
arrays[i++] = new byte[maxArraySize];
OutputHelper.WriteLine($"Alloc array of 64 bytes @ pos {i}");
arrays[i++] = new byte[64];
}
arrays[0] = new byte[maxArraySize];
for (int i = 0; i < arrays.Length; i++)
{
arrays[i] = null;
}
}
}
[TestMethod]
public void TestGetTotalMemory()
{
// create several objects
object[] objects = new object[100];
for (int i = 0; i < objects.Length; i++)
{
objects[i] = new object();
}
// get total memory
long totalMemory = GC.GetTotalMemory(false);
OutputHelper.WriteLine($"Total memory: {totalMemory} bytes");
// release objects
for (int i = 0; i < objects.Length; i++)
{
objects[i] = null;
}
// get total memory, forcing full collection
long totalMemoryAfterCollection = GC.GetTotalMemory(true);
OutputHelper.WriteLine($"Total memory: {totalMemoryAfterCollection} bytes");
// check if memory was released
Assert.IsTrue(totalMemory > totalMemoryAfterCollection, "Memory was not released");
}
}
}