ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Garbage collection in VBA (https://www.excelbanter.com/excel-programming/348300-garbage-collection-vba.html)

Edward Ulle

Garbage collection in VBA
 
I had read that its good practice to set instances of class to Nothing
to free up the memory.

Is there garbage collection in VBA?

Also is there any benefit to setting Application Object Model class
variables to Nothing upon exiting a procedure or function? For example

Dim myRange as Range

Set myRange = Something

Do some code

Set myRange = Nothing

Exit Function

Or is the memory allocated in the function returned to the heap when the
function goes out of scope?



*** Sent via Developersdex http://www.developersdex.com ***

Tom Ogilvy

Garbage collection in VBA
 
Or is the memory allocated in the function returned to the heap when the
function goes out of scope?

For local variables, Yes.

--
Regards,
Tom Ogilvy

"Edward Ulle" wrote in message
...
I had read that its good practice to set instances of class to Nothing
to free up the memory.

Is there garbage collection in VBA?

Also is there any benefit to setting Application Object Model class
variables to Nothing upon exiting a procedure or function? For example

Dim myRange as Range

Set myRange = Something

Do some code

Set myRange = Nothing

Exit Function

Or is the memory allocated in the function returned to the heap when the
function goes out of scope?



*** Sent via Developersdex http://www.developersdex.com ***




Bob Phillips[_6_]

Garbage collection in VBA
 
Read this NG post by Matthew Curland http://tinyurl.com/cq95p

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"Edward Ulle" wrote in message
...
I had read that its good practice to set instances of class to Nothing
to free up the memory.

Is there garbage collection in VBA?

Also is there any benefit to setting Application Object Model class
variables to Nothing upon exiting a procedure or function? For example

Dim myRange as Range

Set myRange = Something

Do some code

Set myRange = Nothing

Exit Function

Or is the memory allocated in the function returned to the heap when the
function goes out of scope?



*** Sent via Developersdex http://www.developersdex.com ***




Edward Ulle

Garbage collection in VBA
 
To both of you thanks.

I've been wasting my time and fingers typing Set all class variables to
Nothing at the end of all my procedures.

Good article Bob.



*** Sent via Developersdex http://www.developersdex.com ***

Bob Phillips[_6_]

Garbage collection in VBA
 
Edward,

Unfortunately, whilst Matthew is making a very good point (and he is a
respected MS developer), there are cases, automation comes to mind, where
not clearing down the objects is suspected of being the reason why Excel
sometimes fails to quit.

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"Edward Ulle" wrote in message
...
To both of you thanks.

I've been wasting my time and fingers typing Set all class variables to
Nothing at the end of all my procedures.

Good article Bob.



*** Sent via Developersdex http://www.developersdex.com ***




Edward Ulle

Garbage collection in VBA
 
Bob,

I understand there are circumstance that explicit releasing of resource
is required.

In my case, I have a macro that is constantly running. It displays a
modeless dialog box with numerous options. I have only a few of my own
classes. Generally I'm using the Excel Object Model classes. Each
option of the dialog box fires up another module to do some processing
of data. Once that module is finished the procedure is exited and
control returns to the main dialog box module. Almost alway the class
instances are local to the module and can be release. So my concern was
does multiple calls to the modules cause accumulation of memory locks or
is it released. As Tom pointed out, if they are local I don't have to
explicit release.

Thanks.

*** Sent via Developersdex http://www.developersdex.com ***


All times are GMT +1. The time now is 02:07 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com