Understanding .NET Garbage Collection

Aus Wiki-WebPerfect
Wechseln zu: Navigation, Suche

Every program needs memory. Unfortunately, memory is finite.

Software must cope with memory usage, and there are two ways to manage it: manually and automatically. Manual memory management is prone to errors, especially with exceptions and asynchronous code. This is why modern managed environments (.NET, Erlang, and many more) implement automatic memory management with garbage collection.

Figure 1


Figure 2

No object or application root in Figure 2 has a reference to Object D or Object H. In the garbage collection process, the garbage collector discards both of these objects then compacts the heap.

There is a potential issue with this scenario. Assuming Object E occupies a large amount of memory, e.g. 300 MB, the performance will be suboptimal when the garbage collector moves Object E to Object D’s memory location.

In .NET objects are divided into two groups:

  • Large Objects – larger than 85KB or multidimensional arrays
  • Small Objects – everything else





Source: https://www.telerik.com/blogs/understanding-net-garbage-collection