Solved

Memory Leak question

Posted on 2002-06-05
18
295 Views
Last Modified: 2013-12-14
If you have an executable, and it has a memory leak, does the memory leak go away when the executable quits?

In other words when you close the application, or when the application exits, is all the memory free'd that was created with new()?

If this issue is platform depedent, my targets is Unix, but I still would be interested in Win32 target information not related to API functions leaks.

Thanks in advance
0
Comment
Question by:Hill8982
  • 7
  • 6
  • 4
  • +1
18 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7056482
>>If you have an executable, and it has a memory leak,
>>does the memory leak go away when the executable
>>quits?

It is hard to give a general answer, but, for most 'modern' OSs, it should be 'yes' and deefinitely is 'yes' for Win32 (not to talk of resource leaks on Win9x). Memory used by an application is listed in 'page table directories', and once a process exits, the alloted memory is freed again.
0
 

Author Comment

by:Hill8982
ID: 7056689
jkr,
Thanks for the Win32 information.

I'm looking for a difinitive answer with the Unix platform.  Specifically DEC, Sun, and HP.
If someone can give a good refference, I'll increase the points.

The reason I'm looking for this information, is that I've been asked to research memory-leak tools in order to test my Unix application.
But I don't think it's necessary because my application is a command line application that runs and exits in a matter of seconds.

My Boss tells me that memory leaks do persist after the executable exits.  So I'm looking for documentation that will either show he's right or wrong.
0
 
LVL 86

Expert Comment

by:jkr
ID: 7056767
Well, that page table thing was addressing the UN*X part :o)
0
 

Author Comment

by:Hill8982
ID: 7056790
jkr,
>>Well, that page table thing was addressing the UN*X part :o)
Ohh!  I didn't get that.

Not that I'm doubting your word, but I'm looking for something I can show to my Boss, (Other then a link to this question) :-)

Do you know of any links or books that would verify this information?
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 7056845
What about a demonstration? Just use this 'program'

void main () {

char *p = new char [ 100000000];

}

and take a look at e.g. /proc/meminfo before and after
0
 

Author Comment

by:Hill8982
ID: 7056872
>>and take a look at e.g. /proc/meminfo before and after

I don't understand.  Is there a meminfo program?  

I know there's a meminfo struct and a meminfo function, but I'm not sure how to get current available memory from that, and wouldn't other running processes currupt the data?
0
 

Author Comment

by:Hill8982
ID: 7056879
Exactly how could I verify that there is no leak after running above program?
0
 
LVL 86

Expert Comment

by:jkr
ID: 7056893
'meminfo' is a 'file' in the /proc filesystem, where a lot of kernel facilities are exposed in a filesystem-like fashion - this is also a 'common feature' of the newer UN*X-OSs.
0
 

Author Comment

by:Hill8982
ID: 7056938
>>this is also a 'common feature' of the newer UN*X-OSs.

Doesn't seem to be available in any of the Servers I have access to.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:jkr
ID: 7056947
Well, then what about 'top'? :o)
0
 
LVL 22

Expert Comment

by:nietod
ID: 7057094
Of course its a good idea to avoid memory leaks in the first place.  You might consider using classes to automate memory lifetime, like smart pointer classes.   In 8+ years I have not had even 1 memory leak reported in my application (except for intentional ones) because I automate all dynamic memory alocations.
0
 

Author Comment

by:Hill8982
ID: 7057326
>>Of course its a good idea to avoid memory leaks in the
>>first place.  
Of course.
I'm inherting which my boss does not want to change unless neccessary.
In my own code, I don't create memory with dumb pointers directly.
0
 

Author Comment

by:Hill8982
ID: 7057332
>>Well, then what about 'top'? :o)
This gives a lot of information.  How can I tell what is being effected by a possible memory leak?
0
 
LVL 30

Expert Comment

by:Axter
ID: 7057445
0
 
LVL 30

Expert Comment

by:Axter
ID: 7057451
Example code to call jkr's memory leak program.
Compiled on DEC, but should work for Sun and HP.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>


int main ()
{
 rusage r1,r2;
 memset(&r2, 0, sizeof(r2));
 getrusage(RUSAGE_SELF, &r1);

 system("memleak"); //call jkr's memleak program

 getrusage(RUSAGE_SELF, &r2);

 printf("unshared data %i/%i\nunshared stack %i/%i\nshared memory size %i/%i\n",
        r1.ru_idrss, r2.ru_idrss, r1.ru_isrss, r2.ru_isrss, r1.ru_ixrss, r2.ru_ixrss);
 return 0;
}
0
 
LVL 30

Expert Comment

by:Axter
ID: 7057461
FYI:
If you want to use top and you want to narrow the focus to specific user, you can use the following method for DEC:
top -Udmaisona

On other platforms, the following method should work:
top -d15 | grep dmaisona

Where 15 is how many times it does a repeat before it quits.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7057479
Here's another link you can check out:
http://saru.nendai.nagoya-u.ac.jp/global/sys/HTML/S/kern%20kern_exit.c.html

I'm not sure about Sun and HP, but DEC uses kern_exit.c.
According to above link, "Exit" deallocate address space and other resources.

Not very diffinitive....
0
 
LVL 86

Expert Comment

by:jkr
ID: 7058015
>>Example code to call jkr's memory leak program

Axter, please do not call that a "program" :o)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

863 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

22 Experts available now in Live!

Get 1:1 Help Now