ようへいの日々精進XP

よかろうもん

小ネタ道場一本勝負 〜 Ubuntu xenial で Elasticsearch が Cannot allocate memory で起動しない場合 〜

試した環境

ubuntu@ubuntu-xenial:~$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.2 LTS"

ubuntu@ubuntu-xenial:~$ free
              total        used        free      shared  buff/cache   available
Mem:        1015992       49948      397164        3440      568880      770376
Swap:             0           0           0

ubuntu@ubuntu-xenial:~$ curl localhost:9200/
{
  "name" : "u0y-s7V",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "p20emC22SyG96ziu0qwl5w",
  "version" : {
    "number" : "5.3.2",
    "build_hash" : "3068195",
    "build_date" : "2017-04-24T16:15:59.481Z",
    "build_snapshot" : false,
    "lucene_version" : "6.4.2"
  },
  "tagline" : "You Know, for Search"
}

インストール直後

deb パッケージ をダウンロードしてインストール後に確認してみると…

ubuntu@ubuntu-xenial:~$ sudo service elasticsearch status
● elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Tue 2017-05-02 23:48:36 UTC; 4s ago
     Docs: http://www.elastic.co
  Process: 10100 ExecStart=/usr/share/elasticsearch/bin/elasticsearch -p ${PID_DIR}/elasticsearch.pid --quiet -Edefault.path.logs=${LOG_DIR} -Edefault.path.data=${DATA_DIR} -Edefault.path.conf=${CONF_DIR} (code=exited, status=1/FAILURE)
  Process: 10097 ExecStartPre=/usr/share/elasticsearch/bin/elasticsearch-systemd-pre-exec (code=exited, status=0/SUCCESS)
 Main PID: 10100 (code=exited, status=1/FAILURE)

May 02 23:48:36 ubuntu-xenial systemd[1]: Started Elasticsearch.
May 02 23:48:36 ubuntu-xenial elasticsearch[10100]: OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x000000008a660000, 1973026816, 0) failed; error='Cannot allocate memory' (errno=12)
May 02 23:48:36 ubuntu-xenial elasticsearch[10100]: #
May 02 23:48:36 ubuntu-xenial elasticsearch[10100]: # There is insufficient memory for the Java Runtime Environment to continue.
May 02 23:48:36 ubuntu-xenial elasticsearch[10100]: # Native memory allocation (mmap) failed to map 1973026816 bytes for committing reserved memory.
May 02 23:48:36 ubuntu-xenial elasticsearch[10100]: # An error report file with more information is saved as:
May 02 23:48:36 ubuntu-xenial elasticsearch[10100]: # /tmp/hs_err_pid10100.log
May 02 23:48:36 ubuntu-xenial systemd[1]: elasticsearch.service: Main process exited, code=exited, status=1/FAILURE
May 02 23:48:36 ubuntu-xenial systemd[1]: elasticsearch.service: Unit entered failed state.
May 02 23:48:36 ubuntu-xenial systemd[1]: elasticsearch.service: Failed with result 'exit-code'.

ふむ。

ということで、技あり

/etc/elasticsearch/jvm.options で JVM オプションを修正してヒープサイズを修正する。

root@ubuntu-xenial:/etc/elasticsearch# diff -u jvm.options.bk jvm.options
--- jvm.options.bk      2017-05-02 23:51:22.295369994 +0000
+++ jvm.options 2017-05-02 23:51:36.788119994 +0000
@@ -19,8 +19,8 @@
 # Xms represents the initial size of total heap space
 # Xmx represents the maximum size of total heap space

--Xms2g
--Xmx2g
+-Xms512m
+-Xmx512m

 ################################################################
 ## Expert settings

実際にどの程度の値に設定すれば良いのかについては、以下のドキュメントに記載されている。

www.elastic.co

ざっくり要約すると…(Powered by Google 翻訳)

  • minimum heap size (Xms) と maximum heap size (Xmx) は同じ値にしましょう
  • Elasticsearch で使用可能なヒープサイズが多いほど、キャッシュに使用できるメモリが増えるばい
  • ただし、ヒープサイズを割り当て過ぎると長い GC が発生する可能性があることに注意しませう
  • maximum heap size (Xmx) は物理メモリの 50% を超えないようにして、カーネルファイルシステムのキャッシュ用に十分な物理メモリが残っていることを確認しませう
  • maximum heap size (Xmx) を 32GB に近い値に設定しない方が良いです(超意訳ですいません … compressed oops の絡み)
  • じゃあ、Xmx の最大値だと 26GB 〜 30GB くらいにしておくと安全でせう(超意訳ですいません)

がっつりメモリがある場合でも、メモリの割り当てには注意しましょうってことですな。

有難うございました

改めて JVM について勉強が必要な事がわかったので、今回は「技あり」ってことで。