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:
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:
- 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.
- How the JVM heap, stack, registers and threads are mapped to operating system? or I should ask how they are mapped to physical machine?
- 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.
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.
JVM(Java Virtual Machine) acts as a run-time engine to run Java applications. JVM is the one that actually calls the main method present in a java code. JVM is a part of JRE(Java Run Environment).
Java applications are called WORA (Write Once Run Anywhere). This means a programmer can develop Java code on one system and can expect it to run on any other Java enabled system without any adjustment. This is all possible because of JVM.
When we compile a .java file, a .class file(contains byte-code) with the same filename is generated by the Java compiler. This .class file goes into various steps when we run it. These steps together describe the whole JVM.