Release all object variables from Memory

  • Is there a way to release all object variables in a subroutine from the memory. For example: Lets say I have 50 object variables and then at the end of the routine I want to release them all. Is there a way to do that without explicitly setting all the object variables to nothing. Below is an example of the method I currently use.



    Thanks for your help!


    Ryan

  • Re: Release all object variables from Memory


    I can't think of a way other than using an array of objects then cycling through them with a loop.


    But doing it that way you would lose names for your objects which I imagine could be awkward when writing code.

    Boo!:yikes:

  • Re: Release all object variables from Memory


    Thanks for your suggestion. I was hoping that there would be some command to release them, but I assumed that there would not be. Another question on the same topic. Is it only effective to release Objects from memory? If I delcare:

    Code
    Dim Vname as Long


    Do I need to release "Vname" from memory? Is it released when the routine is over? Thanks for your help!


    Ryan

  • Re: Release all object variables from Memory


    It depends on where you put your dim statement. Put it inside your sub so it will be release once your macro goes out of the sub.

  • Re: Release all object variables from Memory


    In Visual Basic / VBA the memorys are dropped atteh end of procedure they are whats know as dead, but are still in momory as public avalible memory spaces and can / will be over written next time around. Taking into account Windows / Excel memory managemenst are a little bit differently from most other applications ie excel manages a memory allocation given by windows and windows passes over controil to excel only stepping in at times of crisis, it is good practive to manually code kill the variables as you have outlines


    ie
    use VBNullstring, “” amd NOTHING to clear these so they do not stay in memory at all.


    It is good programming to declair variables in side their procedure and if needs be inside more if they are in more than one prodecure, CONST are very good but cautions I would only really use strings as constants and only if they are called many times, many programmes enjoy setting many const to save code lines, can be good but not always. see how it pains out.


    I use valKillA_xx for each procedure to take care of this ie..




  • Re: Release all object variables from Memory


    Thanks for your reply. However, I am a little confused as I have much to learn. I think what you are saying is that all variables, (string, long, integer, object, etc.) regardless of the type, should be released using code. Although VBA seems to have released the variable and it is not accessible to the user, the variable is still taking up memory unless it released using: Nothing, vbNullString. . . I assume that I need to use vbNullString to release memory when my variable is anything but an object. For objects I need to use Nothing. I have a feeling that I am incorrect so please correct my errors.


    The code you wrote: I do not understand why having a seperate macro to set the variables to vbNullString is better than doing within the macro that is executed. Is this in case you have several different macros with variables using the same name?


    Thanks a lot for your help!


    Ryan

  • Re: Release all object variables from Memory


    Ryan


    You don't really need to do this especially with variables like strin, integer etc.


    The memory the variables used will be available, but it will still contain the information for the variable.


    The only time you really need it is when you are working with other applications.


    For example say you have created a new instance of Access and run your code on it.


    Unless you set that object to Nothing the Access instance will still exist.

    Boo!:yikes:

  • Re: Release all object variables from Memory


    i have soloved this before myexcel.quit use

    Code
    MyExcel.DisplayAlerts = False
                MyExcel.SaveWorkspace()

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!