• Stars
    star
    2,954
  • Rank 14,692 (Top 0.3 %)
  • Language
    C++
  • Created over 8 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A heap memory profiler for Linux

heaptrack - a heap memory profiler for Linux

heaptrack_gui summary page

Heaptrack traces all memory allocations and annotates these events with stack traces. Dedicated analysis tools then allow you to interpret the heap memory profile to:

  • find hotspots that need to be optimized to reduce the memory footprint of your application
  • find memory leaks, i.e. locations that allocate memory which is never deallocated
  • find allocation hotspots, i.e. code locations that trigger a lot of memory allocation calls
  • find temporary allocations, which are allocations that are directly followed by their deallocation

Using heaptrack

The recommended way is to launch your application and start tracing from the beginning:

heaptrack <your application and its parameters>

heaptrack output will be written to "/tmp/heaptrack.APP.PID.gz"
starting application, this might take some time...

...

heaptrack stats:
    allocations:            65
    leaked allocations:     60
    temporary allocations:  1

Heaptrack finished! Now run the following to investigate the data:

    heaptrack_gui "/tmp/heaptrack.APP.PID.gz"

Alternatively, you can attach to an already running process:

heaptrack --pid $(pidof <your application>)

heaptrack output will be written to "/tmp/heaptrack.APP.PID.gz"
injecting heaptrack into application via GDB, this might take some time...
injection finished

...

Heaptrack finished! Now run the following to investigate the data:

    heaptrack_gui "/tmp/heaptrack.APP.PID.gz"

Building heaptrack

Heaptrack is split into two parts: The data collector, i.e. heaptrack itself, and the analyzer GUI called heaptrack_gui. The following summarizes the dependencies for these two parts as they can be build independently. You will find corresponding development packages on all major distributions for these dependencies.

On an embedded device or older Linux distribution, you will only want to build heaptrack. The data can then be analyzed on a different machine with a more modern Linux distribution that has access to the required GUI dependencies.

If you need help with building, deploying or using heaptrack, you can contact KDAB for commercial support: https://www.kdab.com/software-services/workshops/profiling-workshops/

Shared dependencies

Both parts require the following tools and libraries:

  • cmake 2.8.9 or higher
  • a C++11 enabled compiler like g++ or clang++
  • zlib
  • optionally: zstd for faster (de)compression
  • libdl
  • pthread
  • libc

heaptrack dependencies

The heaptrack data collector and the simplistic heaptrack_print analyzer depend on the following libraries:

  • boost 1.41 or higher: iostreams, program_options
  • libunwind

For runtime-attaching, you will need gdb installed.

heaptrack_gui dependencies

The graphical user interface to interpret and analyze the data collected by heaptrack depends on Qt 5 and some KDE libraries:

  • extra-cmake-modules
  • Qt 5.2 or higher: Core, Widgets
  • KDE Frameworks 5: CoreAddons, I18n, ItemModels, ThreadWeaver, ConfigWidgets, KIO, IconThemes

When any of these dependencies is missing, heaptrack_gui will not be build. Optionally, install the following dependencies to get additional features in the GUI:

  • KDiagram: KChart (for chart visualizations)

Compiling

Run the following commands to compile heaptrack. Do pay attention to the output of the CMake command, as it will tell you about missing dependencies!

cd heaptrack # i.e. the source folder
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release .. # look for messages about missing dependencies!
make -j$(nproc)

Compile heaptrack_gui on macOS using homebrew

heaptrack_print and heaptrack_gui can be built on platforms other than Linux, using the dependencies mentioned above. On macOS the dependencies can be installed easily using homebrew and the KDE homebrew tap.

brew install qt@5

# prepare tap
brew tap kde-mac/kde https://invent.kde.org/packaging/homebrew-kde.git
"$(brew --repo kde-mac/kde)/tools/do-caveats.sh"

# install dependencies
brew install kde-mac/kde/kf5-kcoreaddons kde-mac/kde/kf5-kitemmodels kde-mac/kde/kf5-kconfigwidgets \
             kde-mac/kde/kf5-kio kde-mac/kde/kdiagram \
             extra-cmake-modules ki18n threadweaver \
             boost zstd gettext

# run manual steps as printed by brew
ln -sfv "$(brew --prefix)/share/kf5" "$HOME/Library/Application Support"
ln -sfv "$(brew --prefix)/share/knotifications5" "$HOME/Library/Application Support"
ln -sfv "$(brew --prefix)/share/kservices5" "$HOME/Library/Application Support"
ln -sfv "$(brew --prefix)/share/kservicetypes5" "$HOME/Library/Application Support"

To compile make sure to use Qt from homebrew and to have gettext in the path:

cd heaptrack # i.e. the source folder
mkdir build
cd build
CMAKE_PREFIX_PATH=/opt/homebrew/opt/qt@5 PATH=$PATH:/opt/homebrew/opt/gettext/bin cmake ..
cmake -DCMAKE_BUILD_TYPE=Release .. # look for messages about missing dependencies!
make heaptrack_gui heaptrack_print

Interpreting the heap profile

Heaptrack generates data files that are impossible to analyze for a human. Instead, you need to use either heaptrack_print or heaptrack_gui to interpret the results.

heaptrack_gui

heaptrack_gui flamegraph page

heaptrack_gui allocations chart page

The highly recommended way to analyze a heap profile is by using the heaptrack_gui tool. It depends on Qt 5 and KF 5 to graphically visualize the recorded data. It features:

  • a summary page of the data
  • bottom-up and top-down tree views of the code locations that allocated memory with their aggregated cost and stack traces
  • flame graph visualization
  • graphs of allocation costs over time

heaptrack_print

The heaptrack_print tool is a command line application with minimal dependencies. It takes the heap profile, analyzes it, and prints the results in ASCII format to the command line.

In its most simple form, you can use it like this:

heaptrack_print heaptrack.APP.PID.gz | less

By default, the report will contain three sections:

MOST CALLS TO ALLOCATION FUNCTIONS
PEAK MEMORY CONSUMERS
MOST TEMPORARY ALLOCATIONS

Each section then lists the top ten hotspots, i.e. code locations that triggered e.g. the most memory allocations.

Have a look at heaptrack_print --help for changing the output format and other options.

Note that you can use this tool to convert a heaptrack data file to the Massif data format. You can generate a collapsed stack report for consumption by flamegraph.pl.

Comparison to Valgrind's massif

The idea to build heaptrack was born out of the pain in working with Valgrind's massif. Valgrind comes with a huge overhead in both memory and time, which sometimes prevent you from running it on larger real-world applications. Most of what Valgrind does is not needed for a simple heap profiler.

Advantages of heaptrack over massif

  • speed and memory overhead

    Multi-threaded applications are not serialized when you trace them with heaptrack and even for single-threaded applications the overhead in both time and memory is significantly lower. Most notably, you only pay a price when you allocate memory -- time-intensive CPU calculations are not slowed down at all, contrary to what happens in Valgrind.

  • more data

    Valgrind's massif aggregates data before writing the report. This step loses a lot of useful information. Most notably, you are not longer able to find out how often memory was allocated, or where temporary allocations are triggered. Heaptrack does not aggregate the data until you interpret it, which allows for more useful insights into your allocation patterns.

Advantages of massif over heaptrack

  • ability to profile page allocations as heap

    This allows you to heap-profile applications that use pool allocators that circumvent malloc & friends. Heaptrack can in principle also profile such applications, but it requires code changes to annotate the memory pool implementation.

  • ability to profile stack allocations

    This is inherently impossible to implement efficiently in heaptrack as far as I know.

Contributing to heaptrack

As a FOSS project, we welcome contributions of any form. You can help improve the project by:

When submitting bug reports, you can anonymize your data with the tools/anonymize script:

tools/anonymize heaptrack.APP.PID.gz heaptrack.bug_report_data.gz

Known bugs and limitations

Issues with old gold linker

Libunwind may produce bogus backtraces when unwinding from code linked with old versions of the gold linker. In such cases, recording with heaptrack seems to work and produces data files. But parsing these data files with heaptrack_gui will often lead to out-of-memory crashes. Looking at the data with heaptrack_print, one will see garbage backtraces that are completely broken.

If you encounter such issues, try to relink your application and also libunwind with ld.bfd instead of ld.gold. You can see if you are affected by running the libunwind unit tests via make check. But do note that you need to relink your application too, not only libunwind.

More Repositories

1

krita

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
5,850
star
2

ghostwriter

Text editor for Markdown
C++
4,121
star
3

kdenlive

Free and open source video editor, based on MLT Framework and KDE Frameworks 5
C++
2,439
star
4

kdeconnect-kde

Multi-platform app that allows your devices to communicate
C++
2,171
star
5

latte-dock

Replacement dock for Plasma desktops, providing an elegant and intuitive experience for your tasks and plasmoids
C++
1,474
star
6

rust-qt-binding-generator

Generate bindings to use Rust code in Qt and QML
Rust
788
star
7

kdeconnect-android

Native Android port of the KDE Connect Qt app
Java
774
star
8

plasma-desktop

Plasma for the Desktop
C++
692
star
9

okular

KDE document viewer
C++
632
star
10

clazy

Qt-oriented static code analyzer based on the Clang framework
C++
609
star
11

kate

Modern text editor built on the KDE Frameworks and Qt
C++
558
star
12

digikam

digiKam is an advanced open-source digital photo management application that runs on Linux, Windows, and MacOS. The application provides a comprehensive set of tools for importing, managing, editing, and sharing photos and raw files.
C++
507
star
13

kdevelop

Cross-platform IDE for C, C++, Python, QML/JavaScript and PHP
C++
434
star
14

kwin

Easy to use, but flexible, X Window Manager and Wayland Compositor
C++
427
star
15

konsole

Terminal emulator by KDE
C++
418
star
16

dolphin

File manager by KDE
C++
400
star
17

kdiff3

Utility for comparing and merging files and directories
C++
332
star
18

yakuake

Drop-down terminal emulator based on Konsole technologies
C++
330
star
19

kcachegrind

GUI to profilers such as Valgrind
C++
326
star
20

falkon

Cross-platform Qt-based web browser
C++
324
star
21

kdeconnect-ios

Native iOS port of KDE Connect
Swift
313
star
22

snoretoast

Command-line application capable of creating Windows Toast notifications
C++
280
star
23

massif-visualizer

Visualizer for Valgrind Massif data files
C++
267
star
24

elisa

Simple music player aiming to provide a nice experience for its users
C++
238
star
25

breeze-icons

Breeze icon theme.
C++
217
star
26

kirigami

A QtQuick based components set
QML
208
star
27

breeze

Artwork, styles and assets for the Breeze visual style for the Plasma Desktop
C++
192
star
28

plasma-workspace

Various components needed to run a Plasma-based environment
C++
191
star
29

kstars

Desktop Planetarium
C++
191
star
30

spectacle

Screenshot capture utility
C++
179
star
31

labplot

LabPlot is a FREE, open source and cross-platform Data Visualization and Analysis software accessible to everyone.
C++
177
star
32

amarok

Powerful music player that lets you rediscover your music
C++
153
star
33

kube

Modern groupware client based on QtQuick and Sink
C++
152
star
34

calligra

Office and graphic art suite by KDE
C++
144
star
35

khtml

KHtml
136
star
36

plasma-mobile

Plasma shell for mobile devices
QML
132
star
37

plasma-framework

Plasma library and runtime components
C++
130
star
38

snorenotify

Multi-platform Qt notification framework
C++
128
star
39

syntax-highlighting

Syntax highlighting Engine for Structured Text and Code.
HTML
128
star
40

krusader

Advanced twin panel (commander style) file manager
C++
123
star
41

k3b

Full-featured CD/DVD/Blu-ray burning and ripping application
C++
118
star
42

marble

Virtual Globe and World Atlas that you can use to learn more about the Earth
C++
117
star
43

kmymoney

Personal finance manager
C++
114
star
44

neochat

A client for matrix, the decentralized communication protocol
C
113
star
45

extra-cmake-modules

Extra modules and scripts for CMake.
CMake
113
star
46

umbrello

GUI for diagramming Unified Modelling Language (UML)
C++
105
star
47

plasma-workspace-wallpapers

Wallpapers for Plasma Workspaces
CMake
103
star
48

plasma-browser-integration

Components necessary to integrate browsers into the Plasma Desktop
JavaScript
100
star
49

gwenview

Image viewer by KDE
C++
93
star
50

kdeplasma-addons

All kind of add-ons to improve your Plasma experience
C++
85
star
51

okteta

Hex editor for viewing and editing the raw data of files
C++
85
star
52

plasma-bigscreen

Plasma shell for TVs
QML
84
star
53

kalendar

A calendar application using Akonadi to sync with external services (Nextcloud, GMail, ...)
C++
81
star
54

kio-gdrive

KIO Slave to access Google Drive
C++
80
star
55

wacomtablet

GUI for Wacom Linux drivers that supports different button/pen layout profiles
C++
79
star
56

kleopatra

Certificate manager and GUI for OpenPGP and CMS cryptography
C++
79
star
57

kolourpaint

Easy-to-use paint program
C++
78
star
58

qca

Qt Cryptographic Architecture — straightforward cross-platform crypto API
C++
76
star
59

discover

KDE and Plasma resources management GUI
C++
75
star
60

ark

File archiver by KDE
C++
72
star
61

qqc2-desktop-style

Qt Quick Controls 2: Desktop Style
QML
71
star
62

krunner

Framework for providing different actions given a string query.
C++
69
star
63

kile

Integrated LaTeX Editing Environment
HTML
69
star
64

breeze-gtk

Breeze widget theme for GTK 2 and 3
SCSS
66
star
65

liquidshell

Basic desktop shell using QtWidgets
C++
65
star
66

ring-kde

Qt-based Ring communication framework (www.ring.cx) client
QML
64
star
67

ktexteditor

KTextEditor Framework
C++
64
star
68

ktorrent

Powerful BitTorrent client
C++
64
star
69

konqueror

Web browser and Swiss Army knife for any kind of file management and previewing
C++
63
star
70

ktouch

Touch Typing Tutor
C++
61
star
71

kio

KIO
C++
60
star
72

kmail

State-of-the-art feature-rich email client that supports many protocols
C++
60
star
73

vvave

Multi-platform media player
C++
58
star
74

baloo

Baloo is a framework for searching and managing metadata.
C++
56
star
75

craft

Open source meta build system and package manager
Python
55
star
76

haruna

Open source video player built with Qt/QML and libmpv.
QML
53
star
77

kid3

Efficient audio tagger that supports a large variety of file formats
C++
52
star
78

partitionmanager

Manage the disk devices, partitions and file systems on your computer
C++
51
star
79

cantor

Front end to powerful mathematics and statistics packages
Jupyter Notebook
48
star
80

kscreen

KDE's screen management software
C++
46
star
81

plasma-vault

Plasma applet and services for creating encrypted vaults
C++
46
star
82

ksshaskpass

ssh-add helper that uses KWallet and KPasswordDialog
C++
44
star
83

xdg-desktop-portal-kde

A backend implementation for xdg-desktop-portal that is using Qt/KDE
C++
44
star
84

kbibtex

An editor for bibliographies used with LaTeX
C++
43
star
85

kwayland

KWayland provides a Qt-style Client and Server library wrapper for the Wayland libraries.
C++
43
star
86

krdc

Remote Desktop Client
C++
42
star
87

konversation

User-friendly and fully-featured IRC client
C++
42
star
88

qtcurve

Style engine for Qt and other toolkits
C++
42
star
89

systemsettings

Control center to configure your Plasma Desktop
C++
42
star
90

akregator

RSS Feed Reader
C++
40
star
91

plasma-nm

Plasma applet written in QML for managing network connections
C++
40
star
92

ksysguard

Resource usage monitor for your computer
C
40
star
93

kdev-rust

KDevelop plugin which provides Rust language support.
C++
39
star
94

plasma-systemmonitor

An interface for monitoring system sensors, process information and other system resources
QML
39
star
95

kjs

KJS
39
star
96

minuet

Free and open-source software for music education
C++
39
star
97

docker-neon

Docker packaging environment for KDE Neon
Ruby
38
star
98

elf-dissector

Tools for inspecting, analyzing and optimizing ELF files
C++
38
star
99

ruqola

KDE client for Rocket Chat
C++
38
star
100

libqgit2

Qt wrapper library around the libgit2 git access library
C++
37
star