ElasticSearch6.3.2 中的JVM性能调优
前一段时间被人问了个问题:在使用ES的过程中有没有做过什么JVM调优措施?
在我搭建ES集群过程中,参照important-settings官方文档来的,并没有对JVM参数做过多的调整。但谈到JVM配置参数,少不了操作系统层面上的一些配置参数,比如 page cache 和文件描述符的个数:(/etc/security/limits.conf)。另外ES jvm.options配置文件也针对JVM参数做了一些优化,这里简要介绍一下ElasticSearch中与jvm相关的各个配置参数:
- 将 Xms 和 Xmx 设置成一样大 避免JVM堆的动态调整给应用进程带来"不稳定"。参考:Heap Tuning Parameters
- By default, the JVM grows or shrinks the heap at each GC to try to keep the proportion of free space to the living objects at each collection within a specific range. This range is set as a percentage by the parameters -XX:MinHeapFreeRatio=minimum and -XX:MaxHeapFreeRatio=maximum; and the total size bounded by -Xms and -Xmx
- 预留足够的内存空间给page cache。假设一台32GB内存的机器,配置16GB内存给ES进程使用,另外16GB给page cache,而不是将 xmx 设置成32GB。
- 禁用内存交换(/proc/sys/vm/swappiness),后面解释为什么要禁用内存交换?
- 配置JVM参数:-XX:+AlwaysPreTouch 减少新生代晋升到老年代时停顿。JDK官方文档关于 AlwaysPreTouch 的解释是:Pre-touch the Java heap during JVM initialization. Every page of the heap is thus demand-zeroed during initialization rather than incrementally during application execution.
也就是说:在启动时就把参数里说好了的内存全部都分配了,会使得启动慢上一点,但后面访问时会更流畅,比如页面会连续分配,比如不会在晋升新生代到老生代时才去访问页面,导致GC停顿时间加