Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 972
Ryan Avatar asked Apr 28 '13 14:04

Ryan


People also ask

How heap memory and Stack memory works in JVM?

JVM has divided memory space between two parts one is Stack and another one is Heap space. Stack space is mainly used for storing order of method execution and local variables. Stack always stored blocks in LIFO order whereas heap memory used dynamic allocation for allocating and deallocating memory blocks.

How does Java JVM allocate Stack for each thread?

Each thread has its own stack. When a Java application is started, we use the JVM option -Xms and -Xmx to control the size of heap and -Xss to control the stack size. My understanding is that the heap being created becomes a "managed" memory of JVM and all the object being created are placed there.

How does JVM allocate memory?

JVMs allocate memory on an as needed basis from the operating system. Generally, when the JVM starts, it will allocate the minimum memory allocated (Xms) to the application that is running. As the application requires more memory, it will allocate blocks of memory until the maximum allocation (Xmx) has been reach.

How does heap and Stack memory work?

Overview. Stack memory is the space allocated for a process where all the function calls, primitive data types (int, double, etc.) and local and reference variables of the functions are stored. On the other hand heap memory is used to store the objects that are created during the execution of a Java program.


1 Answers

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.

like image 73
Peter Lawrey Avatar answered Sep 20 '22 20:09

Peter Lawrey