Solved

Memory leaks in VB6

Posted on 2004-04-12
17
1,990 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

705 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

21 Experts available now in Live!

Get 1:1 Help Now