You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 510_Deployment/50_heap.asciidoc
+86-123
Original file line number
Diff line number
Diff line change
@@ -1,101 +1,76 @@
1
1
[[heap-sizing]]
2
-
=== Heap: Sizing and Swapping
2
+
=== 堆内存:大小和交换
3
3
4
-
The default installation of Elasticsearch is configured with a 1 GB heap. ((("deployment", "heap, sizing and swapping")))((("heap", "sizing and setting"))) For
5
-
just about every deployment, this number is far too small. If you are using the
6
-
default heap values, your cluster is probably configured incorrectly.
4
+
Elasticsearch 默认安装后设置的堆内存是 1 GB。((("deployment", "heap, sizing and swapping")))((("heap", "sizing and setting")))对于任何一个业务部署来说,
5
+
这个设置都太小了。如果你正在使用这些默认堆内存配置,您的群集可能会出现问题。
7
6
8
-
There are two ways to change the heap size in Elasticsearch. The easiest is to
9
-
set an environment variable called `ES_HEAP_SIZE`.((("ES_HEAP_SIZE environment variable"))) When the server process
10
-
starts, it will read this environment variable and set the heap accordingly.
11
-
As an example, you can set it via the command line as follows:
There is another reason to not allocate enormous heaps to Elasticsearch. As it turns((("heap", "sizing and setting", "32gb heap boundary")))((("32gb Heap boundary")))
57
-
out, the HotSpot JVM uses a trick to compress object pointers when heaps are less
58
-
than around 32 GB.
59
-
60
-
In Java, all objects are allocated on the heap and referenced by a pointer.
61
-
Ordinary object pointers (OOP) point at these objects, and are traditionally
62
-
the size of the CPU's native _word_: either 32 bits or 64 bits, depending on the
63
-
processor. The pointer references the exact byte location of the value.
64
-
65
-
For 32-bit systems, this means the maximum heap size is 4 GB. For 64-bit systems,
66
-
the heap size can get much larger, but the overhead of 64-bit pointers means there
67
-
is more wasted space simply because the pointer is larger. And worse than wasted
68
-
space, the larger pointers eat up more bandwidth when moving values between
69
-
main memory and various caches (LLC, L1, and so forth).
70
-
71
-
Java uses a trick called https://wikis.oracle.com/display/HotSpotInternals/CompressedOops[compressed oops]((("compressed object pointers")))
72
-
to get around this problem. Instead of pointing at exact byte locations in
73
-
memory, the pointers reference _object offsets_.((("object offsets"))) This means a 32-bit pointer can
74
-
reference four billion _objects_, rather than four billion bytes. Ultimately, this
75
-
means the heap can grow to around 32 GB of physical size while still using a 32-bit
76
-
pointer.
77
-
78
-
Once you cross that magical ~32 GB boundary, the pointers switch back to
79
-
ordinary object pointers. The size of each pointer grows, more CPU-memory
80
-
bandwidth is used, and you effectively lose memory. In fact, it takes until around
81
-
40–50 GB of allocated heap before you have the same _effective_ memory of a
82
-
heap just under 32 GB using compressed oops.
83
-
84
-
The moral of the story is this: even when you have memory to spare, try to avoid
85
-
crossing the 32 GB heap boundary. It wastes memory, reduces CPU performance, and
86
-
makes the GC struggle with large heaps.
87
-
88
-
==== Just how far under 32gb should I set the JVM?
89
-
90
-
Unfortunately, that depends. The exact cutoff varies by JVMs and platforms.
91
-
If you want to play it safe, setting the heap to `31gb` is likely safe.
92
-
Alternatively, you can verify the cutoff point for the HotSpot JVM by adding
93
-
`-XX:+PrintFlagsFinal` to your JVM options and checking that the value of the
94
-
UseCompressedOops flag is true. This will let you find the exact cutoff for your
95
-
platform and JVM.
96
-
97
-
For example, here we test a Java 1.7 installation on MacOSX and see the max heap
98
-
size is around 32600mb (~31.83gb) before compressed pointers are disabled:
43
+
==== 不要超过 32 GB!
44
+
这里有另外一个原因不分配大内存给 Elasticsearch。事实上((("heap", "sizing and setting", "32gb heap boundary")))((("32gb Heap boundary"))),
45
+
JVM 在内存小于 32 GB 的时候会采用一个内存对象指针压缩技术。
46
+
47
+
在 Java 中,所有的对象都分配在堆上,并通过一个指针进行引用。
48
+
普通对象指针(OOP)指向这些对象,通常为 CPU _字长_ 的大小:32 位或 64 位,取决于你的处理器。
It should be obvious,((("heap", "sizing and setting", "swapping, death of performance")))((("memory", "swapping as the death of performance")))((("swapping, the death of performance"))) but it bears spelling out clearly: swapping main memory
164
-
to disk will _crush_ server performance. Think about it: an in-memory operation
165
-
is one that needs to execute quickly.
132
+
这是显而易见的,((("heap", "sizing and setting", "swapping, death of performance")))((("memory", "swapping as the death of performance")))((("swapping, the death of performance")))但是还是有必要说的更清楚一点:内存交换
133
+
到磁盘对服务器性能来说是 _致命_ 的。想想看:一个内存操作必须能够被快速执行。
166
134
167
-
If memory swaps to disk, a 100-microsecond operation becomes one that take 10
168
-
milliseconds. Now repeat that increase in latency for all other 10us operations.
169
-
It isn't difficult to see why swapping is terrible for performance.
135
+
如果内存交换到磁盘上,一个 100 微秒的操作可能变成 10 毫秒。
136
+
再想想那么多 10 微秒的操作时延累加起来。
137
+
不难看出 swapping 对于性能是多么可怕。
170
138
171
-
The best thing to do is disable swap completely on your system. This can be done
172
-
temporarily:
139
+
最好的办法就是在你的操作系统中完全禁用 swap。这样可以暂时禁用:
173
140
174
141
[source,bash]
175
142
----
176
143
sudo swapoff -a
177
144
----
178
145
179
-
To disable it permanently, you'll likely need to edit your `/etc/fstab`. Consult
180
-
the documentation for your OS.
146
+
如果需要永久禁用,你可能需要修改 `/etc/fstab` 文件,这要参考你的操作系统相关文档。
181
147
182
-
If disabling swap completely is not an option, you can try to lower `swappiness`.
183
-
This value controls how aggressively the OS tries to swap memory.
184
-
This prevents swapping under normal circumstances, but still allows the OS to swap
185
-
under emergency memory situations.
148
+
如果你并不打算完全禁用 swap,也可以选择降低 `swappiness` 的值。
149
+
这个值决定操作系统交换内存的频率。
150
+
这可以预防正常情况下发生交换,但仍允许操作系统在紧急情况下发生交换。
186
151
187
-
For most Linux systems, this is configured using the `sysctl` value:
152
+
对于大部分Linux操作系统,可以在 `sysctl` 中这样配置:
188
153
189
154
[source,bash]
190
155
----
191
156
vm.swappiness = 1 <1>
192
157
----
193
-
<1> A `swappiness` of `1` is better than `0`, since on some kernel versions a `swappiness`
194
-
of `0` can invoke the OOM-killer.
158
+
<1> `swappiness` 设置为 `1` 比设置为 `0` 要好,因为在一些内核版本 `swappiness` 设置为 `0` 会触发系统 OOM-killer(注:Linux 内核的 Out of Memory(OOM)killer 机制)。
195
159
196
-
Finally, if neither approach is possible, you should enable `mlockall`.
197
-
file. This allows the JVM to lock its memory and prevent
198
-
it from being swapped by the OS. In your `elasticsearch.yml`, set this:
0 commit comments