0

see i m using multiple time malloc & free.

so at the end of application i want to make sure there is no memory leakage. all malloc are freed.

Is there any method or function to see that?

another question : all all os mostly reclaim memory only when that application gets exit but if application is suppose to be run long time & if this way it continuously leack memory then at some time there will be no unalloacated memory & application will be crash or system will re-boot...!! Is it true..???

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222

4 Answers4

3

At the end of a process the OS reclaims used memory (so it cannot "leak").

so at the end of application i want to make sure there is no memory leakage

EDIT

James raised an interesting point in the comments: "Any decent programmer should not rely on the OS to do his job". I must underline I was thinking of the following scenario:

/* mallocs */

/* frees <- useless */
exit(0);
Community
  • 1
  • 1
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • also possible in embedded devices? without re-bout?? – Jeegar Patel Aug 05 '11 at 11:04
  • @Mr.32 Any decent OS should reclaim memory. – cnicutar Aug 05 '11 at 11:05
  • then why all people say that there should not be any memory leackage? – Jeegar Patel Aug 05 '11 at 11:06
  • 2
    Any decent programmer should not rely on the OS to do his job. – James Aug 05 '11 at 11:07
  • what happen if application is not going to end > i mean its works for long time & if it continuously make memory leakage? – Jeegar Patel Aug 05 '11 at 11:09
  • @Mr.32 If the application continuously requests memory and doesn't free it, it will eventually run out of resources. – cnicutar Aug 05 '11 at 11:11
  • @James What is there to gain in freeing memory right before calling exit ? – cnicutar Aug 05 '11 at 11:12
  • You need to put the project in perspective. For your one-off 100 line C program maybe you can let the OS handle the job. But as James pointed out, a good programmer isn't going to rely on the OS to do his job. Also, what if someone else needs to modify the code later? I don't think this is a good answer considering the question. – Mr. Shickadance Aug 05 '11 at 11:17
  • @Mr. Shickadance See my edit :-) I am quite sure you are already relying on your OS for a lot of things (when did you last directly interface with your HDD ?). – cnicutar Aug 05 '11 at 11:19
  • @James: Does this mean any decent programmer should write his own HDD driver, filesystem, etc. too? Perhaps this philosophy is profitable; Oracle may have a job opening for you..... – R.. GitHub STOP HELPING ICE Aug 05 '11 at 11:23
  • Just a hint that helped me a few times with leakages: in Linux `atop` command (and then `m` and `M` option) will show _increment_ of the memory allocated by each process every interval – Jakub M. Aug 05 '11 at 11:37
  • @James: No decent programmer should interfere with the OS doing its job – Christoph Aug 05 '11 at 11:48
  • @all all os mostly reclaim memory only when that application gets exit but if application is suppose to be run long time & if this way it continuously leack memory then at some time there will be no unalloacated memory & application will be crash or system will re-boot...!! Is it true..??? – Jeegar Patel Aug 05 '11 at 11:58
  • @Most People: Freeing memory isn't considered interfering with the OS. Freeing memory at exit isn't a gift from the OS to you, it's just what happens when/if a program exits. Fact is it won't take care of things like freeing handles or other resources your lazily written app is holding. What's there to gain? Pride in your work, you'll be a better developer for doing it. – James Aug 05 '11 at 15:46
  • @James: see http://stackoverflow.com/questions/6346969/there-is-no-point-in-freeing-blocks-at-end-of-program/6347182#6347182 for an example of negative impact of explicit memory cleanup; also, I do consider proper process destruction within the responsibility of the OS – Christoph Aug 05 '11 at 16:16
  • @james : then why we are freeing memory at program exit..??/ if all memory is gona free automaticaly when program exit... – Jeegar Patel Aug 05 '11 at 17:28
2

It is not guaranteed that the OS will reclaim your memory. A desktop or a server OS usually will; an embedded OS might not.

There are several debugging malloc libraries out there; google for debug malloc and use one that suits you. GNU libc has a debugging malloc built in.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
1

First, You should compile your code with debugging support (In gcc, it is -g). Note that this isn't a necessity but this enables the debugger to provide you with line numbers as one of the advantages.

Then you should run your code with a nice debugger like valgrind or gdb or whatever. They should tell you the lines where the memory was allocated but not freed.

Valgrind is a very powerful tool for debugging. You'd need to use the --tool=memcheck option (which i think is enabled by default but doesn't hurt to know).

Nalin Kanwar
  • 120
  • 8
0

You could wrap malloc() and free(), and count some basic statistics by yourself

#define malloc(x) malloc_stat(x)
#define free(x) free_stat(x)

static counter = 0;

void* malloc_stat( size_t s ) {
    counter++;
    return malloc(s);
}

void free_stat( p ) {
    counter--;
    free(p);
}
Jakub M.
  • 32,471
  • 48
  • 110
  • 179