Managed Memory Model in the .NET Framework
- 2/18/2009
Key Points
The managed heap is segmented into Generations 0, 1, and 2 and the Large Object Heap.
The assumption of the memory model for the managed environment is that small objects are short lived, while large objects live longer. In addition, objects of like size are likely to communicate with each other.
Garbage collection in .NET is non-deterministic. You can force garbage collection with the GC.Collect method. However, this is not recommended.
GC.AddMemoryPressure and GC.RemoveMemoryPressure apply artificial pressure to the managed heap. This is useful to account for the difference in size between a managed wrapper and the native resource.
The Large Object Heap is collected, but not compacted, with a full garbage collection.
Finalizable objects implement a Finalize method. Disposable objects implement the IDisposable.Dispose method. Implement the dispose pattern to properly define a base and derive a class as disposable.
Weak references can be reclaimed at the discretion of the Garbage Collector.
References abstract moveable pointers. Pin the reference to fix the pointer, which can then be safely passed to native code.