Torus
asked on
Can memory be released by VB6?
If I have a string variable and then use the string function to assign the size, i.e MyString = string(3000,0), even it is a local variable, the memory is still occupied when the function or procedure exit. Can I release the memory after using the variable?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I've never trusted programs such as task manager (I could be wrong, of course).
If you see well, you're only "wasting" 3000 characters which is I think 3000 bytes, but let's say you're using 10 000 = 10 Kb... which is nothing compared to some 16 Mbytes of RAM (less than 0.1%)... and if we include the Virtual Memory, then you're using NOTHING... The other thing could be that while you're pressing the commandbutton, windows uses some memory to repaint the controls, activating windows, seting focus to some windows and killing focus from others, etc....
If you see well, you're only "wasting" 3000 characters which is I think 3000 bytes, but let's say you're using 10 000 = 10 Kb... which is nothing compared to some 16 Mbytes of RAM (less than 0.1%)... and if we include the Virtual Memory, then you're using NOTHING... The other thing could be that while you're pressing the commandbutton, windows uses some memory to repaint the controls, activating windows, seting focus to some windows and killing focus from others, etc....
ASKER
not 3000bytes. It is double since VB string is saved as Unicode.
BTW, if a program is very large and has many variables, for example, uses 2 to 3 Megabytes memory, it quite wastes memory(if really not release according to task manager). It is also quite significant if many other programs runs together.
Thanks
BTW, if a program is very large and has many variables, for example, uses 2 to 3 Megabytes memory, it quite wastes memory(if really not release according to task manager). It is also quite significant if many other programs runs together.
Thanks
Well, that's right, because when the physical RAM is all used, Windows uses the famous swap file. If you have programs A and B running, if app A is active, Windows saves on a temporary file the contents of the memory of app B, that's why 2 or more programs can share the same memory location, and the same goes for more than 2 programs.
The more programs you have, tho slower it gets because of the time needed to read and write the swap File
The more programs you have, tho slower it gets because of the time needed to read and write the swap File
It is good idea to release memory. VB will do it, but is not very good (C or D) in releasing memory.
After using object or collection set it to Nothing.
Erase arrays
set your bigstring=vbNullString
VB6 is better then VB5. E.g. in VB5 memory will be released faster this way (collection):
For ii = mcol.Count To 1 Step -1
mcol.Remove ii
Next
Set mCol = Nothing
then simple
Set mCol = Nothing
Also, NT will handle memory much better then Win95.
There are also big problems in releasing memory for controls, e.g. ListView. So, we must add code to help VB's garbage collection.
After using object or collection set it to Nothing.
Erase arrays
set your bigstring=vbNullString
VB6 is better then VB5. E.g. in VB5 memory will be released faster this way (collection):
For ii = mcol.Count To 1 Step -1
mcol.Remove ii
Next
Set mCol = Nothing
then simple
Set mCol = Nothing
Also, NT will handle memory much better then Win95.
There are also big problems in releasing memory for controls, e.g. ListView. So, we must add code to help VB's garbage collection.
ASKER
Yup. Ameda. I also found that there is a great problem to release controls. I used some third party control before, it consumed much memory without releasing. This make me quite bothing.BTW, do you have good methods for garbage collection? If using "unload" to unload a control(if using control array) can really release the memory?
>can really release
You must test it. It depends on type of control.
Setting form to Nothing should do it, but VB is not very good in doing this (saying OS to free memory).
You must test it. It depends on type of control.
Setting form to Nothing should do it, but VB is not very good in doing this (saying OS to free memory).
Example:
ListView control is filled with data from Biblio.Mdb, table Titles
It has Columns: 0-Title and 1-ISBN.
LoadList: 7 sec
lvTitles.Clear 1.1 sec
-----------
Everything looks OK (tested on P120/64MB, users have P200 or
better and response time will be acceptable)
Now, user Clicks on ColumnHeader 0
lvTitles is now sorted; lvTitles.SortKey = 0
' and look here ************************** **
lvTitles.Clear 20 sec ' surprise (or tragedy?) !!!
This is how VB (or OS) handles freeing ListView.
This is not the right way. I am sure.
-----------
With a trick (again "do-it-yourself" or "prepare-your-garbage")
this is 6 times (~ a magnitude) faster - clear in 2,9 sec.
t = GetTickCount
If lvTitles.Sorted = True Then
lvTitles.SortKey = 1 ' sorted by ISBN
End If
lvTitles.ListItems.Clear
Debug.Print "clear in " & (GetTickCount - t) / 1000
ListView control is filled with data from Biblio.Mdb, table Titles
It has Columns: 0-Title and 1-ISBN.
LoadList: 7 sec
lvTitles.Clear 1.1 sec
-----------
Everything looks OK (tested on P120/64MB, users have P200 or
better and response time will be acceptable)
Now, user Clicks on ColumnHeader 0
lvTitles is now sorted; lvTitles.SortKey = 0
' and look here **************************
lvTitles.Clear 20 sec ' surprise (or tragedy?) !!!
This is how VB (or OS) handles freeing ListView.
This is not the right way. I am sure.
-----------
With a trick (again "do-it-yourself" or "prepare-your-garbage")
this is 6 times (~ a magnitude) faster - clear in 2,9 sec.
t = GetTickCount
If lvTitles.Sorted = True Then
lvTitles.SortKey = 1 ' sorted by ISBN
End If
lvTitles.ListItems.Clear
Debug.Print "clear in " & (GetTickCount - t) / 1000
ASKER
Dim teststr as string
teststr = string$(3000,0)
I am running the program in NT environment. I open the task manager to see how many memory is used. After I click the command button, the memory used is increased. I wait for a long time without decreasing.
Or I interpreted the symptom wrongly ?
Thanks