Finding Memory leaks in C++ on solaris Unix

There 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).
1234.cpumem
1234.leaks
2341.cpumem
2341.leaks

 
where
  • .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.

3 comments:

  1. we have downloaded and tried cpuleaks on our SunOS 5.10 Generic_150400-09 sun4u sparc SUNW,SPARC-Enterprise.
    Tried to run as group user (xyz) and hit the error..dtrace: failed to initialize dtrace: DTrace requires additional privileges. Took admin(root) help to overcome this by setting "usermod -K defaultpriv=basic,dtrace_proc,dtrace_user xyz"...still above listed error didn't dissolve.
    Ran cpuleaks tool as root... "cpuleaks pid,pid -st 2"...creates empty leak file.

    -rw-rw-r-- 1 xyzuser users 0 Jan 15 11:25 11092.leaks
    ...with error on stdout like..
    dtrace: failed to compile script /tmp/musg.d: line 7: probe description pid11092::__1c2n6FI_pv_:entry does not match any probes
    dtrace: failed to compile script /tmp/musg.d: line 7: probe description pid19881::__1c2n6FI_pv_:entry does not match any probe
    Please advise...I need help. Thanks

    ReplyDelete
    Replies
    1. The above reported problem is resolved after my DEVSsolaris box is rebooted...to take dtrace privilege chgs to take into effect. This was needed...as our server was zone solaris server. Thanks for this great tool.

      Delete
  2. Cool and that i have a nifty offer: Whole House Renovation Checklist Pdf home addition contractors

    ReplyDelete