-
December 16th, 2007, 10:07 AM
#1
Freeing memory of array out of a Subroutine
Hi,
I have a global variable that I want to "erase" from within a subroutine to free memory space. How can I do that?
Code:
'this is the main part of the program
...
DIM A(1000) as Double
...
FillA(100)
...
ClearAndFree()
FillA(20)
...
Private Sub ClearAndFree()
'here I want to free the memory
'but the array must remain a valid variable for future use
'something like:
'clear A
'gc.collect()
End Sub
Private Sub FillA(ByVal n As Integer)
'fills the array A() with numbers up to "n"
For i = 0 To n
A(i)=i/3
Next
End Sub
Any idea how ClearAndFree() must look like???
Thanks for your help!!!
-
December 16th, 2007, 12:30 PM
#2
Re: Freeing memory of array out of a Subroutine
What are you trying to do? You could create a new instance, and erase that.
-
December 16th, 2007, 01:20 PM
#3
Re: Freeing memory of array out of a Subroutine
David is right. You need to provide more information on WHAT you are trying to do [not HOW].
Also calling GC.Collect() (especially without a GC.WaitForPEndingFinalizers()) is a "good" idea about the same percentage of time as firing a .44 Magnum inside an airplane while at altitudes over 30,000 feet.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 16th, 2007, 01:34 PM
#4
Re: Freeing memory of array out of a Subroutine
Thanks for your replies so far. I hope I can provide the information to explain what I try to achieve:
There are several subs that write into this array A(). Therefore I defined it in the main part of my program so that it is globally accessible.
After another subroutine has worked with the data it is no longer needed. Since it is a large array I want to free the memory again. That's my goal.
Although I don't need the data anymore I still need the definition of this array so that the subs that write into A() have a place to store their information. That's what I tried to show with my example:
first write 100 numbers
free the memory
write again into the array (this time 20 numbers, but it could also be up to 1000)
I hope that makes it clear...
What was your comment about GC.Collect() supposed to mean? Sorry I'm a noob and probably using the wrong tools...
Thanks!
-
December 16th, 2007, 09:28 PM
#5
Re: Freeing memory of array out of a Subroutine
You should just set each element of the array to Nothing and allow the .NET garbage collector to worry about when to reclaim memory.
That said, if your array contains structures, as Double is, then doing so is pointless. Each element contains a value and whether that value is zero or something else it still takes up the same amount of space.
Only if the array contains class instance would there be any benefit to setting each element to Nothing. In that case you remove the reference to the object so the system can reclaim the memory it occupies, but only if and when the system decides it is necessary.
I would probably suggest that you only create an array object if and when you need one. Creating an array that can hold 1000 elements just in case you might need that many is a bit pointless. If you need an array that can store 20 objects then create one of that size. When you're done with that just get rid of it, then create another one of the appropriate size when you need another one.
Last edited by jmcilhinney; December 16th, 2007 at 09:32 PM.
-
December 17th, 2007, 01:32 AM
#6
Re: Freeing memory of array out of a Subroutine
Hmmm,
my problem is that I don't know in advance how much data the subroutines that fill the variable will put in (they do some kind of search and put the "hits" into the array).
I tried ReDim, but I get an error telling me that I can't do that on an array. Is there an other way of clearing the array prior to a "refill"?
If you need an array that can store 20 objects then create one of that size. When you're done with that just get rid of it, then create another one of the appropriate size when you need another one.
Would you mind giving me a piece of code that does that? That's exactly what I try to achieve and don't know how:
a) get rid of the array
b) create a new one (ideally with an unlimited boundary)
Thanks!
-
December 17th, 2007, 05:25 AM
#7
Re: Freeing memory of array out of a Subroutine
just use dynamic array and forget about the "Redim". if you are using .Net 2.0 or above, it is favored to use "Generics". below is a sample code that instantiate a dynamic array with an initial capacity of 1K, there's no problem though if you exceed that capacity..
Code:
Private Sub TheSubRoutine()
Dim a As new System.Collections.ArrayList(1024)
a.Add("value1")
a.Add("value2")
a.Add("value3")
a.Clear()
End Sub
Busy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|