Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Garbage Collector Details

Tags:

vba

vb6

I've found myself having to write some VBA code recently and just wondered if anyone had ever come across any details on how the VBA garbage collector works? The .Net GC is very well-documented indeed but I can't find a single shred of detail on the VBA GC, other than that vague mentions that it's a reference counter. I assume that it's pretty similar to the VB6 GC but can't find any information on that either.

Specifically, I'd be interested in knowing:

  • What triggers a GC
  • What algorithm it uses (is collection generational, for example?)
  • How (if at all) does it handle circular references?
  • Is there any way of monitoring its operation

This is more out of curiosity than any particular need to know, any insight at all much appreciated!

like image 845
Jon Artus Avatar asked Nov 05 '10 13:11

Jon Artus


People also ask

Does VBA do garbage collection?

VBA/Excel does not have garbage collection, like old VB. Instead of GC, it uses reference counting. Memory is freed when you set a pointer to nothing (or when variable goes out of scope). Like in old VB it means that circular references are never freed.

Does garbage collector allocate memory?

In the common language runtime (CLR), the garbage collector (GC) serves as an automatic memory manager. The garbage collector manages the allocation and release of memory for an application.

What is garbage collector count?

Reference counting garbage collection is where each object has a count of the number of references to it. Garbage is identified by having a reference count of zero. An object's reference count is incremented when a reference to it is created, and decremented when a reference is destroyed.

How many types of generations are there in a garbage collector?

Short-lived objects are stored in the first generation, generation 0. The longer-lived objects are pushed into the higher generations, 1 or 2. The garbage collector works more frequently in the lower generations than in the higher ones. When an object is first created, it is put into generation 0.


1 Answers

The following assumes that VBA is still using the same garbage collection mechanism used in VB6 (which it very probably does).

VB6 used a reference-counting GC. The GC is triggered deterministically when the last reference to a given object is set to Nothing. Setting local references to Nothing is unnecessary, this happens as they go out of scope.

Every object implements a COM interface that takes care of the reference count for that object. Each assignment of an object reference updates the reference counters of the involved references (i.e. the counter of old object that was previously referenced gets decremented, and the new object’s counter is incremented). An object is garbage collected when its reference counter reaches 0.

Objects in circular references are thus never collected during the lifetime of a VBA application. What’s more, VBA doesn’t offer a way to break circular references. In VB6, weak references could be implemented via WinAPI functions.

like image 192
Konrad Rudolph Avatar answered Sep 20 '22 01:09

Konrad Rudolph