How JVM stack, heap and threads are mapped to physical memory or operation system

Ryan picture Ryan · Apr 28, 2013 · Viewed 19.1k times · Source

The compiler book(The dragon book) explains that value types are created on the stack, and reference types are created on the heap.

For Java, JVM also contains heap and stack in runtime data area. Objects and arrays are created on heap, method frames are pushed to stack. One heap is shared by all threads, while each thread has its own stack. The following diagram shows this:

enter image description here

More about Java run-time data areas.

What I don't understand is that since JVM is essentially a software, how are those JVM heap, stack and threads mapped to physical machine?

I would appreciate it if someone can compare those concept between Java and C++. Because Java runs on JVM, but C++ does not.

To make this question more precise, I want to know the following:

  1. Comparing with Java, What does C++ run-time data area look like? A picture would be helpful, I can't find a good picture like the JVM one above.
  2. How the JVM heap, stack, registers and threads are mapped to operating system? or I should ask how they are mapped to physical machine?
  3. Is it true that each JVM threads is simply a user thread and gets mapped to kernal in some way? (user thread vs kernel thread)

Update: I draw a picture for runtime physical memory of a process.
enter image description here

Answer

Peter Lawrey picture Peter Lawrey · Apr 28, 2013

What I don't understand is that since JVM is essentially a software, how are those JVM heap, stack and threads mapped to physical machine?

The heap is a pre-allocated continuous region of virtual memory. e.g.

 void* heap = malloc(Xmx); // get the maximum size.

The stacks are allocated by the threading library when the thread is started. Again it is a continuous region of virtual memory which is the maximum stack size. Again you could think of it as

 void* stack = malloc(Xss); // get the maximum stack size.

Native threads are OS features which are not part of the JVM space as such.

Because Java runs on JVM, but C++ does not.

C++ still needs a runtime environment and libraries to start up. Try deleting your C++ Runtime or libc and these won't start.

Comparing with Java, What does C++ run-time data area look like?

There is one large region of virtual memory you can use. There isn't a picture because it wouldn't tell you much. Imagine one long rectangle labelled user space.

How the JVM heap, stack, registers and threads are mapped to operating system? or I should ask how they are mapped to physical machine?

Again there is no magic. The JVM heap is a region of memory, a JVM stack is the same a native stack which is what C+ uses, the JVM's registers is the same as native registers which is what C+ uses and JVMs thread are actually native threads which is what C+ uses.

I think you are assuming there is more magic or obscurity going on than there is. Instead you should assume that the simplest, efficient and lightweight design has been used and you won't be far off.

I should ask how they are mapped to physical machine?

one to one basically.