Solved

Can memory be released by VB6?

Posted on 1999-01-07
9
290 Views
Last Modified: 2008-03-10
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?
0
Comment
Question by:Torus
  • 3
  • 3
  • 3
9 Comments
 
LVL 2

Accepted Solution

by:
trillo earned 50 total points
ID: 1454778
I'm surprised!... The variable shouldn't behave like that.
The memory is allocated whenever a variable is declared, and that variable is destroyed when it goes out of its scope, meaning that the memory is released. There no way you can release that memory, because you don't know WHERE in the memory is your string stored.

I think there must be something wrong.. maybe a global variable with the same name as the local variable.... Oops, wait a moment!
How do you know the memory is still occuppied by your string?

I think theres a misunderstood.. he he... I mean, Your string, as any other variable, needs memory to be stored (this is obvious). When you change the value of a variable, you're actually changing some numbers somwhere in memory (where the variable is stored). BUT... here is the important part... when the variable is destroyed, the memory is now free to be used by another variable from your program or any other program... This means that your string, for example "Hello World", is still there, because you only released the variable, not its contents.
"Hello World" will stay there until some other program needs memory and it will use the location of your previous string.

Trillo
0
 
LVL 2

Author Comment

by:Torus
ID: 1454779
Firstly I also think that I have done something wrong. However, I write a very simple program to test it. Just add a command button and then

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

0
 
LVL 2

Expert Comment

by:trillo
ID: 1454780
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....
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 2

Author Comment

by:Torus
ID: 1454781
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
0
 
LVL 2

Expert Comment

by:trillo
ID: 1454782
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
0
 
LVL 15

Expert Comment

by:ameba
ID: 1454783
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.
0
 
LVL 2

Author Comment

by:Torus
ID: 1454784
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?
0
 
LVL 15

Expert Comment

by:ameba
ID: 1454785
>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).

0
 
LVL 15

Expert Comment

by:ameba
ID: 1454786
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

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question