Code Monkey home page Code Monkey logo

heapusage's Introduction

Heapusage

Linux + Mac
Build status

Heapusage is a light-weight tool for finding memory leaks in Linux and macOS applications. It provides a small but important subset of Valgrind's memcheck functionality, and can be a useful alternative to it for debugging memory leaks in certain scenarios such as:

  • Large complex applications which cannot be run at Valgrind slowdown speed
  • Embedded systems with CPU architectures not supported by Valgrind

Like Valgrind, it is recommended to run Heapusage on a debug build of the application to be analyzed.

While Heapusage has less performance impact than Valgrind, its analysis is less precise. It may report leaks originating from system libraries (e.g. libc functions like print()) that might be free'd when the system library is being cleaned up.

Example Usage

$ heapusage ./ex001
==22648== Heapusage - https://github.com/d99kris/heapusage
==22648== 
==22648== HEAP SUMMARY:
==22648==     in use at exit: 12221 bytes in 4 blocks
==22648==   total heap usage: 5 allocs, 1 frees, 13332 bytes allocated
==22648== 
==22648== 6666 bytes in 3 block(s) are lost, originally allocated at:
==22648==    at 0x00007fdca672199d: malloc + 49
==22648==    at 0x000000000040080d: main + 55
==22648==    at 0x00007fdca6376830: __libc_start_main + 240
==22648==    at 0x0000000000400709: _start + 41
==22648== 
==22648== 5555 bytes in 1 block(s) are lost, originally allocated at:
==22648==    at 0x00007fdca672199d: malloc + 49
==22648==    at 0x00000000004007e8: main + 18
==22648==    at 0x00007fdca6376830: __libc_start_main + 240
==22648==    at 0x0000000000400709: _start + 41
==22648== 
==22648== LEAK SUMMARY:
==22648==    definitely lost: 12221 bytes in 4 blocks
==22648== 

Supported Platforms

Heapusage is primarily developed and tested on Linux, but basic functionality should work in macOS / OS X as well. Current version has been tested on:

  • OS X El Capitan 10.11
  • Ubuntu 16.04 LTS
  • (Raspbian / Raspberry Pi 3 - based on fix for issue #1)

Limitation: On macOS / OS X this tool relies on code injection using DYLD_INSERT_LIBRARIES, which generally does not work with third-party applications in a standard system. Using it on (your own) applications built from source should work fine though.

Installation

Pre-requisites (Ubuntu):

sudo apt install git cmake build-essential

Download the source code:

git clone https://github.com/d99kris/heapusage && cd heapusage

Generate Makefile and build:

mkdir -p build && cd build && cmake .. && make -s

Optionally install in system:

sudo make install

Usage

General usage syntax:

heapusage [-d] [-f] [-m minsize] [-n] [-o path] PROG [ARGS..]
heapusage --help
heapusage --version

Options:

-d     debug mode, running program through debugger

-f     check for invalid free's (experimental)

-m <minsize>
       minimum leak in bytes for detailed reporting

-n     no symbol lookup (faster)

-o <path>
       write output to specified file path, instead of stderr

PROG   program to run and analyze

[ARGS] optional arguments to the program

--help display this help and exit

--version
       output version information and exit

Example running heapusage with test program 'ex001':

heapusage ./ex001

Output Format

Example output:

==22648== Heapusage - https://github.com/d99kris/heapusage
==22648== 
==22648== HEAP SUMMARY:
==22648==     in use at exit: 12221 bytes in 4 blocks
==22648==   total heap usage: 5 allocs, 1 frees, 13332 bytes allocated
==22648== 
==22648== 6666 bytes in 3 block(s) are lost, originally allocated at:
==22648==    at 0x00007fdca672199d: malloc + 49
==22648==    at 0x000000000040080d: main + 55
==22648==    at 0x00007fdca6376830: __libc_start_main + 240
==22648==    at 0x0000000000400709: _start + 41
==22648== 
==22648== 5555 bytes in 1 block(s) are lost, originally allocated at:
==22648==    at 0x00007fdca672199d: malloc + 49
==22648==    at 0x00000000004007e8: main + 18
==22648==    at 0x00007fdca6376830: __libc_start_main + 240
==22648==    at 0x0000000000400709: _start + 41
==22648== 
==22648== LEAK SUMMARY:
==22648==    definitely lost: 12221 bytes in 4 blocks
==22648== 

The corresponding file and line number of the stacktrace addresses can be determined using addr2line on Linux (the equivalent tool for macOS is atos):

$ addr2line -f -e ./ex001 0x000000000040080d
main
tests/ex001.c:29

Technical Details

Heapusage intercepts calls to malloc/free/etc and logs each memory allocation and free. At process termination it outputs logging of all allocations not free'd.

Alternatives

There are many memory leak checkers available for Linux and macOS, for example:

  • LeakSanitizer
  • Mtrace
  • Valgrind

License

Heapusage is distributed under the BSD 3-Clause license. See LICENSE file.

Keywords

linux, macos, os x, heap usage, finding memory leaks, alternative to valgrind.

heapusage's People

Contributors

d99kris avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.