Freeup Used Variabel

ZeAL
12-10-2004, 07:42 PM
hi, i have 2 huge iterations with a huge 2 dimensional arrays.



dim MyArray1(2000000) as byte
dim MyArray2(2000000) as byte
dim i as long

for i = 0 to 2000000
MyArray1(i) = i * i
next i

for i = 0 to 2000000
MyArray2(i) = i xor i
next i



how can i freeup MyArray1's memory before the second iteration?
i've tried to use Redim MyArray(1), but it has no effect. my app keep slowing down after the first iteration.
please, help me...
thanks for your time, and sorry for my bad english..

Iceplug
12-11-2004, 10:59 AM
How about MyArray1.Dispose() or MyArray1 = Nothing? :)
Though, that will get rid of the stuff that you just put into the array. If you were doing something with the array after the For Loop, then it would be fine.

i Xor i is always 0, by the way. :)

ZeAL
12-12-2004, 07:59 PM
Nope.. it didn't work..
There is no "dispose" for array.
And there's no diferences in memory usage, before and after "MyArray1 = nothing".

In the original code, after the first iteration, MyArray1 will be processed.
But after the process is done, i need to freeup its allocated memory before i can continued with the second iteration.
in that way (i think), my app (second iteration) won't slow down because of low memory.

Does all application that was builded with vb.net consumes a lot of memory?
when i load my app for the first time, it consumes 10 megs of memory.
is it normal? how can i reduce it? how can i optimize my code without having to change lots of code? (compiler settings, maybe?)

btw, is there a way i can convert string to bytes array?

sorry for my grammar, i'm not good in english. :)
thanks for your time and help...

excaliber
12-13-2004, 04:42 AM
The memory will be freed when you leave the procedure. Or, if you really want to, you could set it to Nothing then call the Garbage Collector (under System.GC namespace, i believe). But it would probably be better to let the GC do its own thing by itself.

Yes, that is normal memory usage. A simple windows form and all that it takes to initialize it uses roughly 10 MB of memory on startup.

Use this to get the bytes from a string:


System.Text.ASCIIEncoding.ASCII.GetBytes(myString)

Joe Mamma
12-13-2004, 07:48 AM
Dim arry() As Byte = New Byte(200000) {}
Dim i As Long
For i = 0 To arry.Length - 1
arry(i) = i * i
Next i
'reiniitalize
arry = New Byte(2000000) {}

Mike Rosenblum
12-13-2004, 08:07 AM
Does all application that was builded with vb.net consumes a lot of memory?
when i load my app for the first time, it consumes 10 megs of memory.
is it normal? Yes, this is normal. Testing the memory consumption of an Empty Form on my computer comes out to 9.7 MB used.



how can i reduce it? Really you can't. Well, ok, I just tested a Console Application (that is, an Application with no Form, operating from a C-Prompt) and it consumes only 5.4 MB. But beyond this, I don't think you can do anything. 5 MB or so would seem to be the overhead required to keep a .Net program running.



how can i optimize my code without having to change lots of code? (compiler settings, maybe?) Turning off bounds checking and the like would speed things up, but would not reduce memory usage. :(



Nope.. it didn't work..
There is no "dispose" for array.
And there's no diferences in memory usage, before and after "MyArray1 = nothing". Yes, there does not seem to be a "dispose" for an Array. However, setting 'MyArray = Nothing' does in fact release your memory resources, but you won't see it until the next Garbage Collection cycle. One generally shouldn't force the issue by calling GC.Collect out of habit, but for testing the principle, you can show it as follows:
Private Sub Array_RAMTester()
Dim MyArray() As Integer
MessageBox.Show(Environment.WorkingSet.ToString("#,###")) ' <-- Reports: 15,138,816

' Allocate 4 MB of Memory:
ReDim MyArray(1000000)

'Initialize all 1,000,000 Integers:
For i As Integer = 0 To MyArray.Length - 1
MyArray(i) = i
Next
MessageBox.Show(Environment.WorkingSet.ToString("#,###")) ' <-- Reports: 19,156,992

MyArray = Nothing
MessageBox.Show(Environment.WorkingSet.ToString("#,###")) ' <-- Reports: 19,156,992

GC.Collect()
MessageBox.Show(Environment.WorkingSet.ToString("#,###")) ' <-- Reports: 15,306,752
End Sub The memory values in the above are around 15 MB (not 10 MB) because I tested this in Debugging mode, and there is more overhead. Notice the 2nd to last Report, where we set 'MyArray = Nothing' and yet the Memory usage does not change. However, after calling GC.Collect(), it does acknowledge the release. :)



btw, is there a way i can convert string to bytes array? It won't save you any memory, but this would do it for you:Dim Str As String = "My sample string..."
Dim bytes() As Byte
Dim ASCII As System.Text.Encoding = System.Text.Encoding.ASCII
bytes = ASCII.GetBytes(Str) It's more normal to iterate over the Chars of an Array, but Bytes are fine. Also, if using Unicode (double-byte) characters, you might want a Unicode Byte array using System.Text.Encoding.Unicode instead.

-- Mike

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum