安装 ZooKeeper 时有一步要求调整 Java Heap 大小,比如 4GB 内存的机器应该把最大 heap size 调整为 3GB, 从而避免swapping(swapping 会严重影响 ZooKeeper 性能)。

查看当前环境最大 Heap Size

[vagrant@zookeeper1 ~]$ java -XX:+PrintFlagsFinal -version | grep -iE 'Heapsize|permsize|threadStackSize'
Picked up _JAVA_OPTIONS: -Xmx3072m
     intx CompilerThreadStackSize               = 0                             {pd product}
    uintx ErgoHeapSizeLimit                     = 0                             {product}
    uintx HeapSizePerGCThread                   = 87241520                      {product}
    uintx InitialHeapSize                      := 62914560                      {product}
    uintx LargePageHeapSizeThreshold            = 134217728                     {product}
    uintx MaxHeapSize                          := 3221225472                    {product}
     intx ThreadStackSize                       = 1024                          {pd product}
     intx VMThreadStackSize                     = 1024                          {pd product}
openjdk version "1.8.0_121"
OpenJDK Runtime Environment (build 1.8.0_121-b13)
OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)

InitialHeapSize = 62914560 Byte = 60M MaxHeapSize = 3221225472 Byte = 3GB

运行时指定

# 指定最大HeapSize为6G,使用64-bit JVM
$ java -Xmx6144M -d64 ...

设置环境变量

# 指定InitialHeapSize为512M,MaxHeapSize为3072M
$ export _JAVA_OPTIONS="-Xms512m -Xmx3072m"