Erase vs ReDim In Arrays

16,268

Solution 1

When in doubt, go with the semantics:

  • Use ReDim to re-dimension an array
  • Use Erase to deallocate an array

Sticking to that will make your code benefit from greater consistency and will make your code's intent clearer to the reader/maintainer/future-you.

Solution 2

Since you asked about the consequences of Redim vs Erase specifically in regards to memory... The only practical difference is with dynamic arrays.

If you erase a non-dynamic array it sets the values in it to 0s, zero length strings, or "empty" for variants. eg: Dim Array(10)

If you erase a dynamic array it completely frees the memory. eg: Dim Array()

Neither option should result in a memory leak. Both should cause memory to free up in a way that doesn't constantly expand the memory used if you loop through the array and redim/erase array parts of your code. If you are running into a memory leak looping through code, that might be a better question to ask about.

From the help on erase:

Dim NumArray(10) As Integer ' Integer array.
Erase NumArray ' Each element set to 0.

Dim StrVarArray(10) As String ' Variable-string array.
Erase StrVarArray ' Each element set to zero-length string ("").    

Dim StrFixArray(10) As String * 10 ' Fixed-string array.
Erase StrFixArray ' Each element set to 0.

Dim VarArray(10) As Variant ' Variant array.
Erase VarArray ' Each element set to Empty.

Dim DynamicArray() As Integer ' Dynamic array.
ReDim DynamicArray(10) ' Allocate storage space.
Erase DynamicArray ' Free memory used by array.
Share:
16,268
Sgdva
Author by

Sgdva

Greetings! I am an advanced user in Excel-VBA you may find most of my useful posts there. I am a beginner for Android Development and try my best whenever it is possible. I upload Excel tutorials on Youtube as well! Kindly check the updates at Facebook! Do not hesitate to contact me through my web presence! Happy coding!

Updated on June 14, 2022

Comments

  • Sgdva
    Sgdva almost 2 years

    Background:

    I just came across this question and made me learn about erase statment in arrays.

    I have always used the following to reset them:

    Sub TestWithRedimOnly()
    Dim ExampleArray() As String
        ReDim Preserve ExampleArray(1)
        ExampleArray(1) = "yo"
        MsgBox ExampleArray(1)
        ReDim ExampleArray(0) As String
        MsgBox ExampleArray(1) 'this confirms is reset!
    End Sub
    

    If I do it with erase

    Sub TestWithEraseAndRedim()
    Dim ExampleArray() As String
        ReDim Preserve ExampleArray(1)
        ExampleArray(1) = "yo"
        MsgBox ExampleArray(1)
        Erase ExampleArray
        MsgBox ExampleArray(1) 'this confirms is reset!
        ReDim ExampleArray(0) As String
        MsgBox ExampleArray(1) 'this confirms is reset!
    End Sub
    

    At the end, both reset the variable.

    Question: Is it really worthy to use erase statement? Does it prevent memory leaking better than just the Redim.. As.. Are there any cases that you can recall about not doing it like so?

    • Alex K.
      Alex K. almost 8 years
      One difference is that after redim a(0) you still have an allocated array with 1 index at a(0). Erase exists to destroy an array, so if thats what you want to do thats what you may as well use.
    • Sgdva
      Sgdva almost 8 years
      Is it really worth to call the erase? I mean an array with 1 index with nothing on it vs an array totally destroyed and then re build -if I want to re use the array variable-
    • Alex K.
      Alex K. almost 8 years
      Erase does what you intend ReDim ExampleArray(0) to do in your example, except it does it "better" and is shorter to type and clear in intent. Personally I can see no reason not to use it.
    • Sgdva
      Sgdva almost 8 years
      So at the end, are they "code synonyms" -in terms in reliability/speed/memory usage/intention- and are up to the coder style?
    • Alex K.
      Alex K. almost 8 years
      No, they are different in behaviour; accessing arr(0) would throw an error after Erase but not after ReDim
    • Sgdva
      Sgdva almost 8 years
      You are right, since the other one would not have even index 0 to start with. For practical purposes, is really a difference of "resetting" with index 0 the array than with erase that completely cleans the array?
    • Comintern
      Comintern almost 8 years
      Why would not using either of them leak memory? They'll get garbage collected when they leave scope regardless.
    • Sgdva
      Sgdva almost 8 years
      Yeah I use one of them, just were wondering which one was the best and why
  • Sgdva
    Sgdva almost 8 years
    Yeah ReDim NumArray(10) allocates the memory for an array index 10 -or 11 based on base- (hence being different from resetting the variable),but, if I do ReDim ArrayDesired(0) as Integer instead of Erase NumArray what's the difference?
  • Sgdva
    Sgdva almost 8 years
    I guess this is a good advice, even though one may behave similar to the other one, for standardized purposes, Erase would be a better way to go.
  • Rodger
    Rodger almost 8 years
    It is a nominal difference at best. If you do Dim arr() ReDim arr(0) Erase arr() and step through you will see that a redim(0) is still an array of 1. An erase arr() empties it entirely.