swap.asciidoc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. [[setup-configuration-memory]]
  2. === Disable swapping
  3. Most operating systems try to use as much memory as possible for file system
  4. caches and eagerly swap out unused application memory. This can result in parts
  5. of the JVM heap or even its executable pages being swapped out to disk.
  6. Swapping is very bad for performance, for node stability, and should be avoided
  7. at all costs. It can cause garbage collections to last for **minutes** instead
  8. of milliseconds and can cause nodes to respond slowly or even to disconnect
  9. from the cluster. In a resilient distributed system, it's more effective to let
  10. the operating system kill the node.
  11. There are three approaches to disabling swapping. The preferred option is to
  12. completely disable swap. If this is not an option, whether or not to prefer
  13. minimizing swappiness versus memory locking is dependent on your environment.
  14. [[disable-swap-files]]
  15. ==== Disable all swap files
  16. Usually Elasticsearch is the only service running on a box, and its memory usage
  17. is controlled by the JVM options. There should be no need to have swap enabled.
  18. On Linux systems, you can disable swap temporarily by running:
  19. [source,sh]
  20. --------------
  21. sudo swapoff -a
  22. --------------
  23. To disable it permanently, you will need to edit the `/etc/fstab` file and
  24. comment out any lines that contain the word `swap`.
  25. On Windows, the equivalent can be achieved by disabling the paging file entirely
  26. via `System Properties → Advanced → Performance → Advanced → Virtual memory`.
  27. [[swappiness]]
  28. ==== Configure `swappiness`
  29. Another option available on Linux systems is to ensure that the sysctl value
  30. `vm.swappiness` is set to `1`. This reduces the kernel's tendency to swap and
  31. should not lead to swapping under normal circumstances, while still allowing the
  32. whole system to swap in emergency conditions.
  33. [[bootstrap-memory_lock]]
  34. ==== Enable `bootstrap.memory_lock`
  35. Another option is to use
  36. http://opengroup.org/onlinepubs/007908799/xsh/mlockall.html[mlockall] on
  37. Linux/Unix systems, or
  38. https://msdn.microsoft.com/en-us/library/windows/desktop/aa366895%28v=vs.85%29.aspx[VirtualLock]
  39. on Windows, to try to lock the process address space into RAM, preventing any
  40. Elasticsearch memory from being swapped out. This can be done, by adding this
  41. line to the `config/elasticsearch.yml` file:
  42. [source,yaml]
  43. --------------
  44. bootstrap.memory_lock: true
  45. --------------
  46. WARNING: `mlockall` might cause the JVM or shell session to exit if it tries to
  47. allocate more memory than is available!
  48. After starting Elasticsearch, you can see whether this setting was applied
  49. successfully by checking the value of `mlockall` in the output from this
  50. request:
  51. [source,js]
  52. --------------
  53. GET _nodes?filter_path=**.mlockall
  54. --------------
  55. // CONSOLE
  56. If you see that `mlockall` is `false`, then it means that the `mlockall`
  57. request has failed. You will also see a line with more information in the logs
  58. with the words `Unable to lock JVM Memory`.
  59. The most probable reason, on Linux/Unix systems, is that the user running
  60. Elasticsearch doesn't have permission to lock memory. This can be granted as
  61. follows:
  62. `.zip` and `.tar.gz`::
  63. Set <<ulimit,`ulimit -l unlimited`>> as root before starting Elasticsearch,
  64. or set `memlock` to `unlimited` in
  65. <<limits.conf,`/etc/security/limits.conf`>>.
  66. RPM and Debian::
  67. Set `MAX_LOCKED_MEMORY` to `unlimited` in the
  68. <<sysconfig,system configuration file>> (or see below for systems using
  69. `systemd`).
  70. Systems using `systemd`::
  71. Set `LimitMEMLOCK` to `infinity` in the <<systemd,systemd configuration>>.
  72. Another possible reason why `mlockall` can fail is that
  73. <<executable-jna-tmpdir,the JNA temporary directory (usually a sub-directory of
  74. `/tmp`) is mounted with the `noexec` option>>. This can be solved by specifying
  75. a new temporary directory for JNA using the `ES_JAVA_OPTS` environment variable:
  76. [source,sh]
  77. --------------
  78. export ES_JAVA_OPTS="$ES_JAVA_OPTS -Djna.tmpdir=<path>"
  79. ./bin/elasticsearch
  80. --------------
  81. or setting this JVM flag in the jvm.options configuration file.