Skip to content

Malloc Trace

Ralf Schmelter edited this page Sep 30, 2025 · 1 revision

Quickstart Guide

The SapMachine malloc trace is a tool for either finding memory leaks in native memory or getting an overview of the native memory usage (how much is allocated by whom). It tracks all memory allocations via malloc(), calloc(), realloc() and similar methods in the whole JVM process. Optionally by tracking free() calls too, it knows the currently live memory and the methods which allocated it.

To be able to use the trace, the VM has to be started with the -XX:+UseMallocHooks flag. Then the malloc trace can be enabled via the following jcmd (<pid> is the process id of the VM process):

jcmd <pid> MallocTrace.enable

Now every call to malloc, calloc and so on is intercepted and a stack trace is taken. To view current statistic about the tracked allocations, the following jcmd can be used:

jcmd <pid> MallocTrace.dump

This returns the 10 stacks with the biggest total allocation size. To get more than the top 10 stacks, the '-max-entries' option can be used:

jcmd <pid> MallocTrace.dump -max-entries=50

The malloc trace can be disabled via:

jcmd <pid> MallocTrace.disable

If you only want to see the memory currently still in use, you must enable the malloc trace with the -track-free flag. Note that this uses more additional memory and costs some additional performance.

To get a list of all the jcmd options supported by the VM use

jcmd <pid> help MallocTrace.enable.

jcmd <pid> help MallocTrace.dump.

For more information see this page.

The old, Linux only malloc trace will be discontinued.

Clone this wiki locally