summaryrefslogtreecommitdiff
path: root/tests/src/GC/Performance/Tests/LOHSmooth.cs
blob: e421394d536f1fbb28c5794d3a3ef7604f498785 (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
// 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.

using System;
using System.Threading;
using System.Diagnostics;

public class VSTSTest
{
    static int _threadCount = 16;
    static int _minSize = 200 * 1024;
    static int _maxSize = 250 * 1024;
    static int _holdSize = 100;
    static int _maxCount = 100;
    static int _count = 0;
    static bool _done = false;
    static long _maxPrivate = 0;
    static object lockObject = new Object();
        
    // each worker will hold *in the worst case scenario* 100 * 250k * 2 = 50MB
    // all 16 threads will hold in the worst case scenario 800MB
    static void Worker(object ctx)
    {
        string[] s = new string[_holdSize];
        Random rnd = new Random();
        for (int i = 0; i < _holdSize; i++)
        {
            Thread.Sleep(rnd.Next(50, 100));
            s[i] = new string('x', rnd.Next(_minSize, _maxSize));
     
	    long _private = Process.GetCurrentProcess().PrivateMemorySize64;
		
	    lock (lockObject)
	    {
		if (_private > _maxPrivate)
		    _maxPrivate = _private;
	    }
        }
        
	if (_count++ >= _maxCount)
		_done = true;
	
	if (!_done)
		ThreadPool.QueueUserWorkItem(Worker);
    }

    static public void Allocate()
    {
        for (int i = 0; i < _threadCount; i++)
        {
            ThreadPool.QueueUserWorkItem(Worker);
        }
    }

    public static void Main(string[] args)
    {		    
        Allocate();    
        while (!_done)
        {
            Thread.Sleep(0);
        }
    }
}