swap.asciidoc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.mlockall`
  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.mlockall: 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. curl 'http://localhost:9200/_nodes?pretty&filter_path=**.mlockall'
  30. --------------
  31. If you see that `mlockall` is `false`, then it means that the `mlockall`
  32. request has failed. You will also see a line with more information in the
  33. logs with the words `Unable to lock JVM Memory`.
  34. The most probable reason, on Linux/Unix systems, is that the user running
  35. Elasticsearch doesn't have permission to lock memory. This can be granted as follows:
  36. `.zip` and `.tar.gz`::
  37. Set <<ulimit,`ulimit -l unlimited`>> as root before starting Elasticsearch,
  38. or set `memlock` to `unlimited` in
  39. <<limits.conf,`/etc/security/limits.conf`>>.
  40. RPM and Debian::
  41. Set `MAX_LOCKED_MEMORY` to `unlimited` in the
  42. <<sysconfig,system configuration file>> (or see below for systems using `systemd`).
  43. Systems using `systemd`::
  44. Set `LimitMEMLOCK` to `infinity` in the <<systemd,systemd configuration>>.
  45. Another possible reason why `mlockall` can fail is that the temporary directory
  46. (usually `/tmp`) is mounted with the `noexec` option. This can be solved by
  47. specifying a new temp directory, by starting Elasticsearch with:
  48. [source,sh]
  49. --------------
  50. ./bin/elasticsearch -Djava.io.tmpdir=/path/to/temp/dir
  51. --------------
  52. or using the `ES_JAVA_OPTS` environment variable:
  53. [source,sh]
  54. --------------
  55. export ES_JAVA_OPTS="$ES_JAVA_OPTS -Djava.io.tmpdir=/path/to/temp/dir"
  56. ./bin/elasticsearch
  57. --------------
  58. [[disable-swap-files]]
  59. ==== Disable all swap files
  60. The second option is to completely disable swap. Usually Elasticsearch
  61. is the only service running on a box, and its memory usage is controlled
  62. by the JVM options. There should be no need to have swap enabled.
  63. On Linux systems, you can disable swap temporarily
  64. by running: `sudo swapoff -a`. To disable it permanently, you will need
  65. to edit the `/etc/fstab` file and comment out any lines that contain the
  66. word `swap`.
  67. On Windows, the equivalent can be achieved by disabling the paging file entirely
  68. via `System Properties → Advanced → Performance → Advanced → Virtual memory`.
  69. [[swappiness]]
  70. ==== Configure `swappiness`
  71. The second option available on Linux systems is to ensure that the sysctl value
  72. `vm.swappiness` is set to `1`. This reduces the kernel's tendency to swap and
  73. should not lead to swapping under normal circumstances, while still allowing
  74. the whole system to swap in emergency conditions.