Reclaiming memory from the allocation of a large byte array
Posted on 2004-11-22
Under VB 6 you could delcare a dynamic array, make it as big as you wanted it, then Erase it to reclaim the memory allocated to it.
Under VB.NET, I'm allocating a 20mb byte array as a local variable in a function.
Dim TotalBytes(20000000) As Byte
I've tried everything I can think of to get VB to release the memory allocated to this array once it goes out of scope....
I've tried Dimming it inside of an If/Then/Else block, and Erasing it:
I've tried Clearing it:
TotalBytes.Clear(TotalBytes, 0, 20000000)
I've tried Redimming it:
I've tried setting it to Nothing:
TotalBytes = Nothing
And, at the end of the procedure, I've tried collecting the garbage:
However, if I'm looking at my Available Memory under the Task Manager, I don't get the memory back until I exit the program. Now, In the past, I've been able to get around things like this with a kludge such as loading a hidden form with this code inside... when the function ends, then unload the form and set it to nothing... that might work, but I'm wondering, does anybody have another technique for reclaiming the memory allocated to a large byte array?