Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is frame in JVM heap allocated or stack allocated?

Tags:

java

jvm

The JVM Specification (JSE 8 Edition) mentioned:

Page 12: 2.5.2 JVM Stacks: "Because the JVM stack is never manipulated directly except to push and pop frames, frames may be heap allocated."

Page 15: 2.6: Frames: "Frames are allocated from the JVM stack of the thread creating the frame." And on Page 16: "Note that a frame created by a thread is local to that thread and cannot be referenced by any other thread."

This sounds quite confusing to me. Since a frame is local to the thread that creates the frame, why allocate the frame in heap since heap is shared among all JVM threads? It does not make sense unless something is missed here. The sentence on Page 12 is an interesting statement.

Any hints? Thanks.

like image 797
r ne Avatar asked Nov 04 '14 18:11

r ne


1 Answers

JVM stack is an abstraction. It may be allocated anywhere or nowhere at all. E.g. if a method is inlined by JIT, it does not have a separate stack frame.

HotSpot JVM uses native thread stack as JVM stack. However there are JVM implementations (CLDC HI, for instance) that allocate JVM stacks in Java Heap. The benefit of this approach is to have a single memory management for everything including thread stacks. Such JVM may run on a platform without standard memory manager (like libc) or even without OS.

JVM Heap is also an abstraction. It is not more "shared" between JVM threads than native thread stacks. The heap may have thread-local areas as well. At the same time native threads stacks reside in virtual memory which is also shared among all threads of the process.

like image 70
apangin Avatar answered Nov 05 '22 14:11

apangin