summaryrefslogtreecommitdiff
path: root/tests/src/GC/Stress/Tests/573277.cs
blob: 185217d71742d15692058df4e34a40ffe228da05 (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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System;
using System.Security;
using System.Collections.Generic;
using System.Threading;

#if WINCORESYS
[assembly:AllowPartiallyTrustedCallers]
#endif

public class Test
{
    public static bool fail = false;

    [System.Security.SecuritySafeCritical]
    public static int Main(String[] args)
    {
        Thread[] threads = new Thread[Math.Max(Environment.ProcessorCount * 2, 64)];
        for (int i = 0; i < threads.Length; i++)
        {
            threads[i] = new Thread(new ThreadStart(Allocate));
            threads[i].Name = i.ToString();
            threads[i].Start();
        }

        for (int i = 0; i < threads.Length; i++)
        {
            threads[i].Join();
        }

        if (fail)
        {
            Console.WriteLine("Test Failed");
            return 0;
        }

        Console.WriteLine("Test Passed");
        return 100;
    }

    public static void Allocate()
    {
        try
        {
            List<byte[]> list = new List<byte[]>();
            for (int i = 0; i < 10000; i++)
            {
                if (fail)
                {
                    break;
                }

                byte[] b = new byte[8000];
                if (i % 10 == 0)
                {
                    list.Add(b);
                }
            }
        }
        catch (OutOfMemoryException)
        {
            Console.WriteLine("OOM");
            fail = true;
        }
    }
}