Wiki Home

VBDotNet Garbage Collection

Namespace: DotNet
To describe the Garbage Collection without swearing is possible, but to describe the effects it has on compatibility with VB4(32), VB5, VB6 is virtually impossible without swearing.

The GC is part of the CLR's memory management system. Values types are typically stored on the stack (push/pop) but objects are stored on the heap. The GC is responsible for allocating and de-allocating memory needed to store objects. There are two heaps used, a generational compacting heap and a large object heap which isn't compacted.

Objects managed by the GC can have a finalizer called before they are collected, although the inclusion of a finalizer does have some cost in performance. The GC scans root level entries and then creates an object graph. Objects that can no longer be reached have their finalizer called on them, then in the next cycle they are marked as free space, The heap is also compacted to avoid memory fragmentation.

Finalizers can be called in any order. For example, if your program, A, referenced object B, which in turn referenced object C, (A-->B-->C), then if A releases B, both B and C can be collected by the GC. However, finalization can occur in B before C, or in C before B. The GC gives no guarantee as to the order.

The other problem with the GC, is that finalization may occur sometime after the object is released. There have been many discussions on this issue. The lack of determinalistic finalization.

A major difficulty in overcoming these two limitations is the need to do some form of reference counting. Unfortunately, the current stack management model used by the CLR, does not allow the firing of destructors, hence it becomes difficult to implement any automatic reference counting.

For info on MS's POV on this see: Brian Harry's posting to the dotnet forum at DISCUSS.DEVELOP.COM.

For a general overview see the MSDN magazine November issue 's article on Garbage Collection

Question: is Garbage Collecting coming from the CLR? So that all languages that share the CLR will share the same GC? -- Nancy Folsom

Answers self...apparently so.
For a discussion of the subject, look here.
Category DotNet Category VBDotNet
( Topic last updated: 2000.10.18 01:32:04 AM )