Freeing Dynamic Arrays

Discussion in 'ASP General' started by headware, May 14, 2008.

  1. headware

    headware Guest

    Do you have to manually release memory allocated by creating a dynamic
    array using ReDim? In other words, if I have the following code:

    ReDim Test(1000)
    For i = 0 To 1000
    Test(i) = "test value " & i
    Next

    Do have I have set Test = Nothing to prevent a memory leak?

    Thanks,
    Dave
    headware, May 14, 2008
    #1
    1. Advertising

  2. headware wrote:
    > Do you have to manually release memory allocated by creating a dynamic
    > array using ReDim? In other words, if I have the following code:
    >
    > ReDim Test(1000)
    > For i = 0 To 1000
    > Test(i) = "test value " & i
    > Next
    >
    > Do have I have set Test = Nothing to prevent a memory leak?
    >


    No. You did not create Test using the Set keyword did you? That means it
    is not considered an object and there is no need to treat it as one. If
    you wish to be explicit, use

    Erase Test

    By the way, you seem to have gotten the idea that failing to set any
    object to nothing will invariably lead to a memory leak, and this is
    just not the case. With most objects, there is no problem with allowing
    the vbscript garbage handler to take care of them when they go out of
    scope. Where memory leaks may occur is with objects that are in a state
    in which they cannot be immediately de-referenced by the garbage handler
    and thus remain in memory. The most well-known culprits for this are ADO
    objects that may be unable to be closed before being set to nothing,
    perhaps because they may be getting handled in the wrong order: parent
    objects before their child objects. It is a good idea to be explicit
    with ADO objects.

    You may be interested in this bit from the guy at Microsoft who was
    responsible for developing a lot of the scripting runtime library:
    http://blogs.msdn.com/ericlippert/archive/2004/04/28/122259.aspx


    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], May 14, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. countofquad
    Replies:
    4
    Views:
    367
    countofquad
    Mar 5, 2005
  2. headware

    Freeing Dynamic Arrays

    headware, May 14, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    278
    Peter Bromberg [C# MVP]
    May 14, 2008
  3. Freeing Reallocated arrays

    , Aug 19, 2008, in forum: C Programming
    Replies:
    1
    Views:
    308
    Ian Collins
    Aug 19, 2008
  4. Philipp
    Replies:
    21
    Views:
    1,110
    Philipp
    Jan 20, 2009
  5. headware

    Freeing Dynamic Arrays

    headware, May 12, 2008, in forum: ASP General
    Replies:
    2
    Views:
    97
    headware
    May 14, 2008
Loading...

Share This Page