leaks

Search a process's memory for unreferenced malloc buffers

leaks pid | partial-executable-name [-nocontext] [-nostacks] [-exclude symbol] [-trace address]

Identifies leaked memory -- memory that the application has allocated, but has been lost and cannot be freed.
Examines a process's memory for values that may be pointers to malloc-allocated buffers.

  • Any buffer reachable from a pointer in writable global memory (e.g., __DATA segments), a register, or on the stack is assumed to be memory in use.
  • Any buffer reachable from a pointer in a reachable malloc-allocated buffer is also assumed to be in use.
    Buffers which are not reachable are leaks; the buffers could never be freed because no pointer exists in memory to the buffer, and thus free() could never be called for these buffers. Such buffers waste memory; removing them can reduce swapping and memory usage. Leaks are particularly dangerous for long-running programs, for eventually the leaks could fill memory and cause the application to crash.

    For each leaked buffer , leaks outputs the address of the leaked memory and its size.
    If leaks can determine that the object is an instance of an Objective-C, CoreFoundation, or C++ class, or a CFType, it specifies the name and type of the class, and the binary image that implements the class. It then outputs a string or hexadecimal representation of the contents of the memory, unless -nocontext was specified.

    If the MallocStackLogging environment variable was set when the application was launched, leaks also outputs a stack trace describing where the buffer was allocated. OPTIONS
    -nocontext Do not print the string or hex representation of leaked memory. Although that information can be useful for recognizing the contents of the buffer and understanding why it might be leaked, it can provide overwhelming detail, and could expose confidential information.
    -nostacks Do not print backtraces of leaked blocks even if the target process has MallocStackLogging environment variable set.
    -exclude symbol Exclude leaked blocks whose backtraces include symbol. This option can be repeated for multiple symbols. Ignoring leaks that are allocated in libraries .
    -trace address Print chains of references from process 'roots' (e.g., global data, registers, or locations on stacks) to the given block. Useful for determining what is holding a buffer such that it has not been freed.

    ENVIRONMENT

    leaks may detect more leaks if the target process is run with the MallocScribble environment variable. If this variable is set when malloc blocks are deallocated they are filled with 0x55 bytes, overwriting data such as pointers remaining in those blocks. This reduces the number of false pointers remaining in the process memory.

    EXIT STATUS

    0 No leaks were detected.
    1 One or more leaks were detected.
    >1 An error occurred.

    SEE ALSO

    malloc(3), heap(1), malloc_history(1), stringdups(1), vmmap(1), DevToolsSecurity(1)

    The Xcode developer tools includes Instruments, a graphical application that can give information similar to that provided by leaks. The Allocations instrument graphically displays dynamic, real-time information about the object and memory use in an application, including backtraces of where the allocations occurred.

     
    > sudo leaks 1842
    Process:         mdworker [1842]
    Path:            /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/Metadata.framework/Versions/A/Support/mdworker
    Load Address:    0x105131000
    Identifier:      mdworker
    Version:         ???
    Code Type:       X86-64
    Parent Process:  launchd [1]
    
    Date/Time:       2016-11-22 16:56:18.464 -0500
    Launch Time:     2016-11-22 16:56:02.665 -0500
    OS Version:      Mac OS X 10.11.6 (15G1108)
    Report Version:  7
    Analysis Tool:   /Applications/Xcode.app/Contents/Developer/usr/bin/leaks
    Analysis Tool Version:  Xcode 8.1 (8B62)
    ----
    
    leaks Report Version:  2.0
    Process 1842: 2679 nodes malloced for 406 KB
    Process 1842: 1 leak for 192 total leaked bytes.
    Leak: 0x7fa8c0d03790  size=192  zone: DefaultMallocZone_0x1051c5000   OS_dispatch_source  ObjC  libdispatch.dylib
        0x7783b420 0x00007fff 0x00000001 0x00000000      ..w............
        0x89abcdef 0xffffffff 0xc0c26a00 0x00007fa8     .........j......
        0x00000000 0x00000000 0x00000000 0x00000000     ................
        0x00000000 0x00000000 0x00000000 0x00000000     ................
        0x00000000 0x00000000 0x00000000 0x00000000     ................
        0x00000001 0x00000000 0x00000000 0x00000000     ................
        0x00000000 0x00000000 0xffffffff 0xffffffff     ................
        0x0000001f 0x00000000 0x997195bc 0x00007fff     ..........q.....
        ...
    

     > sudo leaks -nocontext -nostacks 436 
    Password:
    Process:         sysmond [436]
    Path:            /usr/libexec/sysmond
    Load Address:    0x10685e000
    Identifier:      sysmond
    Version:         ???
    Code Type:       X86-64
    Parent Process:  launchd [1]
    
    Date/Time:       2016-11-22 16:33:33.958 -0500
    Launch Time:     2016-11-21 16:59:34.408 -0500
    OS Version:      Mac OS X 10.11.6 (15G1108)
    Report Version:  7
    Analysis Tool:   /Applications/Xcode.app/Contents/Developer/usr/bin/leaks
    Analysis Tool Version:  Xcode 8.1 (8B62)
    ----
    
    leaks Report Version:  2.0
    Process 436: 5758 nodes malloced for 426 KB
    Process 436: 0 leaks for 0 total leaked bytes