1

官方文档摘要

jvm.options

  • 一般情况下并不需要更改 JVM 的配置项。
  • 最可能更改的 JVM 配置是堆大小(heap size)
  • 可以使用 jvm.options 配置文件或 ES_JAVA_OPTS 环境变量更改 JVM 配置
  • 首选通过 jvm.options 更改配置

heap size

  • Elasticsearch 通过 jvm.options 中的 Xms 和 Xmx 设置堆的大小
  • 应该讲 Xms 和 Xmx 设为相同的值

    -Xms8g 
    -Xmx8g 
  • Xms 和 Xmx 的值不应超过物理内存的 50%,因为 Elasticsearch 在堆内存之外还需要将内存用于其他。例如 Elasticsearch 需要利用堆外内存来进行网络通信,依赖操作系统的文件系统缓存来有效访问文件,而 JVM 本身也需要一些内存。
  • CompressedOops(compressed ordinary object pointers,压缩普通对象指针)

    (0, 2GB]      Compressed Oops mode: 32-bit
    [2GB, 26GB]   Compressed Oops mode: Zero based,26G不是确切值,视系统而定
    (26GB, 32GB)  Compressed Oops mode: Non-zero disjoint base,32G不是确切值,视系统而定
    [32GB, )      CompressedOops 失效,32G不是确切值,视系统而定

    CompressedOops

  • 26 GB 对大多数系统时安全的
  • 如果设置的堆内存大小的值低于阈值(CompressedOops,32G),在启动 ES 时在日志中会显示类似如下行

    $ bin/elasticsearch
    heap size [1.9gb], compressed ordinary object pointers [true]

kibana

查看 jdk 版本

GET _cat/nodes?h=jdk&v
jdk
15.0.1

查看 jvm 堆内存配置

  • 命令

    GET _cat/nodes?h=heap*&v
  • 输出示例

    heap.current heap.percent heap.max
         1.4gb            4   30.3gb
         1.1gb            3   30.3gb
       826.7mb            2   30.3gb

操作步骤

检查 CompressedOops 阈值

$ ll
total 572
drwxr-xr-x  9 qbit qbit   4096 Mar 26 14:36 ./
drwxrwxr-x  3 qbit qbit   4096 May  8 09:50 ../
drwxr-xr-x  2 qbit qbit   4096 Mar 26 14:36 bin/
drwxr-xr-x  2 qbit qbit   4096 Mar 26 14:36 config/
drwxr-xr-x  9 qbit qbit   4096 Mar 26 14:36 jdk/
drwxr-xr-x  3 qbit qbit   4096 Mar 26 14:36 lib/
-rw-r--r--  1 qbit qbit  13675 Mar 26 14:28 LICENSE.txt
drwxr-xr-x  2 qbit qbit   4096 Mar 26 14:36 logs/
drwxr-xr-x 38 qbit qbit   4096 Mar 26 14:37 modules/
-rw-r--r--  1 qbit qbit 523209 Mar 26 14:36 NOTICE.txt
drwxr-xr-x  2 qbit qbit   4096 Mar 26 14:36 plugins/
-rw-r--r--  1 qbit qbit   8164 Mar 26 14:28 README.asciidoc
下面测试的粒度为 GB,也可以到 MB
# CompressedOops 阈值
# 32 G,false 表示超过了阈值
$ ./jdk/bin/java -Xmx32g -XX:+PrintFlagsFinal 2> /dev/null | grep UseCompressedOops
    bool UseCompressedOops    = false    {lp64_product}
# 31G,true 表示在阈值之内
$ ./jdk/bin/java -Xmx31g -XX:+PrintFlagsFinal 2> /dev/null | grep UseCompressedOops
    bool UseCompressedOops    := true    {lp64_product}
# zero based Compressed Oops 阈值
# 31G,Non-zero
./jdk/bin/java  -server -Xms31G -Xmx31G -XX:+UnlockDiagnosticVMOptions -Xlog:gc+heap+coops=info -version
[0.134s][info][gc,heap,coops] Heap address: 0x0000001000800000, size: 31744 MB, Compressed Oops mode: Non-zero disjoint base: 0x0000001000000000, Oop shift amount: 3
openjdk 13.0.2 2020-01-14
OpenJDK Runtime Environment AdoptOpenJDK (build 13.0.2+8)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 13.0.2+8, mixed mode, sharing)
# 30G,Zero based
./jdk/bin/java  -server -Xms30G -Xmx30G -XX:+UnlockDiagnosticVMOptions -Xlog:gc+heap+coops=info -version
[0.128s][info][gc,heap,coops] Heap address: 0x0000000080000000, size: 30720 MB, Compressed Oops mode: Zero based, Oop shift amount: 3
openjdk version "13.0.1" 2019-10-15
OpenJDK Runtime Environment AdoptOpenJDK (build 13.0.1+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 13.0.1+9, mixed mode, sharing)

确定堆内存大小的值

# 公式
heapSize = math.floor(RAM/2)  # 物理内存的 50% 下取整
if heapSize > threshold:      # 如果大于阈值,则设为等于(或略小于阈值大小)
    heapSize = threshold
# 例一
机器状况: 物理内存 128G,CompressedOops 阈值为 31 G,
         zero based Compressed Oops 阈值为 30 G
配置建议: 启动两个实例,堆内存大小设为 30G
# 例二(最佳实践)
机器状况: 物理内存 64G,CompressedOops 阈值为 31 G,
         zero based Compressed Oops 阈值为 30 G
配置建议: 启动一个实例,堆内存大小设为 30G
# 例三
机器状况: 物理内存 32G,CompressedOops 阈值为 31 G
         zero based Compressed Oops 阈值为 30 G
配置建议: 启动一个实例,堆内存大小设为 16G

启动检查

  • 查看堆内存

    cat ./config/jvm.options | grep '^\-Xm'
    -Xms4g
    -Xmx4g
    $ ./bin/elasticsearch
    OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
    [2020-05-08T10:00:11,381][INFO ][o.e.e.NodeEnvironment    ] [qhost] using [1] data paths, ......
    [2020-05-08T10:00:11,383][INFO ][o.e.e.NodeEnvironment    ] [qhost] heap size [3.9gb], compressed ordinary object pointers [true]
    [2020-05-08T10:00:11,481][INFO ][o.e.n.Node               ] [qhost] node name [qhost], node ID ......
    [2020-05-08T10:00:11,481][INFO ][o.e.n.Node               ] [qhost] version[7.6.2], pid[38912], ......
    ...........................
    # 从 heap size [3.9gb], compressed ordinary object pointers [true] 行可以看到,
    # 对象指针压缩是启用的
  • 查看物理内存

    # RSS(Resident Set Size)即为占用物理内存大小,单位为 KB
    ps aux | head -1; ps aux | grep Elasticsearch
    USER    PID %CPU %MEM    VSZ   RSS      TTY STAT START   TIME COMMAND
    qbit  41699  1.2 44.6 10100728 4545928 pts/0 Sl+ 11:40   0:37  ...bootstrap.Elasticsearch

测试案例

本文出自 qbit snap

qbit
268 声望279 粉丝