调整 Elasticsearch 分配的 Heap Size

今天在吐槽纸吸管之前,在 Mastodon 搜索「纸吸管」,准备连上之前的吐槽形成一个 thread。但没有出来任何搜索结果。查看了一下服务器 Elasticsearch 的运行状态:

sudo service elasticsearch status

随之发现处于 inactive,同时报错:

Jan 08 07:20:22 boilingcong elasticsearch[31559]: # There is insufficient memory for the Java Runtime Environment to continue.
Jan 08 07:20:22 boilingcong elasticsearch[31559]: # Native memory allocation (mmap) failed to map 986513408 bytes for committing reserved memory.

原来是我可怜的内存只剩下 900 多 MB free,报错信息和查询结果都显示这并不满足 Elasticsearch 默认分配堆的大小:

By default, Elasticsearch tells the JVM to use a heap with a minimum and maximum size of 1 GB.

随后又有:

Elasticsearch will assign the entire heap specified in jvm.options via the Xms (minimum heap size) and Xmx (maximum heap size) settings. These two settings must be equal to each other.

其在 jvm.options 里:

-Xms1g
-Xmx1g

所以我们需要通过添加 /etc/elasticsearch/jvm.options.d/custom.options 或者直接修改1的方式将其更改成:

-Xms512m
-Xmx512m

并重新启动:

sudo service elasticsearch restart

  1. 不过注意文档说「You should never need to modify the root jvm.options file instead preferring to use custom JVM options files.」 ↩︎


comments powered by Disqus