Quick tip on dynamically created arrays

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,016
Location
Cambridge, UK
Programming Experience
5-10
For those of you out there you use arrays in vb.net, and are setting them to nothing after you are finished with them i would like to introduce you to a little known keyword in vb.net..

VB.NET:
Erase

When used with arrays this keyword will set all the elements in the array to nothing before you set the actual array to nothing. One implementation would be

VB.NET:
Dim Arr as array
 
ReDim arr(9)
'Do whatever
 
Erase arr
 
How much of a benefit would this be? If the array was the last surviving reference for a prticular group of objects, would they be garbage collected aswell. I have to admit, i'm somewhat lazy in this regard; I only have limited use for arrays and mine tend to exist over a very small scope - I hence leave them to fall out of scope and be garbage colelcted rather than doing anything explicitly with them.
Does Erase/array = Nothing have any advantage in this regard?
 
From what i understand it's just the .Net way of doing things...


Erase behaves differently when used with different types of arrays (apparently it's also faster than just setting the array to nothing, but don't shoot the messenger) When used with fixed size array using Erase will set all the elements to nothing. However when used with a dynamically created array (ReDim) it will actually free the resources/memory used by the array for garbage collection.
 
Back
Top