» Home » VB Fibre
Site
News
Files

Visual Basic
Strings
Math
General
Properties
Memory
Methods

Search
Testing

Inline ASM-VB
Strings
Math
General
Memory

Search
Using inline ASM
Submit!

Clearing arrays the right way

There are a few methods to clear your arrays. You should alwaysclear your arrays do when you do not use them anymore or exit your program. Some people use:

Code:
Redim MyArray(0)

But that ain't good. Your program will reallocate your array so that it will contain one item! Even if your array only has a few bytes for every item, it ain't good. The memory won't return to Windows. The best way to clear your arrays, making them completely empty is the following method:

Code:
Erase MyArray

That's all! The entire array will be into oblivion now, and the memory has been cleared…

None % faster than None None (sec) None (sec)
0% 1 1
0% 1 1
0% 1 1
0% 1 1
0% 1 1


User contributed notes:

Author: PRoPHEZZoR (Name_Here_No_Spam at HaCKERMAiL dot COM) Date: 11:08 24/08/2004
Gnerk! It's the SAME function when compiled! Otherwise you wouldn't get the same results!... You haven't really tested it, have you?

Author: Almar Joling () Date: 11:08 24/08/2004
This is not about time, it's about clearing memory the way that people not do. Most people use Redim ArrayName(0) instead of Erase, which still leaves one item.

Author: Tom (hurendo_kun at hotmail dot com) Date: 15:05 31/05/2005
Do they? Wow, I'd never considered using ReDim to clear an array unless I needed to reallocate the array for new data --- in which case I was going to dump the old anyway. However, you forgot to mention one very important detail: ReDim only works on variable-length arrays (those declared initially without dimensions), whereas Erase works on both fixed- and variable-length arrays. It's a good standard to use no matter how you look at it.

Author: Tanner Helland (tannerhelland at hotmail dot com) Date: 23:08 23/08/2006
And they most definitely DO NOT compile to the same thing! It's just as Almar wrote in the description - ReDim MyArray(0) still leaves the array in memory, along with the entire SafeArray header (which doesn't take up much space, but it still exists!). Erase clears out all references to the array, and is always the way to go.

Although it is worth mentioning that if the array is declared locally, VB will automatically erase it when it goes out of scope.

Author: MegaKameha () Date: 16:07 04/07/2007
ReDim myArray(-1) ;)

Add user-note
Author:
E-mail (optional):
Anti spam, please enter 'ok' without quotes:
Comment: