+ Reply to Thread
Results 1 to 6 of 6

Garbage collection in VBA

  1. #1
    Edward Ulle
    Guest

    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 ***

  2. #2
    Tom Ogilvy
    Guest

    Re: 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" <[email protected]> wrote in message
    news:[email protected]...
    > 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 ***




  3. #3
    Bob Phillips
    Guest

    Re: 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" <[email protected]> wrote in message
    news:[email protected]...
    > 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 ***




  4. #4
    Edward Ulle
    Guest

    Re: 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 ***

  5. #5
    Bob Phillips
    Guest

    Re: 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" <[email protected]> wrote in message
    news:[email protected]...
    > 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 ***




  6. #6
    Edward Ulle
    Guest

    Re: 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 ***

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1