Solved

Memory leaks in VB6

Posted on 2004-04-12
17
2,015 Views
Last Modified: 2013-11-13
I am having a major problem with a new application we have written.  The application uses a number of DLLs, all of which were written in VB.  We call the DLLs via:

Dim obj as DLLName.Class
set obj = new DLLName.Class
....
set obj = nothing

When we call each DLL, depending on the DLL, the working set of memory for our app increases.  This makes sense.

The problem is that after we are done with the DLL and set the object to nothing, while the DLL appears to unload from memory (using Process Explorer to view it) the memory for our app still remains high!  Even after a few hours it doesn't go back down.

We tested this by writting a little test app which creates a new XML file, adds 200K to the file and then closes it.  Memory spikes from 3Megs to 25Megs and then, after closing, drops to 20Megs (not the 3Megs).

Is there a better way (memory wise) of loading, calling and unloading DLLs from memory that would result in freeing up memory after you are finished with an object?  
0
Comment
Question by:jlippiner
  • 9
  • 4
  • 2
  • +2
17 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10805764
The main criteria is: does memory grow with every creation of DLLName.Class? If not, your application is OK. Try to run the same function which creates, uses and releases DLLName.Class, number of times. If every operation adds something to memory used by your application, you have a problem. If memory remains at 20Megs, program is OK.
Windows application manages it's virtual memory committing additional physical memory when necessary. However, it doesn't release committed memory immidiately if there is no special reason to do this.
I think it is difficult to make memory leak using pure VB code. If you have Bounds Checker, run your program under it to see if it is OK.
0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 10805838

It is  not sufficient to destroy your Dll by setting it to Nothing.

Inside a Dll a 100% cleanup in necessary. all objectes inside the DLL must explicitly

get desroyed after you used the Dll(preferably in terminate event). This includes all Database objects, Forms, other classes, etc.
0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 10805849

It is  not sufficient just to destroy your Dll by setting it to Nothing.

Inside a Dll a 100% cleanup in necessary. all objectes inside the DLL must explicitly

get desroyed after you used the Dll(preferably in terminate event). This includes all Database objects, Forms, other classes, etc.

So try that and if still its not working then try to attach some of the code inside your Dll here. specially objects being used.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
LVL 15

Expert Comment

by:unknown_routine
ID: 10805850

It is  not sufficient just to destroy your Dll by setting it to Nothing.

Inside a Dll a 100% cleanup in necessary. all objectes inside the DLL must explicitly

get desroyed after you used the Dll(preferably in terminate event). This includes all Database objects, Forms, other classes, etc.

So try that and if still its not working then try to attach some of the code inside your Dll here. specially objects being used.
0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 10805853

It is  not sufficient just to destroy your Dll by setting it to Nothing.

Inside a Dll a 100% cleanup in necessary. all objectes inside the DLL must explicitly

get desroyed after you used the Dll(preferably in terminate event). This includes all Database objects, Forms, other classes, etc.

So try that and if still its not working then try to attach some of the code inside your Dll here. specially objects being used.
0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 10805857
??? I have no clue why my message posted 4 times! LOL :))
0
 

Author Comment

by:jlippiner
ID: 10805909
If all the objects in the DLL were not cleaned up, wouldn't the DLL still appear in Process Explorer?

A few minutes after unloading the DLL it disappears from Process Explorer, which indicates to me that any memory it was using should be freed.  Is this not correct?  How can a DLL unload BUT the objects inside the DLL that unloaded still be loaded ?!
0
 
LVL 4

Accepted Solution

by:
frankd earned 500 total points
ID: 10805978
Hi,

I had a similar problem with an ocx I wrote.  It turned out that I needed the latest service pack to fix it.  What system are you running on and what SP have you installed?

Frank
0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 10806224
If all the objects in the DLL were not cleaned up, wouldn't the DLL still appear in Process Explorer?
===============================================================

Unfortunatlely answer is not neccessary, even if you kill the dll,it does not mean all the memory resources it uses

 How can a DLL unload BUT the objects inside the DLL that unloaded still be loaded ?!

Very simple: VB is not smart enough that automaticaly figure out  which memory blocks are being used
by the objects inside the dll. and thats why it asks the user to destroy them one by one.

Same thing exists for a form. you can unolad a form but unless you destroy it propery ,memory wont get freed.

0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 10806244
If all the objects in the DLL were not cleaned up, wouldn't the DLL still appear in Process Explorer?
===============================================================

Unfortunatlely answer is not neccessary, even if you kill the dll,it does not mean all the memory resources it uses

 How can a DLL unload BUT the objects inside the DLL that unloaded still be loaded ?!

Very simple: VB is not smart enough that automaticaly figure out  which memory blocks are being used
by the objects inside the dll. and thats why it asks the user to destroy them one by one.

Same thing exists for a form. you can unolad a form but unless you destroy it propery ,memory wont get freed.


Attach somne of your code here. specaily the declaration part.
0
 

Author Comment

by:jlippiner
ID: 10806943
Frank -

You nailed it!  It wasnt us after all but Microsoft.  After compiling with the latest SP6 the memory issue is resolved.

Thanks,

Josh
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 10806976
Then I don't see why you give out A instead of B grade.

to be honest, frankd gave you a hint that will save you a lot of time

8->
0
 

Author Comment

by:jlippiner
ID: 10807015
I gave him the middle option.  Whatever that was.  As for the reasoning - no one else seemed to get it.
0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 10807444
you have a point there  EDDYKT.

If frankd solution  removed the memory leak ,which is one of the major issues in VB applications, then
he deserved an A.
0
 

Author Comment

by:jlippiner
ID: 10807543
Actually I might have spoke to soon.

While frankd's solution seemed to fix most of the memory issues, some DLLs are still causing the memory to remain high even though they are unloaded.

Any other suggestions are welcome.

Regards,

Joshua
0
 
LVL 4

Expert Comment

by:frankd
ID: 10807688
jlippiner,

I thank you for the points.  While an A would have been nice, I'm here to help where I can.  If you find out that my idea was not an answer I'm sure your points could be refunded.

Regards,

Frank
0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 10807832
then continue with my suggestions.
0

Featured Post

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.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

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