Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preventing Memory Leakage

Status
Not open for further replies.

Alt255

Programmer
May 14, 1999
1,846
US
This really isn't much of a tip but I see the problem surfacing in this forum at least once or twice a week.

Never assume that terminating a VB application restores Windows to the state it held before the application was sarted. Executing the "End" statement only terminates program execution without releasing the memory resources allocated with the load of each form. Additional resources are depleted every time the application is started and ended.

To help avoid memory leakage, use this code in your main form:
[tt]
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
For F = 0 To Forms.Count - 1
Unload Forms(F)
Next
End Sub
[/tt]

And make sure all of the app's external resources are released too!
VCA.gif

 
well that helps memory allocations that were assign to objects. Here is an example of how you can cause a bad memory leak if you dont watch it.

do someloop
obj = new objname
msgbox obj, ....
loop

also assuming you dont get rid of any objs, you end up cluttering the memory with references, that could not be removed, eve if you killed any of their parent forms.
Karl
kb244@kb244.com
Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)

 
Good point, Karl. The unload sequences should enumerate every "new" object and destroy it before the app terminates.

What say we create a FAQ on the "clean" way to terminate an application?

VCA.gif

 
or rather perhaps tips and notes not only to just terminate floating memory, but what to watch for in your own coding to prevent possible memory leaks.

(belive me, I Get big in memory leak prevention most of the time, I write in C++ too, and when working with pointers in C++ and other dynamic access, a memory leak can create a world of living hell for your computer)
Karl
kb244@kb244.com
Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)

 
This is really interesting. I nevery really watched for my memory leaks and now that i have a really big program with alot of data in memory i could really use this "faq" that you guys are talking of. if it gets made i would really apreciate getting pointed to it.

thanks
Karl Pietri
lordhuh@home.com

Karl Pietri
lordhuh.pota.to

 
The Tek-Tips FAQ service is temporarily out of service, Karl (lordhuh). If Karl (kb244) is willing, we can whip something up and place it in the VB General Discussion FAQ area as soon as it is up and running.

If anybody else would like to throw in some related tips or code, send it to my email address and I'll merge them into the FAQ. I'll make sure that everbody receives full credit for his contributions.
VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Or :

Dim Form As Form
For Each Form In Forms
Unload Form
Set Form = Nothing
Next Form
End Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX
Source CodeBook for the programmer
 
More info on memory leaks would be greatly appreciated.
Easiest way I know to see memory leaks is to monitor unused physical memory and allocated memory with the Win98 systems monitor set to say 1 scan every 5 minutes.
You will get a shock how many programs leak until they crash even though the resources monitor shows resources at 90% after they have been running a few hours or days.
A typical indication is that the prog eventually seems to run in slow motion before it freezes.
I find that the MCI control causes a program to leak even though the prog doesnt leak withpout it and the MCI control doesnt leak when it is the only thing in a program. Anyone know why?
Also opening and closing a copy of IE5 often recovers more memory than was there before you opened it. Is there some magical routine in it that recovers leaked memory?
 
Interesting thing about the little example of Edderic:
- It first unload a object
- And then set it's reference to nothing
So to me it seems that when unloading a object, there's still a reference left, otherwise VB would raise an error when trying to set it's reference to nothing.
 
remedy: Correct, all the unload-statement does is remove the form from the memory (wich is after all what we discuss... :), static variables and (I belive) properties of the form remain and can still be accesed until the referanse is set to nothing, or the program terminates.

-Mats
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top