OS and JVM Tuning: Garbage Collection, Heap Sizing and Kernel Settings
Why JVM Tuning Matters
A NameNode with a long garbage collection pause stops answering heartbeats. After a few seconds ZooKeeper may declare it dead and fail over, or DataNodes may time out. Stable memory and short pauses are core admin work.
Heap Sizing
| Daemon | Typical heap | Rule |
|---|---|---|
| NameNode | 16 to 200+ GB | About 1 GB per million blocks as a start, plus 20 to 30 percent headroom. Watch actual usage after full GC |
| DataNode | 2 to 8 GB | Grows with block count per node |
| ResourceManager | 8 to 32 GB | Depends on apps and nodes |
| NodeManager | 1 to 4 GB | Containers run in separate JVMs |
| JournalNode | 1 to 4 GB | Small |
| Hive Metastore, HS2 | 4 to 32 GB | Depends on tables and sessions |
Do not set heap too close to RAM: Leave memory for the OS page cache, the thread stacks, direct memory and other daemons. Also keep heap below about 32 GB if you can, because compressed object pointers stop working above that and objects grow.
NameNode JVM Options (G1GC Example)
bash
# hadoop-env.sh
export HDFS_NAMENODE_OPTS="
-Xms96g -Xmx96g
-XX:+UseG1GC
-XX:MaxGCPauseMillis=200
-XX:+ParallelRefProcEnabled
-XX:InitiatingHeapOccupancyPercent=35
-XX:G1HeapRegionSize=32m
-XX:+AlwaysPreTouch
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/var/log/hadoop/nn-heap.hprof
-Xlog:gc*:file=/var/log/hadoop/nn-gc.log:time,uptime,level,tags:filecount=10,filesize=100m
$HDFS_NAMENODE_OPTS"
export HDFS_DATANODE_OPTS="-Xms4g -Xmx4g -XX:+UseG1GC -XX:MaxGCPauseMillis=200 $HDFS_DATANODE_OPTS"
| Option | Purpose |
|---|---|
-Xms = -Xmx | Fixed heap, no resizing pauses |
UseG1GC, MaxGCPauseMillis | Predictable pauses on large heaps |
InitiatingHeapOccupancyPercent | Start concurrent cycle earlier to avoid full GC |
AlwaysPreTouch | Touch all memory at startup (slower start, steadier runtime) |
HeapDumpOnOutOfMemoryError | Evidence for post-mortem |
| GC log flags | Needed to tune. Flag syntax depends on the Java version (above is Java 11+) |
Java version: Use a Java version supported by your Hadoop release (Hadoop 3.4 and 3.5 support Java 8, 11 and 17, check the release notes). Some JVM flags are removed in newer versions, so test them.
Read a GC Log
bash
# find the longest pauses
grep -E "Pause (Young|Full|Remark|Cleanup)" /var/log/hadoop/nn-gc.log | awk '{print $NF, $0}' | sort -rn | head
grep -c "Pause Full" /var/log/hadoop/nn-gc.log # Full GCs: should be zero in normal operatio
| Observation | Meaning | Action |
|---|---|---|
| Full GC events | Heap too small or fragmentation | Increase heap, lower IHOP, fix leak |
| Young pauses > 1 s | Large young gen or slow CPU/memory | Tune region size, reduce churn (fewer small files) |
| Heap after GC climbing | Namespace growth or leak | Plan capacity, heap dump analysis |
| Humongous allocations | Objects bigger than half a region | Increase G1HeapRegionSize |
Kernel and OS Settings
bash
# /etc/sysctl.d/99-hadoop.conf
vm.swappiness = 1
vm.overcommit_memory = 1
vm.max_map_count = 262144
net.core.somaxconn = 8192
net.ipv4.tcp_max_syn_backlog = 8192
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.ip_local_port_range = 10000 65000
fs.file-max = 1000000
# apply: sysctl --system
# /etc/security/limits.d/hadoop.conf
hdfs - nofile 128000
hdfs - nproc 65536
yarn - nofile 128000
yarn - nproc 65536
bash
# transparent huge pages OFF (can cause latency spikes)
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
cat /sys/kernel/mm/transparent_hugepage/enabled
# mount options for data disks (fstab): noatime,nodiratime
# I/O scheduler for HDD: mq-deadline; for NVMe: none
cat /sys/block/sdb/queue/scheduler
Time, Swap and NUMA
- chrony running and synced (Kerberos needs it)
- Swap disabled or
swappiness=1: swapping a JVM heap causes huge pauses - On large multi-socket servers check NUMA behaviour (
numactl --hardware). Bind or interleave for the NameNode if you see imbalance - Reserve memory for the OS page cache (Kafka, HBase, DataNode benefit)
Verify After Changes
bash
cat /proc/$(pgrep -f proc_namenode | head -1)/limits | grep -i "open files"
jcmd $(pgrep -f proc_namenode | head -1) VM.flags | tr ' ' '\n' | grep -E "Xmx|UseG1GC"
jstat -gcutil $(pgrep -f proc_namenode | head -1) 5000 3
Plan heap growth: Keep a chart of NameNode heap used after GC against block count. When it reaches 70 percent, schedule a heap increase and restart through failover during a maintenance window.
Conclusion
Size JVM heaps from real metadata and workload, use G1GC with logging and heap dumps, turn off swapping and huge pages, raise file limits, and verify with jstat and GC logs. Stable GC means stable HA.
Quick Summary: fixed heap, G1GC + GC logs; swappiness 1; THP off; nofile 128000.

