Location>code7788 >text

Flame Graph User Guide

Popularity:961 ℃/2025-04-08 20:39:21

🔥 Flame Graph is a visual performance analysis tool that helps you quickly locate CPU, memory, or I/O bottlenecks. It looks like a flame, hence the name.

  1. What problems can the flame diagram solve?
  • High CPU usage: Find out which functions consume the most CPU time.
  • Program lag: analyze the code execution path and find slow operations.
  • Thread blocking: See which threads are waiting for locks or I/O.
  • Memory leak (need to be accompanied by memory analysis tools).

  1. Preparation
    Linux / macOS

Install perf (Linux Performance Analysis Tool)

sudo apt install linux-tools-common linux-tools-generic # Ubuntu/Debian
sudo yum install perf # CentOS/RHEL

Install FlameGraph script

git clone /brendangregg/
cd FlameGraph
export PATH=$PATH:$(pwd) # Temporarily added to PATH
3. Generate CPU flame diagram (taking Java as an example)
(1) Collect data
Method 1: Use perf (recommended, for Linux)

1. Find the Java Process ID

jps # or ps -ef | grep java

2. Acquisition CPU call stack (sampling for 30 seconds)

sudo perf record -F 99 -p -g -- sleep 30

3. Generate flame diagram

perf script | | >

Method 2: Use async-profiler (simpler, supports Java)

Download async-profiler

wget /jvm-profiling-tools/async-profiler/releases/download/v2.9/async-profiler-2.
tar -xzf async-profiler-.
cd async-profiler-

Acquisition of CPU data (sampling for 30 seconds)

./ -d 30 -f
(2) View the flame map
The generated one can be opened in a browser:
firefox # or chrome, edge
4. How to understand the flame diagram?
Flame diagram structure

  • Y axis (height): call stack depth (the deeper the call chain is longer).
  • X-axis (width): The proportion of function execution time (the wider the more CPU is used).
  • Color: No special meaning, only used to distinguish different functions.
    Key operations
    operate
    illustrate
    Mouse hover
    Display function name and CPU ratio
    Click to enlarge
    View the detailed call chain of a function
    Search (Ctrl+F)
    Find specific functions (such as )
    [picture]

  1. Frequently Asked Questions
    (1) What should I do if the flame diagram shows [unknown]?
  • Cause: Debug symbols are missing (such as -XX:+PreserveFramePointer not enabled by JVM).
  • Solution:

Add parameters when Java runtime

java -XX:+PreserveFramePointer -jar your_app.jar

Or use the --all-user option of async-profiler

./ --all-user -d 30 -f

(2) How to analyze memory leaks?
Use memory flame diagram instead (requires async-profiler):
./ -d 30 -e alloc -f mem_flamegraph.svg
6. Advanced usage
Scene
Order
Analytics lock competition
./ -e lock -d 30 -f
Analyze I/O Wait
perf record -e 'sched:sched_stat_iowait' -p -g -- sleep 10
Generate differential flame diagram
diff two_flamegraphs.svg
7. Summary

  1. Installation tool: perf + FlameGraph or async-profiler.
  2. Collect data: perf record or ./ -d 30.
  3. Generate SVG: Convert data.