1

I've looked through the different threads, and there are a lot of conflicting information out there. The most useful article I found was this: Java theory and practice: Urban performance legends, revisited but it's from 2005.

So I was wondering what is the state of the Java standard regarding heap allocation. In the release notes for Java 7 it says:

(The server compiler) does not replace a heap allocation with a stack allocation for non-globally escaping objects.

I got the impression from the other article that this is indeed possible to do in the compiler, but it seems to not be required by the standard.

A related question which doesn't seem to answer my question is Stack and Heap memory in Java The accepted answer for this question says that only primitives may be put on the stack. There does not seem to be any consensus on that. Secondly, I'm obviously asking a different question.

I understand that in Java everything is a reference. That said, would a compiler allocate everything on the heap even if it is only reachable from a stack reference?

  • Note that the document you quoted is discussing the HotSpot Virtual Machine, which is a specific implementation. Other implementations can behave differently. As you mentioned, it's allowed but not required by the specification. – Wyzard Jan 31 '16 at 5:17
  • Yes I do realize that. I should have said so in the question. – starflyer Jan 31 '16 at 5:29
  • Possible duplicate of Stack and Heap memory in Java – gnat Jan 31 '16 at 7:17
4

I was wondering what is the state of the Java standard regarding heap allocation.

It only takes a couple of seconds to actually grep the text of the Java Language Specification for all occurrences of the words "stack" or "heap" and inspect those, so the answer is not really hard: it doesn't say anything about stack or heap allocation. And why would it? That's an implementation detail.

The vast majority of hits for "heap" are talking about "heap pollution", which is the name of a condition that can occur with Java Generics, where a reference of a certain generic type can point to an object of a different type. However, this is just a name the designers of Java came up with, it's not necessarily related to a heap.

The vast majority of hits for "stack" are talking about the StackOverflowException. One hit talks about the fact that asynchronous exceptions which are constructed at one point in time but only thrown much later may construct the stack trace lazily. On the rest of the hits are in "example" sections, where the spec talks about how something may be implemented.

So, in short: no, the spec doesn't say anything really about stack and heap. It's perfectly possible to implement Java with only a dynamically resizable stack, and it's perfectly possible to implement Java with only a heap. Of course, you need to keep some information about the call graph, which you may very well do for example in a "stack" implemented as a linked list of heap-allocated stack frames.

  • Yes. Looking and not finding something isn't exactly the definitive proof that something doesn't exist. And I timed myself. It took more like minutes than a couple of seconds. – starflyer Feb 2 '16 at 4:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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