开发者

VBA Garbage Collector Details

开发者 https://www.devze.com 2023-01-23 10:43 出处:网络
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开发者_JAVA百科 VBA garbage collector works? The .Net GC is very well-d

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开发者_JAVA百科 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号