GcMemoryAllocationTest

Форк
0
107 строк · 2.7 Кб
1
PrintHelp();
2

3
List<string> list1 = new List<string>();
4
List<string> list2 = new List<string>();
5
List<string> list3 = new List<string>();
6
List<string> list4 = new List<string>();
7
CancellationTokenSource cts = new CancellationTokenSource();
8

9
void AddStringsToList(List<string> list)
10
{
11
    while (true)
12
    {
13
        if (cts.Token.IsCancellationRequested)
14
            break;
15
        
16
        list.Add($"String added at {DateTime.Now}");
17
    }
18
}
19

20
void ClearList(List<string> list)
21
{
22
    list.Clear();
23
    list.TrimExcess();
24
}
25

26
void PrintHelp()
27
{
28
    Console.WriteLine("=== Application Help ===");
29
    Console.WriteLine("Available commands:");
30
    Console.WriteLine("start    - Start adding strings to the lists.");
31
    Console.WriteLine("stop     - Stop adding strings to the lists and exit the application.");
32
    Console.WriteLine("continue - The tasks are already running.");
33
    Console.WriteLine("clear    - Clear all the lists.");
34
    Console.WriteLine("gc       - Call the garbage collector.");
35
    Console.WriteLine("help     - Display this help message.");
36
    Console.WriteLine("========================");
37
}
38

39
var isStarted = false;
40
var tasks = new List<Task>();
41

42
while (true)
43
{
44
    string? input = Console.ReadLine();
45

46
    if (input == "start")
47
    {
48
        if (isStarted)
49
        {
50
            Console.WriteLine("The tasks are already running.");
51
            continue;
52
        }
53

54
        cts = new CancellationTokenSource();
55
        tasks.Add(Task.Run(() => AddStringsToList(list1), cts.Token));
56
        tasks.Add(Task.Run(() => AddStringsToList(list2), cts.Token));
57
        tasks.Add(Task.Run(() => AddStringsToList(list3), cts.Token));
58
        tasks.Add(Task.Run(() => AddStringsToList(list4), cts.Token));
59

60
        isStarted = true;
61
        Console.WriteLine("Tasks started.");
62
    }
63
    else if (input == "stop")
64
    {
65
        if (!isStarted)
66
        {
67
            Console.WriteLine("The tasks are not running.");
68
            continue;
69
        }
70

71
        cts.Cancel();
72
        await Task.WhenAll(tasks);
73
        isStarted = false;
74
        Console.WriteLine("Tasks stopped.");
75
    }
76
    else if (input == "clear")
77
    {
78
        if (isStarted)
79
        {
80
            Console.WriteLine("Stop tasks before clearing.");
81
            continue;
82
        }
83
        
84
        ClearList(list1);
85
        ClearList(list2);
86
        ClearList(list3);
87
        ClearList(list4);
88
        
89
        Console.WriteLine("Lists cleared.");
90
    }
91
    else if (input == "gc")
92
    {
93
        GC.Collect();
94
        GC.WaitForPendingFinalizers();
95
        Console.WriteLine("Garbage collector called.");
96
    }
97
    else if (input == "exit")
98
    {
99
        break;
100
    }
101
    else if (input == "help")
102
    {
103
        PrintHelp();
104
    }
105
}
106

107
Console.WriteLine("Testing has been finished. Press any key to exit ...");
108

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.