-
Notifications
You must be signed in to change notification settings - Fork 8
/
GarbageCollectionSample.cs
154 lines (130 loc) · 5.58 KB
/
GarbageCollectionSample.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Threading;
/// <summary>
/// System.Runtime.GCLatencyMode.SustainedLowLatency
/// https://docs.microsoft.com/en-us/dotnet/api/system.runtime.gclatencymode?view=netcore-3.1
///
/// Batch Disables garbage collection concurrency and reclaims
/// objects in a batch call. This is the most intrusive mode.
/// This mode is designed for maximum throughput at the
/// expense of responsiveness.
///
/// Interactive Enables garbage collection concurrency and reclaims
/// objects while the application is running. This is
/// the default mode for garbage collection on a
/// workstation and is less intrusive than Batch.
/// It balances responsiveness with throughput. This
/// mode is equivalent to garbage collection on a
/// workstation that is concurrent.
///
/// LowLatency Enables garbage collection that is more conservative
/// in reclaiming objects. Full collections occur only
/// if the system is under memory pressure, whereas
/// generation 0 and generation 1 collections might occur
/// more frequently. This mode is not available for the
/// server garbage collector.
///
/// NoGCRegion Indicates that garbage collection is suspended while
/// the app is executing a critical path.
/// NoGCRegion is a read-only value; that is, you cannot
/// assign the NoGCRegion value to the LatencyMode property.
/// You specify the no GC region latency mode by calling the
/// TryStartNoGCRegion method and terminate it by calling the
/// EndNoGCRegion() method.
///
/// SustainedLowLatency Enables garbage collection that tries to minimize latency
/// over an extended period. The collector tries to perform
/// only generation 0, generation 1, and concurrent
/// generation 2 collections. Full blocking collections may
/// still occur if the system is under memory pressure.
/// </summary>
namespace CodeSamples.Useful
{
internal class GarbageCollectionInfo
{
public int MaxGeneration { get; private set; }
public int Generation0 { get; private set; }
public int Generation1 { get; private set; }
public int Generation2 { get; private set; }
public long TotalMemory { get; private set; }
public GarbageCollectionInfo()
{
MaxGeneration = GC.MaxGeneration;
Generation0 = GC.CollectionCount(0);
Generation1 = GC.CollectionCount(1);
Generation2 = GC.CollectionCount(2);
TotalMemory = GC.GetTotalMemory(false);
}
public override string ToString()
{
return $"GC Info: MaxGen={MaxGeneration}, Gen0={Generation0}, Gen1={Generation1}, Gen2={Generation2}, Total Memory={TotalMemory}";
}
}
internal class GarbageCollection
{
#pragma warning disable S1481
private void MakeSomeGarbage()
{
Console.WriteLine("Making Garbage...");
for (int i = 0; i < 5000; i++)
{
var version = new Version();
}
}
#pragma warning restore S1481
#pragma warning disable S1215
private void ForceGarbageCollection()
{
var gc = new GarbageCollection();
var gcBefore = new GarbageCollectionInfo();
Console.WriteLine($"Before Garbage Collection: {gcBefore}");
gc.MakeSomeGarbage();
var gcAfterGeneratingGarbage = new GarbageCollectionInfo();
Console.WriteLine($"After Generating Garbage: {gcAfterGeneratingGarbage}");
GC.Collect(0);
var gcAfterCleanupGen0 = new GarbageCollectionInfo();
Console.WriteLine($"After Garbage Collection of Gen0: {gcAfterCleanupGen0}");
GC.Collect(1);
var gcAfterCleanupGen1 = new GarbageCollectionInfo();
Console.WriteLine($"After Garbage Collection of Gen1: {gcAfterCleanupGen1}");
GC.WaitForPendingFinalizers();
GC.Collect(2);
var gcAfterCleanupGen2 = new GarbageCollectionInfo();
Console.WriteLine($"After Garbage Collection of Gen2: {gcAfterCleanupGen2}");
GC.WaitForPendingFinalizers();
GC.Collect();
var gcAfterCleanup = new GarbageCollectionInfo();
Console.WriteLine($"After Garbage Collection: {gcAfterCleanup}");
}
#pragma warning restore S1215
private void TurnGarbageCollectionOff()
{
Console.Write("Turning Garbage Collection into low latency (= off)...");
System.Runtime.GCSettings.LatencyMode = System.Runtime.GCLatencyMode.SustainedLowLatency;
Console.WriteLine("done!");
}
private void TurnGarbageCollectionOn()
{
Console.Write("Turning Garbage Collection into interactive (= on)...");
System.Runtime.GCSettings.LatencyMode = System.Runtime.GCLatencyMode.Interactive;
Console.WriteLine("done!");
}
public void Go()
{
TurnGarbageCollectionOff();
Thread.Sleep(1000);
TurnGarbageCollectionOn();
ForceGarbageCollection();
}
}
public class GarbageCollectionSample : SampleExecute
{
public override void Execute()
{
Title("GarbageCollectionSample");
var garbageCollection = new GarbageCollection();
garbageCollection.Go();
Finish();
}
}
}