swap.asciidoc 3.6 KB

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