blob: d495d6abcc93417c007f1494a2780ece2dd0d5cc (
plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/* AddUsageTest
*
* Tests GC.AddMemoryPressure by passing a valid value (AddMemoryPressureTest.Pressure)
* and making sure the objects with added pressure get collected more times by
* the GC than those without pressure.
*/
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class Dummy
{
public Dummy(long pressure)
{
GC.AddMemoryPressure(pressure);
}
public Dummy()
{
}
}
public class AddUsageTest
{
public static int Pressure = 100000; // test will fail with values less than this
private int _numTests = 0;
private AddUsageTest()
{
}
public bool ThresholdTest()
{
_numTests++;
int gcCount1 = GC.CollectionCount(0);
for (int i = 0; i < 100; i++)
{
Dummy heavy = new Dummy(AddUsageTest.Pressure);
int gen = GC.GetGeneration(heavy);
if (gen != 0)
{
Console.WriteLine("Warning: newly-allocated dummy ended up in gen {0}", gen);
}
//GC.WaitForPendingFinalizers();
}
gcCount1 = GC.CollectionCount(0) - gcCount1;
int gcCount2 = GC.CollectionCount(0);
for (int i = 0; i < 100; i++)
{
Dummy light = new Dummy(AddUsageTest.Pressure);
int gen = GC.GetGeneration(light);
if (gen != 0)
{
Console.WriteLine("Warning: newly-allocated dummy ended up in gen {0}", gen);
}
//GC.WaitForPendingFinalizers();
}
gcCount2 = GC.CollectionCount(0) - gcCount2;
Console.WriteLine("{0} {1}", gcCount1, gcCount2);
if (gcCount1 > gcCount2)
{
Console.WriteLine("ThresholdTest Passed");
Console.WriteLine();
return true;
}
Console.WriteLine("ThresholdTest Failed");
Console.WriteLine();
return false;
}
public bool RunTest()
{
int numPass = 0;
if (ThresholdTest())
numPass++;
return (numPass == _numTests);
}
public static int Main()
{
AddUsageTest test = new AddUsageTest();
if (test.RunTest())
{
Console.WriteLine("Test Passed");
return 100;
}
Console.WriteLine("Test Failed");
return 1;
}
}
|