I am doing some tests with escape analysis in Java 7 in order to better understand what objects are eligible to stack allocation.
Here is the code I wrote to test st
I just investigated the same thing, but for Java 8. I put my answer in a duplicate question as I didn't find this one in time.
Summary from the full answer:
First of all, it's implementation dependent. This answer applies to OpenJDK 1.8 and probably also the Oracle JVM 1.8.
Secondly, as others have stated, stack allocation only happens when a method is compiled by the C2 compiler, which only happens once a method has been called enough times.
If so, objects can be stack allocated if
The inlining especially is not predictable if you don't know some of the specific quirks of Hotspot. See the linked answer for some details.
Edit: I tried running your test on java 8 (OpenJDK), and everything is inlined there. So there are differences in stack allocation between java 7 and 8.