Eligibility for escape analysis / stack allocation with Java 7

后端 未结 3 1426
盖世英雄少女心
盖世英雄少女心 2020-12-06 11:44

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

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 11:58

    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

    • all method calls that use it are inlined
    • it is never assigned to any static or object fields, only to local variables (parameters to inlined method calls become local variables)
    • at each point in the program, which local variables contain references to the object must be JIT-time determinable, and not depend on any unpredictable conditional control flow.
    • If the object is an array, its size must be JIT-time constant and indexing into it must use JIT-time constants.

    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.

提交回复
热议问题