concept GC in category .net

appears as: GC, The GC
Concurrency in .NET

This is an excerpt from Manning's book Concurrency in .NET.

The implementations of memoization introduced in the previous code examples are a somewhat naive approach. The solution of storing data in a simple dictionary works, but it isn’t a long-term solution. A dictionary is unbounded; consequently, the items are never removed from memory but only added, which can, at some point, lead to memory leak issues. Solutions exist to all these problems. One option is to implement a memoize function that uses a WeakReference type to store the result values, which permits the results to be collected when the garbage collector (GC) runs. Since the introduction of the collection ConditionalWeakDictionary with the .NET 4.0 framework, this implementation is simple: a dictionary takes as a key a type instance that’s held as a weak reference. The associated values are kept as long as the key lives. When the key is reclaimed by the GC to be collocated, the reference to the data is removed, making it available for collection.

Weak references are a valuable mechanism for handling references to managed objects. The typical object reference (also known as a strong reference) has a deterministic behavior, where as long as you have a reference to the object, the GC won’t collect the object that consequently stays alive. But in certain scenarios, you want to keep an invisible string attached to an object without interfering with the GC’s ability to reclaim that object’s memory. If the GC reclaimed the memory, your string becomes unattached and you can detect this. If the GC hasn’t touched the object yet, you can pull the string and retrieve a strong reference to the object to use it again. This facility is useful for automatically managing a cache that can keep weak references to the least recently used objects without preventing them from being collected and inevitably optimizing the memory resources.

4.2.1 When the GC is the bottleneck: structs vs. class objects

The goal of the Mandelbrot example is to transform a sequential algorithm into a faster one. No doubt you’ve achieved a speed improvement; 9.038 to 3.443 seconds is a little more than three times faster on a quad-core machine. Is it possible to further optimize performance? The TPL scheduler is partitioning the image and assigning the work to different tasks automatically, so how can you improve the speed? In this case, the optimization involves reducing memory consumption, specifically by minimizing memory allocation to optimize garbage collection. When the GC runs, the execution of the program stops until the garbage collection operation completes.

In the Mandelbrot example, a new Complex object is created in each iteration to decide if the pixel coordinate belongs to the Mandelbrot set. The Complex object is a reference type, which means that new instances of this object are allocated on the heap. This piling of objects onto the heap results in memory overhead, which forces the GC to intervene to free space.