-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
plug.cs
85 lines (72 loc) · 2.1 KB
/
plug.cs
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
// 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 System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Security;
public class Test
{
public static void Usage()
{
Console.WriteLine("USAGE:");
Console.WriteLine("plug.exe [numIterations]");
}
[SecuritySafeCritical]
public static int Main(string[] args)
{
int size = 10000;
int power = 20;
int numIterations = 0;
GCHandle[] list = new GCHandle[size];
if (args.Length == 0)
{
//using defaults
numIterations = 100;
}
else if (args.Length == 1)
{
if (!Int32.TryParse(args[0], out numIterations))
{
Usage();
return 1;
}
}
else
{
Usage();
return 1;
}
Console.WriteLine("Running {0} iterations", numIterations);
for (int j=0; j<numIterations; j++)
{
for (int i=0; i<size; i++)
{
GCHandleType type = GCHandleType.Normal;
if (i%5==0)
{
// pin every 5th handle
type = GCHandleType.Pinned;
}
if (!list[i].IsAllocated)
{
try
{
byte[] b = new byte[(int)Math.Pow(2,(i%power))];
list[i] = (GCHandle.Alloc(b, type));
}
catch (OutOfMemoryException)
{
Console.WriteLine("OOM");
Console.WriteLine("Heap size: {0}", GC.GetTotalMemory(false));
Console.WriteLine("Trying to allocate array of size: {0}", Math.Pow(2,(i%power)));
}
}
else
{
list[i].Free();
}
}
}
return 100;
}
}