Creation Zone

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Sunday, 13 March 2005

Sun Studio: Investigating memory leaks with Collector/Analyzer

Posted on 14:29 by Unknown
Sun Studio compiler collection bundles a set of tools for collecting and analyzing performance data from an application. Collector & Analyzer, a pair of tools from Sun Studio suite that we use to collect and analyze the performance data. Both tools can be used from the command line or from a graphical user interface.

These two tools help answering the following questions:
  • How much of the available resources does the program consume?
  • Which functions or load objects are consuming the most resources?
  • Which source lines and instructions are responsible for resource consumption?
  • How did the program arrive at this point in the execution?
  • Which resources are being consumed by a function or load object?
But this blog post mainly concentrates on finding memory leaks from the following simple C program

% more mem.c
#include <stdlib.h>
#include <stdio.h>

void allocate() {
int *x;
char *y;

x = (int *) malloc(sizeof(int) * 100);
y = (char *) malloc (sizeof(char) * 200);

printf("\nAddress of x = %u, y = %u", x, y);

x = (int *) malloc (sizeof(int) * 25);
y = (char *) malloc (sizeof(char) * 25);

printf("\nNew address of x = %u, y = %u\n", x, y);
free (y);
}

int main() {
allocate();
return (0);
}

From the code, it is clear that the program is requesting memory allocations four times (a total of 725 bytes); but releasing only 25 bytes from virtual memory. The following steps explain how to find this 700 byte leak with Collector/Analyzer.

  1. Prepare the program for data collection

    To see source code in annotated source and disassembly, and source lines in the line analysis, the source file must be compiled with -g compiler option to generate debug symbol information
    % CC -g -o mem mem.c

  2. Collect the data

    We can collect run-time data that is useful for later analysis by running the executable under collect utility. To see the entire list of arguments that collect accepts, type collect without any arguments on the command line. Since we were interested in memory leaks, we have to turn the heap tracing on with -H option. (By default, heap tracing is off). Also we have to supply an experiment name to hold the data with -o option
    % which collect
    /opt/SUNWspro/prod/bin/collect

    % collect -H on -o mem.er mem
    Creating experiment database mem.er ...

    Address of x = 134743584, y = 134743992
    New address of x = 134744200, y = 134614672


    It creates the mem.er experiment and archives the binary files describing each load object referenced in the loadobjects file
  3. Finally invoke the performance analyzer, er_print (command line tool) or analyzer (graphical tool)
    % er_print mem.er
    (er_print) source allocate
    Source file: ./mem.c
    Object file: ./mem
    Load Object: ./mem

    Incl. Incl. Excl. Incl.
    Bytes Leaks User CPU User CPU
    Leaked sec. sec.
    1. #include <stdlib.h>
    2. #include <stdio.h>
    3.
    0 0 0. 0. 4. void allocate() {

    0 0 0. 0. 5. int *x;
    0 0 0. 0. 6. char *y;
    7.
    400 1 0. 0. 8. x = (int *) malloc(sizeof(int) * 100);
    200 1 0. 0. 9. y = (char *) malloc (sizeof(char) * 200);
    10.
    0 0 0. 0. 11. printf("\nAddress of x = %u, y = %u", x, y);
    12.
    100 1 0. 0. 13. x = (int *) malloc (sizeof(int) * 25);
    0 0 0. 0. 14. y = (char *) malloc (sizeof(char) * 25);
    15.
    0 0 0. 0. 16. printf("\nNew address of x = %u, y = %u\n", x, y);
    0 0 0. 0. 17. free (y);
    0 0 0. 0. 18. }
    19.
    0 0 0. 0. 20. int main() {

    ## 700 3 0. 0. 21. allocate();
    0 0 0. 0. 22. return (0);
    0 0 0. 0. 23. }


    source allocate shows the annotated source with line numbers and number of bytes leaked from each line since we collected the data with heap tracing on (otherwise it shows the total CPU time spent in each line, by default)

    From the annotated source, we can see that 700 bytes were leaked after the execution of line:
    ## 700     3      0.        0.              21.         allocate();
    . And from the source of allocate(), the following lines were the culprits:

    400 1 0. 0. 8. x = (int *) malloc(sizeof(int) * 100);
    200 1 0. 0. 9. y = (char *) malloc (sizeof(char) * 200);
    100 1 0. 0. 13. x = (int *) malloc (sizeof(int) * 25);
    ____________

    0 0 0. 0. 14. y = (char *) malloc (sizeof(char) * 25);
    statement was clean, since we have 0 0 0. 0. 17. free (y); at the end of function allocate().

    We can also get much useful information with the help of leaks and allocs commands that relate to memory allocations and deallocations. Brief description of those commands are as follows:
    • leaks

      Display a list of memory leaks, aggregated by common call stack. Each entry presents the total number of leaks and the total bytes leaked for the given call stack. The list is sorted by the number of bytes leaked.

    • allocs

      Display a list of memory allocations, aggregated by common call stack. Each entry presents the number of allocations and the total bytes allocated for the given call stack. The list is sorted by the number of bytes allocated.
Suggested Reading:
  • Sun Studio Performance Analyzer
Email ThisBlogThis!Share to XShare to Facebook
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • *nix: Workaround to cannot find zipfile directory in one of file.zip or file.zip.zip ..
    Symptom: You are trying to extract the archived files off of a huge (any file with size > 2 GB or 4GB, depending on the OS) ZIP file with...
  • JDS: Installing Sun Java Desktop System 2.0
    This document will guide you through the process of installing JDS 2.0 on a PC from integrated CDROM images Requirements I...
  • Linux: Installing Source RPM (SRPM) package
    RPM stands for RedHat Package Manager. RPM is a system for installing and managing software & most common software package manager used ...
  • Solaris: malloc Vs mtmalloc
    Performance of Single Vs Multi-threaded application Memory allocation performance in single and multithreaded environments is an important a...
  • C/C++: Printing Stack Trace with printstack() on Solaris
    libc on Solaris 9 and later, provides a useful function called printstack , to print a symbolic stack trace to the specified file descripto...
  • Installing MySQL 5.0.51b from the Source Code on Sun Solaris
    Building and installing the MySQL server from the source code is relatively very easy when compared to many other OSS applications. At least...
  • Oracle Apps on T2000: ORA-04020 during Autoinvoice
    The goal of this brief blog post is to provide a quick solution to all Sun-Oracle customers who may run into a deadlock when a handful of th...
  • Siebel Connection Broker Load Balancing Algorithm
    Siebel server architecture supports spawning multiple application object manager processes. The Siebel Connection Broker, SCBroker, tries to...
  • 64-bit dbx: internal error: signal SIGBUS (invalid address alignment)
    The other day I was chasing some lock contention issue with a 64-bit application running on Solaris 10 Update 1; and stumbled with an unexpe...
  • Oracle 10gR2/Solaris x64: Fixing ORA-20000: Oracle Text errors
    First, some facts: * Oracle Applications 11.5.10 (aka E-Business Suite 11 i ) database is now supported on Solaris 10 for x86-64 architectur...

Categories

  • 80s music playlist
  • bandwidth iperf network solaris
  • best
  • black friday
  • breakdown database groups locality oracle pmap sga solaris
  • buy
  • deal
  • ebiz ebs hrms oracle payroll
  • emca oracle rdbms database ORA-01034
  • friday
  • Garmin
  • generic+discussion software installer
  • GPS
  • how-to solaris mmap
  • impdp ora-01089 oracle rdbms solaris tips upgrade workarounds zombie
  • Magellan
  • music
  • Navigation
  • OATS Oracle
  • Oracle Business+Intelligence Analytics Solaris SPARC T4
  • oracle database flashback FDA
  • Oracle Database RDBMS Redo Flash+Storage
  • oracle database solaris
  • oracle database solaris resource manager virtualization consolidation
  • Oracle EBS E-Business+Suite SPARC SuperCluster Optimized+Solution
  • Oracle EBS E-Business+Suite Workaround Tip
  • oracle lob bfile blob securefile rdbms database tips performance clob
  • oracle obiee analytics presentation+services
  • Oracle OID LDAP ADS
  • Oracle OID LDAP SPARC T5 T5-2 Benchmark
  • oracle pls-00201 dbms_system
  • oracle siebel CRM SCBroker load+balancing
  • Oracle Siebel Sun SPARC T4 Benchmark
  • Oracle Siebel Sun SPARC T5 Benchmark T5-2
  • Oracle Solaris
  • Oracle Solaris Database RDBMS Redo Flash F40 AWR
  • oracle solaris rpc statd RPC troubleshooting
  • oracle solaris svm solaris+volume+manager
  • Oracle Solaris Tips
  • oracle+solaris
  • RDC
  • sale
  • Smartphone Samsung Galaxy S2 Phone+Shutter Tip Android ICS
  • solaris oracle database fmw weblogic java dfw
  • SuperCluster Oracle Database RDBMS RAC Solaris Zones
  • tee
  • thanksgiving sale
  • tips
  • TomTom
  • windows

Blog Archive

  • ►  2013 (16)
    • ►  December (3)
    • ►  November (2)
    • ►  October (1)
    • ►  September (1)
    • ►  August (1)
    • ►  July (1)
    • ►  June (1)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  February (2)
    • ►  January (1)
  • ►  2012 (14)
    • ►  December (1)
    • ►  November (1)
    • ►  October (1)
    • ►  September (1)
    • ►  August (1)
    • ►  July (1)
    • ►  June (2)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  February (1)
    • ►  January (2)
  • ►  2011 (15)
    • ►  December (2)
    • ►  November (1)
    • ►  October (2)
    • ►  September (1)
    • ►  August (2)
    • ►  July (1)
    • ►  May (2)
    • ►  April (1)
    • ►  March (1)
    • ►  February (1)
    • ►  January (1)
  • ►  2010 (19)
    • ►  December (3)
    • ►  November (1)
    • ►  October (2)
    • ►  September (1)
    • ►  August (1)
    • ►  July (1)
    • ►  June (1)
    • ►  May (5)
    • ►  April (1)
    • ►  March (1)
    • ►  February (1)
    • ►  January (1)
  • ►  2009 (25)
    • ►  December (1)
    • ►  November (2)
    • ►  October (1)
    • ►  September (1)
    • ►  August (2)
    • ►  July (2)
    • ►  June (1)
    • ►  May (2)
    • ►  April (3)
    • ►  March (1)
    • ►  February (5)
    • ►  January (4)
  • ►  2008 (34)
    • ►  December (2)
    • ►  November (2)
    • ►  October (2)
    • ►  September (1)
    • ►  August (4)
    • ►  July (2)
    • ►  June (3)
    • ►  May (3)
    • ►  April (2)
    • ►  March (5)
    • ►  February (4)
    • ►  January (4)
  • ►  2007 (33)
    • ►  December (2)
    • ►  November (4)
    • ►  October (2)
    • ►  September (5)
    • ►  August (3)
    • ►  June (2)
    • ►  May (3)
    • ►  April (5)
    • ►  March (3)
    • ►  February (1)
    • ►  January (3)
  • ►  2006 (40)
    • ►  December (2)
    • ►  November (6)
    • ►  October (2)
    • ►  September (2)
    • ►  August (1)
    • ►  July (2)
    • ►  June (2)
    • ►  May (4)
    • ►  April (5)
    • ►  March (5)
    • ►  February (3)
    • ►  January (6)
  • ▼  2005 (72)
    • ►  December (5)
    • ►  November (2)
    • ►  October (6)
    • ►  September (5)
    • ►  August (5)
    • ►  July (10)
    • ►  June (8)
    • ►  May (9)
    • ►  April (6)
    • ▼  March (6)
      • Mixed-Programming and External Linkage
      • Sun Studio: Investigating memory leaks with Collec...
      • Solaris 10: Installing MPlayer
      • Solaris: Setting up a DHCP client
      • UNIX/Linux: Activating Comcast High Speed Internet...
      • Obfuscated "C" code
    • ►  February (5)
    • ►  January (5)
  • ►  2004 (36)
    • ►  December (1)
    • ►  November (5)
    • ►  October (12)
    • ►  September (18)
Powered by Blogger.

About Me

Unknown
View my complete profile