SparklySpartan
New member
- Local time
- Today, 01:36
- Joined
- Feb 25, 2025
- Messages
- 13
Hi all,
I was wondering if I could get some of your opinions on whether explicit memory management in VBA code is a good practice / necessary in modern day VBA.
Here's my current understanding, please correct me where I'm wrong. When an object, such as a recordset, goes out of scope, that is to say, the context in which it's being used ends, assuming a reference to that object wasn't returned somewhere else to be used further, VBA's garbage collection will automatically de-allocate the memory that was being used by that object and give it back to the operating system, preventing a memory leak from occurring.
I don't know when exactly this happens, whether it's right when the reference to the object goes out of scope or later, so maybe, explicitly calling Recordset.Close and setting the reference to nothing could be advantageous. But if it turns out the memory always gets de-allocated automatically, right away, anyways, I would consider these two lines of code redundant.
The question of when garbage collection occurs is my main curiosity, and it feels like the deciding factor of whether or not this is a good practice. I've been writing code that has not been explicitly doing these 1-2 steps for every object and haven't noticed any issues, but if it turns out it does matter, I'll go ahead and start doing so.
Thanks in advance for your responses.
I was wondering if I could get some of your opinions on whether explicit memory management in VBA code is a good practice / necessary in modern day VBA.
Here's my current understanding, please correct me where I'm wrong. When an object, such as a recordset, goes out of scope, that is to say, the context in which it's being used ends, assuming a reference to that object wasn't returned somewhere else to be used further, VBA's garbage collection will automatically de-allocate the memory that was being used by that object and give it back to the operating system, preventing a memory leak from occurring.
I don't know when exactly this happens, whether it's right when the reference to the object goes out of scope or later, so maybe, explicitly calling Recordset.Close and setting the reference to nothing could be advantageous. But if it turns out the memory always gets de-allocated automatically, right away, anyways, I would consider these two lines of code redundant.
The question of when garbage collection occurs is my main curiosity, and it feels like the deciding factor of whether or not this is a good practice. I've been writing code that has not been explicitly doing these 1-2 steps for every object and haven't noticed any issues, but if it turns out it does matter, I'll go ahead and start doing so.
Thanks in advance for your responses.