简体   繁体   中英

What is the difference between off-heap, native heap, direct memory and native memory?

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.

Now I describe them as such:

  1. Native memory means 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 memory means you use native memory by means of java.nio.DirectByteBuffer .

  3. Native heap means you use native memory by means of unsafe.allocateMemory or simply do a malloc in your JNI code.

  4. Off-heap is the same as native memory.

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?

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

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

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.

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.

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.

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

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 ?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM