Solved

Can memory be released by VB6?

Posted on 1999-01-07
9
280 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Author Comment

by:Torus
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 2

Expert Comment

by:trillo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now