X

Improve application security, performance, and scalability

  • February 9, 2015

Finding Memory Leaks in a Server or a Long Running Program

Raj Prakash
Architect
Here is a tip. You may know that you can call Studio Code Analyzer Discover functions from your program to get memory leak and memory allocation information. For example, you can call discover_report_unreported_leaks() to see the leaks that Discover knows about at any given point in your program. The complete list of functions is here.



If you have a long running program or a server that never exits, you can call these functions using the debugger, dbx, at any time even if you haven't put the calls in your code. The program, of course, must be Discover'ed earlier at least in the lite mode, option -l. Note that dbx can attach to a running program.



Below is an example. a.out is a long running program with two processes with one leak each. I wrote a script, rl, that has the commands to ask the program to report unreported leaks. You give it a process id. You can use the script again and again to see new leaks, if any.
    % cat rl
#!/bin/sh
dbx - $1 > /dev/null 2>&1 << END
call __discover_report_unreported_leaks()
exit
END

Discover and run the program
    % discover -l -w - a.out
% a.out
8252: Parent allocation 64
8253: Child allocation 32

In another window, I do
    % rl 8252 

The program reports
    ******** discover_report_unreported_leaks() Report ********
1 allocation at 1 location left on the heap with a total size of 64 bytes
LEAK 1: 1 allocation with total size of 64 bytes
main() + 0x1e
14:
15: if (child > 0) {
16:
17:=> void *p = malloc(64);
18: printf("%jd: Parent allocation 64\n", (intmax_t)getpid());
19: p = 0;
20: for (int j=0; j < 1000; j++) sleep(1);
_start() + 0x66
**********************************************************

Now for the child process
    % rl 8253

The program reports
    ******** discover_report_unreported_leaks() Report ********
1 allocation at 1 location left on the heap with a total size of 32 bytes
LEAK 1: 1 allocation with total size of 32 bytes
main() + 0x80
21: }
22:
23: else {
24:=> void *p = malloc(32);
25: printf("%jd: Child allocation 32\n", (intmax_t)getpid());
26: p = 0;
27: for (int j=0; j < 1000; j++) sleep(1);
_start() + 0x66
**********************************************************

Join the discussion

Comments ( 1 )
  • guest Monday, February 9, 2015

    Welcome to blogland! May you have many joyous postings and have a terrific time enlightening the rest of us


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha
Oracle

Integrated Cloud Applications & Platform Services