Thursday, October 12, 2017
.NET Garbage Collector
.NET got quite sophisticated garbage collector operating over so-called managed heap, which is basically just a heap combining two more specific heaps intended for small and large objects. All objects on a heap belong to certain generation. The first generation (Generation 0) is small recently created objects with shortest life expectancy, second generation (Generation 1) is what objects of generation 0 become if they manage to survive garbage collection being younger generation and finally third generation (Generation 2) is a generation for either true survivors, meaning objects of younger generations that got promoted or just large objects (over 85kB).
If you need more flexibility you can actually control your Grabage Collector behavior through static class GC.
For example, you might want to collect some certain generation:
GC.Collect(0);
GC.Collect(1);
GC.Collect(2);
GC.Collect(); // This one is gonna collect all generations.
Or you can make GC enter so-called "no GC region latency mode" which allows you to execute certain parts of your code without GC intervention:
// Specify how much may be allocated without disturbing GC
GC.TryStartNoGCRegion(48);
// Your time-critical code
GC.EndNoGCRegion();
Keep in mind that TryStartNoGCRegion accepts size per heap.There are two heaps
(for small and large objects), so total size is whatever you pass in there
multiplied by 2. In the example above, total size would be 48 * 2 = 96.
Subscribe to:
Post Comments (Atom)
Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!
ReplyDeletenet software development