Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


log-malloc-simple is pre-loadable library tracking all memory allocations of a program. It produces simple text trace output, that makes it easy to find leaks and also identify their origin.

The output of log-malloc-simple can be easily parsed by a log analyzer tool. A simple log-analyzer written in Java is also part of this package.

Changes compared to log-malloc2

log-malloc-simple is a much simplified version of log-malloc2. The simplifications and their reason:

  • The return value of all allocations is returned intact. This prevents some crashes that were caused by log-malloc2 due to lack of memory alignment returned by modified calloc method.
  • Only malloc_usable_size() is used to track the size of allocated memory.
  • Conditional compilation is removed. I have no chance to test all versions of the code. Only platforms with GNU backtrace and malloc_usable_size are supported.
  • Data aggregation of the logger is removed. It is delegated to a separate log-analyzer tool.


  • logging to file descriptor 1022 (if opened)
  • call stack backtrace (using GNU backtrace())
  • thread safe
  • fork is detected (PID is part of each log message) and allocations in the forked process are ignored (they caused noise in the logs)


  • When using glib, use G_SLICE=always-malloc environment variable value so that g_slice allocations are better trackable (in case of a leak there will be no false blame of a different component).
  • In some cases pthread_create call has a phantom free that frees memory block that was never allocated. I guess it can somehow call original malloc without going through the anchor functions.


  • malloc_usable_size() method - could be get rid of but we could not track the size of freed memory chunks. In case we analyze all the logs of the whole lifecycle of the program then it could be accepted.
  • /proc/self/exe, /proc/self/cwd


LD_PRELOAD=./ command args ... 1022>/tmp/program.log

or it is possible to send the logs to a pipe that is processed on the same computer:

LD_PRELOAD=./ command args ... 1022>/tmp/malloc.pipe

or it is possible to send the logs to TCP directly:

LD_PRELOAD=./ command args ... 1022>/dev/tcp/$HOSTNAME/$PORT

On the log processing computer a pipe and netcat can be used to direct the data into the log analyser tool.

Log analyzer tool

Standalone program written in Java. Usage:

  • Create a pipe that will transfer memory allocation log to the Java program: $ mkfifo /tmp/malloc.pipe
  • Start analyzer: $ java -jar analyzer.jar /tmp/malloc.pipe
  • Use console to command analyzer: stop/start collecting data, print or save current state to file
  • Start program to analyze: $ LD_PRELOAD=./ my_executable args ... 1022>/tmp/malloc.pipe
  • Optional: Create a snapshot of all active memory allocations by command snapshot 00.snapshot
  • Run test cases that should run without leaking memory.
  • Optional: Create a snapshot of all active memory allocations by command snapshot 01.snapshot
  • Optional: Execute the compare snapshots mode of the analyser to see comparison of the allocated blocks. (See below)
  • See the output of the analyzer for non-freed memory chunks.
  • Log output may also be directed to a static file and analysed offline.
  • Input of analyser may be redirected. This way it can be used in an automated process.

Building instructions

The maven way

git clone
git clone
cd log-malloc-simple
mvn package

The resulting, standalone jar, with all its dependencies packed in, will be located as follows: java/hu.qgears.analyzelogmalloc/target/analyzer.jar

Eclipse IDE

  • The analyser is written in Java and stored as an Eclipse project. (The project uses OSGI for dependency management but must be run as a standalone Java program.)
  • import the hu.qgears.analyzelogmalloc and hu.qgears.commons projects into an Eclipse workspace
  • hint: hu.qgears.commons dependency is located in the repo
  • create a Java app. run configuration for the Analyze class. Analyser may be used by launching inside Eclipse as a standalone applocation.
  • export the run configuration as an executable jar
  • TODO: implement MAVEN build

Log file format

# PID 5625
# EXE /usr/bin/gedit
# CWD /home/rizsi/github-qgears/log-malloc-simple/c


+ INIT 32 0x7f1a9b04e140
+ malloc 40 0xadf010


+ free 24 0x1db9360
  • Log stream begins with basic process information lines beginning with #
  • #MAPS ...: content of /proc/self/maps
  • Log entries start with a line beginning with '+' followed by an entry type name and two numbers (all separated by single space characters):
    • First is size in bytes as a decimal number
    • Second is memory address as a hexadecimal constant (eg. 0x1db9010) end with a line beginning with '-'
  • Log entry types are:
    • "INIT" - (size and address parameter is not important) means that the analyser tool is set up
    • "FINI" - (no size and address parameter) means that the process quit
    • "malloc", "calloc", "memalign", "posix_memalign", "valloc", "free": These methods have the same names in C
    • "realloc_free", "realloc_alloc": realloc calls result in two separate log entries one for free and one for allocation
  • Allocation log entries contain stack trace where the allocation related method was called from
  • Log entries are closed with a line starting with "-" character

Analysed data output

Analyser waits for commands on stdin:

  • off - turn analyzer off while the application is setting up (to spare CPU cycles when analysation is not required - eg initialization of the program)
  • on - turn analyzer on when the critical session is started (default is on)
  • (reset - clear all log entries cached by the analyzer)
  • print - print current allocation status (since last reset/on) to stdout
  • save - print current allocation status (since last reset/on) to file
  • snapshot - save all current stored allocations into a file (see compare below)

The analysed data output is in text format. After a short summary all not-freed allocations are listed. These entries are ordered and summarised by the identifier of the instruction (library+pointer) calling the allocation method. The textual output of the same program in different moments may be compared to each other using text comparing tools to find leaks. (A single call from each allocator calling instruction is printed as an example in the output but this does not mean that it is the only possible stack trace that calls this leaking method.):

Processing timespan in millis (since first log processed after reset, measured with currentTimeMillis): 9,331
Allocation balance (bytes, negative means leak): -353,560
Number of objects allocated in log session but not freed yet: 2714
Size of objects freed in log session but not allocated in log session (bytes): 2,888
Number of objects freed in session but not allocated in session: 59 without multiple frees: 50
Matching alloc/free pairs through the logging session (n, bytes): 4144 383,696

allocator: /usr/lib/x86_64-linux-gnu/[0x7ff6ccd50c6b]
	N:6 BYTES: 12,432
+ malloc 2072 0x1fb0f40



Compare snapshots

Command line: --compare <fileState2> --pipe <fileState1> --printAllIfContains pattern

What it does:

  • process both fileState1 and fileState1 normally into a separate model in memory
  • diff the the models in memory:
  • Number of objects, allocated bytes of objects by allocator identifier (the program line that calls to allocator) is counted for each allocator ** First same allocations are removed ** Freed and new allocations are diffed by size and number ** new allocations are part of the report
  • Output is ordered by the size of the increment (possible leak) by allocator identifier
  • --printAllIfContains (multiple instances are possible) if a pattern is present in the stack trace of the first example then all examples are written into the output. Useful in case we find a leak that starts like the one below and we want to see each distinct stack traces which end in this leaking function call. Then we add the --printAllIfContains g_malloc+0x29 parameter:
/lib/i386-linux-gnu/ [0x93737a39]
/lib/i386-linux-gnu/ [0x937501a9]


  • Based on previous works by Samuel Behan and Ivan Tikhonov - see comments in log-malloc-simple.c


  • LGPLv3 for C code (library)
  • GPLv3 for Java malloc log analyzer


pre-loadable library tracking all memory allocations of a program. Simplified version of log-malloc2






No releases published


No packages published