11

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.

1
  • "may be heap allocated", my guess is, may be is there because it is implementation dependent and no inherent limitations from JVM specification. JVM implementer has freedom to implement the stack whichever the way they want.
    – kosa
    Nov 4, 2014 at 18:14

1 Answer 1

8

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.

2
  • Good background introduction, but no direct hint to the answer of this question yet...
    – r ne
    Nov 13, 2014 at 2:07
  • @user1139247 Why not? "Is frame in JVM heap allocated or stack allocated?" - I've told both kinds of JVM implementation exist. Most popular one, HotSpot JVM, uses native stacks. Why allocate the frame in heap since heap is shared among all JVM threads? - I've also told about benefits of such approach.
    – apangin
    Nov 13, 2014 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.