|
| 1 | +using System; |
| 2 | +using System.Threading; |
| 3 | + |
| 4 | +/// <summary> |
| 5 | +/// System.Runtime.GCLatencyMode.SustainedLowLatency |
| 6 | +/// https://docs.microsoft.com/en-us/dotnet/api/system.runtime.gclatencymode?view=netcore-3.1 |
| 7 | +/// |
| 8 | +/// Batch Disables garbage collection concurrency and reclaims |
| 9 | +/// objects in a batch call. This is the most intrusive mode. |
| 10 | +/// This mode is designed for maximum throughput at the |
| 11 | +/// expense of responsiveness. |
| 12 | +/// |
| 13 | +/// Interactive Enables garbage collection concurrency and reclaims |
| 14 | +/// objects while the application is running. This is |
| 15 | +/// the default mode for garbage collection on a |
| 16 | +/// workstation and is less intrusive than Batch. |
| 17 | +/// It balances responsiveness with throughput. This |
| 18 | +/// mode is equivalent to garbage collection on a |
| 19 | +/// workstation that is concurrent. |
| 20 | +/// |
| 21 | +/// LowLatency Enables garbage collection that is more conservative |
| 22 | +/// in reclaiming objects. Full collections occur only |
| 23 | +/// if the system is under memory pressure, whereas |
| 24 | +/// generation 0 and generation 1 collections might occur |
| 25 | +/// more frequently. This mode is not available for the |
| 26 | +/// server garbage collector. |
| 27 | +/// |
| 28 | +/// NoGCRegion Indicates that garbage collection is suspended while |
| 29 | +/// the app is executing a critical path. |
| 30 | +/// NoGCRegion is a read-only value; that is, you cannot |
| 31 | +/// assign the NoGCRegion value to the LatencyMode property. |
| 32 | +/// You specify the no GC region latency mode by calling the |
| 33 | +/// TryStartNoGCRegion method and terminate it by calling the |
| 34 | +/// EndNoGCRegion() method. |
| 35 | +/// |
| 36 | +/// SustainedLowLatency Enables garbage collection that tries to minimize latency |
| 37 | +/// over an extended period. The collector tries to perform |
| 38 | +/// only generation 0, generation 1, and concurrent |
| 39 | +/// generation 2 collections. Full blocking collections may |
| 40 | +/// still occur if the system is under memory pressure. |
| 41 | +/// </summary> |
| 42 | + |
| 43 | +namespace CodeSamples.Useful |
| 44 | +{ |
| 45 | + public class GarbageCollectionSample : SampleExecute |
| 46 | + { |
| 47 | + private void TurnGarbageCollectionOff() |
| 48 | + { |
| 49 | + Console.Write("Turning Garbage Collection into low latency (= off)..."); |
| 50 | + System.Runtime.GCSettings.LatencyMode = System.Runtime.GCLatencyMode.SustainedLowLatency; |
| 51 | + Console.WriteLine("done!"); |
| 52 | + } |
| 53 | + |
| 54 | + private void TurnGarbageCollectionOn() |
| 55 | + { |
| 56 | + Console.Write("Turning Garbage Collection into interactive (= on)..."); |
| 57 | + System.Runtime.GCSettings.LatencyMode = System.Runtime.GCLatencyMode.Interactive; |
| 58 | + Console.WriteLine("done!"); |
| 59 | + } |
| 60 | + |
| 61 | + public override void Execute() |
| 62 | + { |
| 63 | + Title("GarbageCollectionSample"); |
| 64 | + TurnGarbageCollectionOff(); |
| 65 | + Thread.Sleep(1000); |
| 66 | + TurnGarbageCollectionOn(); |
| 67 | + |
| 68 | + Finish(); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments