Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can memory be released by VB6?

Posted on 1999-01-07
9
Medium Priority
?
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
9 Comments
 
LVL 2

Accepted Solution

by:
trillo earned 100 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

718 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