codingenious
codingenious

Reputation: 8653

Java heap inside or outside jvm memory?

Till today I knew that java has heap and that is created by JVM. Further, this memory is allocated by OS to JVM instance, i.e. heap resides inside JVM instance.

But today I saw a picture, enter image description here

which shows, JVM and heap far apart.

So, I am confused right now, Can anyone let me know, whether I was wrong before or I am not able to understand the picture?

Upvotes: 21

Views: 7858

Answers (5)

Jorge Campos
Jorge Campos

Reputation: 23361

There is a lot to discuss on this question. I always like the articles from IBM since it contains very good information. For this specific question, here is an excerpt.

From The native and Java heaps article on IBM:

The JVM maintains two memory areas, the Java™ heap, and the native (or system) heap. These two heaps have different purposes and are maintained by different mechanisms.

The Java heap contains the instances of Java objects and is often referred to as 'the heap'. It is the Java heap that is maintained by Garbage Collection, and it is the Java heap that is changed by the command-line heap settings. The Java heap is allocated using mmap, or shmat if large page support is requested. The maximum size of the Java heap is preallocated during JVM startup as one contiguous area, even if the minimum heap size setting is lower. This allocation allows the artificial heap size limit imposed by the minimum heap size setting to move toward the actual heap size limit with heap expansion.

The native, or system heap, is allocated by using the underlying malloc and free mechanisms of the operating system, and is used for the underlying implementation of particular Java objects; for example:

  • Motif objects required by AWT and Swing
  • Buffers for data compression routines, which are the memory space that the Java Class Libraries require to read or write compressed data like .zip or .jar files.
  • Malloc allocations by application JNI code
  • Compiled code generated by the Just In Time (JIT) Compiler
  • Threads to map to Java threads

Hope it helps you understand.

Upvotes: 16

Jakub Kubrynski
Jakub Kubrynski

Reputation: 14149

Java is using heap and off-heap memory. You can check full memory layout under http://www.slideshare.net/slideshow/embed_code/27597947?startSlide=7

Upvotes: 1

sarah.ferguson
sarah.ferguson

Reputation: 3257

The JVM in green is most likely not a JVM instance but the JVM code which resides in the system memory, inside the Java Heap(s) there are the JVM instances you expected.

If you look at this image below it is clearer, the JVM in green in your image would be inside the Host Operating system blue area

JVM

Upvotes: 16

lichengwu
lichengwu

Reputation: 4307

The jvm is just an application on OS, so it will run on OS's memroy.

OS will allocate memory(native + heap + non-heap) for JVM.

The jvm use native memory and applications run on JVM use heap and non-heap.

Upvotes: 1

kutschkem
kutschkem

Reputation: 8163

You are confusing two things. The first is process memory. Yes the heap is part of the JVM's process' memory. What is labeled "JVM" in your picture is, most likely, the JVM's program code. It makes sense that it is separate from the heap. Actually, almost all programs have separated code / data areas; i am not sure, but i think the OS is partly responsible for that.

Upvotes: 1

Related Questions