check memory leak in a program or code

We can use “valgrind”, simply compile your code with -g option, and then run
valgrind --leak-check=full ./program > output.log
if you run “top” then you will see a process called “memcheck”.

this is a kind of output you will get:
==1651== Memcheck, a memory error detector.
==1651== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==1651== Using LibVEX rev 1804, a library for dynamic binary translation.
==1651== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==1651== Using valgrind-3.3.0-Debian, a dynamic binary instrumentation framework.
==1651== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==1651== For more details, rerun with: -v
==1651==
==1651==
==1651== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 33 from 1)
==1651== malloc/free: in use at exit: 0 bytes in 0 blocks.
==1651== malloc/free: 567 allocs, 567 frees, 147,342,292 bytes allocated.
==1651== For counts of detected errors, rerun with: -v
==1651== All heap blocks were freed -- no leaks are possible.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: