Skip navigation

I was facing memory leak issue with an application (application contains 12 .c file and each .c file contains approx 4000 to 7000 lines of code), So one solution is to debug the application with ‘valgrind’ and find out memory leak problem, but I was wondering about different solution than ‘valgrind’ tool. Since I wanted to fix the memory leak issue
with different method so I started exploring the area.

I have used the ‘top’ command in Linux and I know it gives complete snapshot of a System at given interval time. So I started looking for parameter which gives me physical(actual) memory used by the process/application, and I come to know about Resident set size (From wikipedia, “Resident set size is is the portion of a process’s memory that is held in RAM”) Now all I have to write and application that print the ‘RSS’ value.

Now that gives a birth to ‘Memwatch’ ( ‘Memwatch’ print total memory and actual physical memory usage of a process at certain interval.

So to trace the memory leak in application, I start the application with gdb and holds a break point at ‘main’ and I started execution function by function, while step over to the function, I constantly monitor the ‘Memwatch’ output and at one point I found that significantly memory has increase, so I debug that function and found that huge memory leakage was in the function (because usage of strdup and malloc without free implementation).

In short this is another short implementation of ‘top’ command but you can use the function ‘CheckUsage’ in your application and call it before and after the function and it will print the memory usage at begin and end of function, but this is lazy and another way to find out. Another solution is to achieve the same thing is with the mix of ‘top’, ‘awk’ and other Linux command.

‘Resident Set Size’ value help me to find out the memory leak and I wrote a small application on top of that, and I decided to share it with the world.

If you like this post, do ping me on Twitter at @suresh_kanzaria

Happy Hacking !




Leave a Reply

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

You are commenting using your 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: