Home / Data Platform / Streaming & CDC

Data PlatformCheat sheetAdvanced

Flink TaskManager memory cheat sheet: heap, managed, network, metaspace and which setting each OOM needs

Map every Flink TaskManager memory component to its config key, read the effective sizes, and match each OutOfMemoryError or container kill to the one setting that fixes it.

Deepak Bhardwaj7 min read

Applies toApache Flink 1.17 to 2.x (standalone, YARN, Kubernetes and the Flink Kubernetes Operator), RocksDB and HashMap state backends

The model in one picture

A TaskManager has a fixed budget and Flink splits it before the JVM starts. If you raise one component without knowing which one failed, you just move the shortage somewhere else.

Total process memory   (taskmanager.memory.process.size)  <- what the container gets
├── JVM metaspace      (taskmanager.memory.jvm-metaspace.size)
├── JVM overhead       (taskmanager.memory.jvm-overhead.*)   native: threads, GC, code cache
└── Total Flink memory (taskmanager.memory.flink.size)
    ├── JVM heap
    │   ├── Framework heap  (taskmanager.memory.framework.heap.size)
    │   └── Task heap       (taskmanager.memory.task.heap.size)
    └── Off-heap
        ├── Managed memory   (taskmanager.memory.managed.size | .fraction)  RocksDB, batch sort/join, Python
        ├── Framework off-heap (taskmanager.memory.framework.off-heap.size)
        ├── Task off-heap    (taskmanager.memory.task.off-heap.size)  direct memory for your code/connectors
        └── Network memory   (taskmanager.memory.network.min | .max | .fraction)  shuffle buffers

Set one top-level size: taskmanager.memory.process.size for containers (YARN, Kubernetes), or taskmanager.memory.flink.size for standalone hosts. Flink derives the rest. If you also set task heap and managed memory explicitly, the sums must agree or the TaskManager fails at startup with a configuration conflict.

Key settings and documented defaults

Defaults change between releases. Check the memory configuration page for your exact version before you depend on these.

Key What it controls Typical default
taskmanager.memory.process.size Whole container/JVM process none (set it)
taskmanager.memory.flink.size Everything except metaspace and overhead none
taskmanager.memory.managed.fraction Share of total Flink memory given to managed memory 0.4
taskmanager.memory.network.fraction Share of total Flink memory for network buffers 0.1, clamped by min/max
taskmanager.memory.network.min Lower clamp for network memory 64 MB
taskmanager.memory.network.max Upper clamp for network memory version dependent; check your docs
taskmanager.memory.jvm-metaspace.size Class metadata 256 MB
taskmanager.memory.jvm-overhead.fraction Native overhead share of process size 0.1, clamped by min/max (max 1 GB)
taskmanager.memory.framework.heap.size / .off-heap.size Flink's own needs 128 MB each; rarely change
taskmanager.memory.task.off-heap.size Direct memory for user code 0

On Flink 2.x the configuration file is config.yaml. Flink 1.x uses flink-conf.yaml (1.19 and later can also read config.yaml). With the Flink Kubernetes Operator, spec.taskManager.resource.memory becomes the process size.

Read the effective sizes before changing anything

Every TaskManager logs its final split at startup. Find it rather than recalculating by hand:

# Kubernetes
kubectl logs <taskmanager-pod> -c flink-main-container | grep -A 20 "Final TaskExecutor Memory configuration"

# Standalone / YARN
grep -A 20 "Final TaskExecutor Memory configuration" log/flink-*-taskexecutor-*.log

The REST API gives you live usage per TaskManager. Metric names vary slightly by version, so list them first:

FLINK=http://jobmanager:8081
TM=$(curl -s $FLINK/taskmanagers | jq -r '.taskmanagers[0].id')

# Available metric names
curl -s "$FLINK/taskmanagers/$TM/metrics" | jq -r '.[].id' | grep -E 'Memory|Netty|Shuffle'

# Heap, direct, metaspace and managed usage
curl -s "$FLINK/taskmanagers/$TM/metrics?get=Status.JVM.Memory.Heap.Used,Status.JVM.Memory.Heap.Max,Status.JVM.Memory.Direct.MemoryUsed,Status.JVM.Memory.Metaspace.Used,Status.Flink.Memory.Managed.Used,Status.Flink.Memory.Managed.Total" | jq

# Configured memory breakdown as Flink sees it
curl -s "$FLINK/taskmanagers/$TM" | jq '.memoryConfiguration'

Which error needs which setting

This table is the reason the cheat sheet exists. Match the exact message before touching a value.

Symptom Component that ran out Fix
java.lang.OutOfMemoryError: Java heap space or GC overhead limit exceeded Task heap Raise taskmanager.memory.task.heap.size, or raise process size and lower managed.fraction if you use the HashMap backend. Check for huge windows or state held on heap.
java.lang.OutOfMemoryError: Direct buffer memory Task off-heap (or framework off-heap) Raise taskmanager.memory.task.off-heap.size. Connectors and clients that allocate direct buffers (Kafka, HTTP, compression codecs) are the usual cause.
java.lang.OutOfMemoryError: Metaspace JVM metaspace Raise taskmanager.memory.jvm-metaspace.size. If it grows after every job restart in a session cluster, you have a class-loading leak: fix the user jar or move to application mode.
Insufficient number of network buffers at deploy time Network memory Raise taskmanager.memory.network.fraction and/or .max, or reduce parallelism/slots per TaskManager.
Pod OOMKilled (exit code 137), or YARN "running beyond physical memory limits" Native memory outside the JVM's own accounting Usually RocksDB beyond its budget or glibc arena growth. Confirm state.backend.rocksdb.memory.managed: true, raise jvm-overhead, and see the next section.
Heap fine, but checkpoints slow and RocksDB reads disk constantly Managed memory too small for the working set Raise managed.fraction or managed.size; give fewer slots per TaskManager.

Warning

Raising -Xmx through env.java.opts bypasses the memory model. The container limit stays the same, so the extra heap is taken from native memory that RocksDB and the JVM need, and the next failure is an OOMKilled that is harder to diagnose. Change the Flink memory keys instead.

RocksDB and managed memory

With the RocksDB backend and state.backend.rocksdb.memory.managed: true (the default), Flink caps RocksDB's block cache and write buffers at the managed memory of each slot. Managed memory is split evenly across slots, so 4 slots on a TaskManager with 4 GB managed memory gives each slot about 1 GB.

Useful knobs:

state.backend.type: rocksdb                 # Flink 2.x key; older releases use state.backend
state.backend.rocksdb.memory.managed: true
state.backend.rocksdb.memory.write-buffer-ratio: 0.5   # share of the budget for memtables
state.backend.rocksdb.memory.high-prio-pool-ratio: 0.1 # index/filter blocks kept hot
# state.backend.rocksdb.memory.fixed-per-slot: 1gb     # fixed budget per slot instead of managed memory

Even with these settings, RocksDB can go somewhat over budget. Memtables fill before a flush, and allocator fragmentation adds more. If pods are killed while heap is well below its limit, increase taskmanager.memory.jvm-overhead.min (for example to 512 MB or 1 GB) before you increase managed memory. Some teams also set MALLOC_ARENA_MAX=2 or use jemalloc to cut glibc fragmentation. Test either change on one job first.

Tip

On the HashMap backend, streaming jobs barely use managed memory, so the default 40% is mostly wasted. Lower taskmanager.memory.managed.fraction (for example to 0.05–0.1) and give the space to task heap, where HashMap state actually lives. Leave some managed memory if you run PyFlink, because Python workers use it by default.

Sizing patterns that hold up

Slots per TaskManager

More slots per TaskManager means each slot gets a smaller share of managed and network memory, and one heavy operator can starve the others on the same JVM. For stateful RocksDB jobs, 1–4 slots per TaskManager is easier to reason about than 16.

Network memory and parallelism

The buffers needed grow with the number of channels between operators, which is roughly upstream × downstream parallelism. A job that deploys at parallelism 50 can fail at 200 with insufficient network buffers and no other change. Raise network.max along with parallelism, or enable buffer debloating (taskmanager.network.memory.buffer-debloat.enabled: true) to keep in-flight data small without starving channels.

A worked example

Container limit 8 GB, RocksDB backend, 2 slots:

taskmanager.memory.process.size: 8g
taskmanager.numberOfTaskSlots: 2
taskmanager.memory.managed.fraction: 0.45
taskmanager.memory.jvm-overhead.min: 768m
taskmanager.memory.jvm-metaspace.size: 384m

Restart one TaskManager, check the "Final TaskExecutor Memory configuration" log lines, and confirm that heap, managed and network sizes come out as you expect before you roll it out to the cluster.

Prevent it next time

  • Alert on heap use, Status.Flink.Memory.Managed.Used against its total, and container restarts with reason OOMKilled, per TaskManager rather than averaged across the cluster.
  • Keep memory settings in Git next to the job's parallelism. A parallelism change and a network memory change belong in the same pull request.
  • Record the last good "Final TaskExecutor Memory configuration" output in the job's runbook, so on-call can compare it against the failing pod.
  • Load-test with production-sized state. A RocksDB job that has never had 200 GB of state per slot will not show its native memory peak in dev.
  • When you upgrade Flink, diff the memory defaults in the release notes. A changed default can shift the split without any change to your config.