Monday, August 10, 2009

Finding Memory Leaks in Solaris

I work on Unix platform. I used to think that to find memory leaks/corruptions, we have valgrind in Linux, but there is no such tool for Solaris. While browsing, I came across many tools available for Solaris also and here I am mentioning about one of such tools which is simple and straight. dbx - is a utility for source-level debugging and execution of programs written in C++, ANSI C, Fortran 77, Fortran 95, and Java programming languages.
Here is step by step procedure to use it.
1)Here is my sample program
/* To Compile - cc prog.c */
int main()
{
void fun();
fun();
return 0;
}

void fun(void)
{
int *p;
p = (int *)malloc(sizeof(int));
return;
}


2) Compile this program
$> cc ex1.c

3) Start executable using dbx
$> dbx ./a.out

4) set memory checks option on check -memuse
(dbx) check -memuse

5) run the executable
(dbx) run

The output will be something like this,
=============================================================
Actual leaks report (actual leaks: 1 total size: 4 bytes)

Total Num of Leaked Allocation call stack
Size Blocks Block
Address
========== ====== =========== =======================================
4 1 0x8060c20 fun < main


Possible leaks report (possible leaks: 0 total size: 0 bytes)


Checking for memory use...

Blocks in use report (blocks in use: 0 total size: 0 bytes)



execution completed, exit code is 0
====================================================================
Here, actual leaks is 1 which is 4 bytes, at 0x8060c20 , reason is fun < main. So we can know that fun() has a memory leak.

dbx tells only function where memory leaks are there, but it will not give exact Line Number.


@kova

No comments: