java JVM 堆栈、堆和线程如何映射到物理内存或操作系统
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16264118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How JVM stack, heap and threads are mapped to physical memory or operation system
提问by Ryan
The compiler book(The dragon book) explains that value types are created on the stack, and reference types are created on the heap.
编译器书(The Dragon Book)解释了在栈上创建值类型,在堆上创建引用类型。
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:
对于 Java,JVM 还包含运行时数据区中的堆和栈。对象和数组在堆上创建,方法帧被推送到堆栈。一个堆由所有线程共享,而每个线程都有自己的堆栈。下图显示了这一点:
More about Java run-time data areas.
有关Java 运行时数据区的更多信息。
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?
我不明白的是,既然 JVM 本质上是一个软件,那么那些 JVM 堆、堆栈和线程是如何映射到物理机的?
I would appreciate it if someone can compare those concept between Java and C++. Because Java runs on JVM, but C++ does not.
如果有人可以比较 Java 和 C++ 之间的这些概念,我将不胜感激。因为 Java 在 JVM 上运行,而 C++ 不是。
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)
- 与Java相比,C++运行时数据区是什么样的?图片会很有帮助,我找不到像上面的 JVM 那样的好图片。
- JVM 堆、堆栈、寄存器和线程如何映射到操作系统?或者我应该问它们如何映射到物理机?
- 每个 JVM 线程都只是一个用户线程并以某种方式映射到内核,这是真的吗?(用户线程与内核线程)
Update:
I draw a picture for runtime physical memory of a process.
更新:我为进程的运行时物理内存绘制了一张图片。
采纳答案by Peter Lawrey
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?
我不明白的是,既然 JVM 本质上是一个软件,那么那些 JVM 堆、堆栈和线程是如何映射到物理机的?
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.
本机线程是不属于 JVM 空间的操作系统特性。
Because Java runs on JVM, but C++ does not.
因为 Java 在 JVM 上运行,而 C++ 不是。
C++ still needs a runtime environment and libraries to start up. Try deleting your C++ Runtime or libc and these won't start.
C++ 仍然需要运行时环境和库来启动。尝试删除您的 C++ 运行时或 libc,它们将无法启动。
Comparing with Java, What does C++ run-time data area look like?
与Java相比,C++运行时数据区是什么样的?
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?
JVM 堆、堆栈、寄存器和线程如何映射到操作系统?或者我应该问它们如何映射到物理机?
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.
再次没有魔法。JVM 堆是一个内存区域,JVM 堆栈与 C+ 使用的本机堆栈相同,JVM 的寄存器与 C+ 使用的本机寄存器相同,JVM 线程实际上是 C+ 使用的本机线程.
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.
基本上是一对一的。