This is on JDK 17, and all tests are run with 256MB memory. I’ve been testing escape analysis interactions with streams via some simple tests and ran into something interesting.
The code below appears to trigger escape analysis just fine (tested by adding -verbose:gc
and running the code with and without -XX:-DoEscapeAnalysis
. Runs without EA print GC activity while runs with EA do not)
public int mapSum_boxed(){
record TestObject(int i){};
return IntStream.range(0, 1_000_000_000)
.boxed()
.map(x-> new TestObject(1))
.mapToInt(x-> x.i())
.sum();
}
However, the code below fails to trigger escape analysis, and I get GC activity printed to the console:
public int mapSum_boxed(){
record TestObject(int i){};
return IntStream.range(0, 1_000_000_000)
.boxed()
.map(x-> 1) // <------- additional map() op
.map(x-> new TestObject(1))
.mapToInt(x-> x.i())
.sum();
}
Does anyone have any insight as to why this is happening ? I initially thought that the map(x->1)
op was somehow converting the whole stream to Integer
type but that seems unlikely…and that really should’ve happened at the boxed()
stage anyways.