ElasticSearch 占用内存过大的解决方案

    • 孙锡源
      楼主

      老实说,我个人之前是没有ES的使用经验的,只是知道有这样一个东西。

      初次运行发现这货竟然吃掉了整整8GB的内存……

      一开始为了先赶紧把功能开发完,就没管。后来发现因为内存被占用,导致交换分区被频繁读写,看来不搞是不行了。

      之所以占用内存很大并不是因为ES真的需要这么多,而是因为他会在启动时提前创建一个堆空间(不知道啥是堆空间的建议先复习C语言基础知识)。

      在ElasticSearch 7.13中这个堆空间的默认大小是可用内存的45%(网上很多文章提到默认堆空间是1GB,这其实是老版本的情况,而且它们介绍的修改方法在新版本中也已经用不了了)。

      要修改这个堆空间的大小可以编辑:/etc/elasticsearch/jvm.options文件,这是ES附带的JVM虚拟机的运行参数配置文件。

      在这个文件的大概第19行就有堆空间大小的配置了:

      19 ################################################################
      20 ## IMPORTANT: JVM heap size
      21 ################################################################
      22 ##
      23 ## The heap size is automatically configured by Elasticsearch
      24 ## based on the available memory in your system and the roles
      25 ## each node is configured to fulfill. If specifying heap is
      26 ## required, it should be done through a file in jvm.options.d,
      27 ## and the min and max should be set to the same value. For
      28 ## example, to set the heap to 4 GB, create a new file in the
      29 ## jvm.options.d directory containing these lines:
      30 ##
      31 ## -Xms4g
      32 ## -Xmx4g
      33 ##
      34 ## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
      35 ## for more information
      36 ##
      37 ################################################################

      照着提示改,然后重启服务就可以了。

正在查看 0 条回复
  • 哎呀,回复话题必需登录。

加入 LitePress 论坛 ,参与知识分享与交流
登录 注册 进行评论
立即加入