Almas Abdrazak
Almas Abdrazak

Reputation: 3632

Why JIT C2 does not run an escape analysis?

I am experimenting with a C2 server compiler in OpenJDK 11. For some background, after reading this blog I got curious if using Optional class inside of a method won't make any object allocations.

Here is my code

import java.util.Optional;

public class Hello{

    public static void main(String[] argc){
        long sum = 0;
        for(int i =0;i<10000000;i++){
            sum+=(compute(i %2 == 0 ? "1":null));
        }
        System.out.println(sum);
    }

    static int compute(final String obj){
       // Optional.ofNullable internally creates an instance of Optional
        return Optional.ofNullable(obj).map(String::length).orElse(2);
    }

}

My java version is

openjdk 11.0.17 2022-10-18 LTS
OpenJDK Runtime Environment Corretto-11.0.17.8.1 (build 11.0.17+8-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.17.8.1 (build 11.0.17+8-LTS, mixed mode, sharing)

After running it with

java -server \
     -verbose:gc \
     -XX:-DoEscapeAnalysis Hello

I am getting this output(I see an expected young GC as escape analysis is disabled)

[0.031s][info][gc] Using G1
[0.071s][info][gc] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 24M->1M(248M) 3.192ms
15000000

However even if I add an escape analysis JVM still runs a young GC

java -server \
     -verbose:gc \
     -XX:+DoEscapeAnalysis Hello
[0.032s][info][gc] Using G1
[0.072s][info][gc] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 24M->1M(248M) 3.201ms
15000000

I wonder why this is the case. Thanks in advance !

Upvotes: 1

Views: 99

Answers (0)

Related Questions