java 堆外、本机堆、直接内存和本机内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30622818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:23:31  来源:igfitidea点击:

off-heap, native heap, direct memory and native memory

javamemoryjvm

提问by Chris

Recently I came across these concepts while learning JVM internals. I am aware that there are already a lot of questions on SO about them individually, but I still cannot grasp the relationship between them or, simply what they are.

最近我在学习 JVM 内部原理时遇到了这些概念。我知道已经有很多关于它们的问题,但我仍然无法理解它们之间的关系,或者只是它们是什么。

Now I describe them as such:

现在我这样描述它们:

  1. Native memorymeans the memory area outside normal JVM heap, but still within the total user space memory spared by OS for JVM process (for example on 32-bit Windows it is by default 2 GB). This space is reserved by JVM to store some internal data, such as Permanent Generation / Method Area etc.
  2. Direct memorymeans you use native memory by means of java.nio.DirectByteBuffer.

  3. Native heapmeans you use native memory by means of unsafe.allocateMemoryor simply do a mallocin your JNI code.

  4. Off-heapis the same as native memory.

  1. 本机内存是指正常 JVM 堆之外的内存区域,但仍在操作系统为 JVM 进程预留的总用户空间内存内(例如,在 32 位 Windows 上,默认情况下为 2 GB)。这个空间是JVM预留的,用来存放一些内部数据,比如永久代/方法区等。
  2. 直接内存意味着您通过java.nio.DirectByteBuffer.

  3. 本机堆意味着您通过unsafe.allocateMemory或简单地malloc在 JNI 代码中使用本机内存。

  4. 堆外与本机内存相同。

And one additional question, is it possible to allocate memory directly outside the total memory space (4GB on 32-bit OS) spared for JVM process ?

还有一个问题,是否可以在为 JVM 进程预留的总内存空间(32 位操作系统上为 4GB)之外直接分配内存?

Please point out the mistakes in my understanding and if possible, give a clear description about them.

请指出我的理解中的错误,如果可能,请对其进行清楚的描述。

采纳答案by Chris K

1) Heap memory: memory within the JVM process that is used to hold Java Objects and is maintained by the JVMs Garbage Collector.

1)堆内存:JVM 进程内的内存,用于保存 Java 对象并由 JVM 垃圾收集器维护。

2) Native memory/Off-heap: is memory allocated within the processes address space that is not within the heap and thus is not freed up by the Java Garbage Collector.

2) 本机内存/堆外:是在进程地址空间内分配的内存,该内存不在堆内,因此不会被 Java 垃圾收集器释放。

3) Direct memory: is similar to native, but also implies that an underlying buffer within the hardware is being shared. For example, a buffer within the network adapter or graphics display. The goal here is to reduce the number of times the same bytes is being copied about in memory.

3)直接内存:类似于本机,但也暗示硬件内的底层缓冲区正在被共享。例如,网络适配器或图形显示中的缓冲区。这里的目标是减少在内存中复制相同字节的次数。

Finally, depending upon the OS then extra native allocations (assigning of the memory address space) can be carried out via Unsafe alloc and/or by memory mapping a file. Memory mapping a file is especially interesting as it can easily allocate more memory than the machine currently has as physical ram. Also note, that the total address space limit is restricted by the size of a pointer being used, a 32bit pointer cannot go outside of 4GB. Period.

最后,根据操作系统,额外的本地分配(内存地址空间的分配)可以通过 Unsafe alloc 和/或通过内存映射文件来执行。内存映射文件特别有趣,因为它可以轻松分配比机器当前作为物理内存拥有的内存更多的内存。另请注意,总地址空间限制受所用指针大小的限制,32 位指针不能超出 4GB。时期。

回答by the8472

And one additional question, is it possible to allocate memory directly outside the total memory space (4GB on 32-bit OS) spared for JVM process ?

还有一个问题,是否可以在为 JVM 进程预留的总内存空间(32 位操作系统上为 4GB)之外直接分配内存?

4GB is the total virtual address space limit for a process on a 32bit OS. 4-byte pointers simply cannot address more than that.

4GB 是 32 位操作系统上进程的总虚拟地址空间限制。4 字节指针根本无法寻址更多。

The only thing you can do is opening a large file and interacting with it through a limited amount of memory-mapped buffers, mapping and releasing them as needed and hoping that the OS page cache keeps them in physical memory.

您唯一能做的就是打开一个大文件并通过有限数量的内存映射缓冲区与其交互,根据需要映射和释放它们,并希望操作系统页面缓存将它们保留在物理内存中。

If you need more than 2GB of memory you really should work with a 64bit OS and JVM.

如果您需要超过 2GB 的内存,您真的应该使用 64 位操作系统和 JVM。

回答by Joey Trang

a lot of high performant server application which run on JVM have use off-heap memory to increase performance of server such as Apache Cassandra. It used to store most of data structure on heap but in recent releases, it has been stored on off-heap memory

许多在 JVM 上运行的高性能服务器应用程序都使用堆外内存来提高服务器(例如 Apache Cassandra)的性能。它曾经将大部分数据结构存储在堆上,但在最近的版本中,它已存储在堆外内存中