Finding Memory leaks in C++ on solaris UnixThere are lot of ways where one can identify memory leaks and lots of blogs and materieals
are available to help a designer to identify the same.
Some say using dbx and others say use mdb or gdb. I agree that all those tools will be useful, but it takes a lot of effort for understanding as well as putting it to work takes some time.
I have thought to break the legacy rules and do something new with the knowledge that
I have on various tools of solaris. So I thought of giving it a try to use tools like
dtrace, perl, ps command etc along with c++.
Putting this toll to action is an effort less job.
I am happy to have created a tool which I think would be very use for any solaris unix programmer.
This tool will give the list of all memory leaks of a process running in the background.
It uses dtrace to identify the memory allocations,
It uses the ps command to identify the memory used by a process.
It uses perl to filter and generate reports of meory leaks.
Main functionalities of this tool are:
- Generate a report which contains actual physical memory used by the process, Virtual memory used, % of cpu used and %memory used.
- Generate a report which contains all the memory leaks of the process due to inefficient coding techniques.
- Generate a separate report for each and every process.
I have tested this on:
SunOS 5.10 Generic_147440-27 sun4u sparc SUNW,SPARC-Enterprise
Thanks to Frederic, I have taken part of his logic written in his blog:
You can download the zip file from the link here:
The file name is cpuleaks.gz.
Unzip this file using gzip -d cpuleaks.gz and you can place it on any server and give the execute permissions and run this process as a root user(dtrace can be run only using root access on my server. If dtrace can be used by a user other than root, well and fine).
Execution of the process should be as below:
cpuleaks <comma separated pid list> -st <time interval for mem usage>
ex: cpuleaks 1234,2341 -st 2
This above command will give 4 files as output after either the processes 1234,2341 complete or if you press CTRL +C(an interrupt signal).
- .leaks report will have the list of all memory leaks
- .cpumem report will have the memory usage of the process for every interval passed as an argument to the cpuleaks binary.