Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

memory

jvm

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.

like image 921
Eddie Deng Avatar asked Jun 03 '15 14:06

Eddie Deng


People also ask

What is native heap memory?

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.

What are the differences between on-heap and off-heap memory usage?

The on-heap store refers to objects that will be present in the Java heap (and also subject to GC). On the other hand, the off-heap store refers to (serialized) objects that are managed by EHCache, but stored outside the heap (and also not subject to GC).

What is off-heap memory?

Off-heap memory refers to the memory allocated directly to the operative system, it can be part of the same physical memory or/and disk access based such as memory mapped-files.

What is native memory?

Native memory is the memory provided to the application process by the operating system. The memory is used for heap storage and other purposes. The native memory information available in the native memory perspective view varies by platform but typically includes the following information: Table 1.


2 Answers

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.

like image 148
Chris K Avatar answered Oct 11 '22 02:10

Chris K


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

like image 42
Joey Trang Avatar answered Oct 11 '22 01:10

Joey Trang