Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Memory leaks in VB6

Posted on 2004-04-12
17
Medium Priority
?
2,057 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
[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
  • 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
Technology Partners: 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 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 1500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

670 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