Don't get me wrong I think .NET is great, and they've made some smart decisions that allow it to be competitive with much less engineering work on the VM.
As well as not having escape analysis it also lacks tiered compilation which is a much more pressing problem from my perspective, and is being actively tackled now.
There was always the DevTools vs WinDev differences regarding the role of .NET in Windows, and for a while it seemed Microsoft has happy having .NET just be good enough.
Thankfully they have changed their mindset and are improving .NET to be as close to C++ as possible, at least for 99% of the use cases where using a GC language is an acceptable trade off.
An evil example is
class Something
{
private static readonly Something _inst;
public Something()
{
_inst = this;
}
}
Where the reference leaked just by instantiating it. Does java detect that this escaped the stack? How?There isn't one Java, rather lots of it.
So, in what concerns Oracle, there are reports from Oracle Labs that Graal is better than Hotspot on escape analysis.
No idea where IBM, Azul, Aicas, microEJ, PTC stand.
Even less regarding Android, because not only Google has done their own thing, each OEM also has the freedom to change how ART works.
Regarding your actual question, I have this feeling none of them is able to detect it.
It does however also remove half the guarantees about uninitiated object visibility in the java memory model because your leaking the reference inside the constructor.
Shows that the Hotspot doesn't handle it in all scenarios:
> But, EA is not ideal: if we cannot statically determine the object is not escaping, we have to assume it does. Complicated control flow may bail earlier. Calling non-inlined — and thus opaque for current analysis — instance method bails. Doing some things that rely on object identity bail, although trivial things like reference comparison with non-escaping objects gets folded efficiently.
> This is not an ideal optimization, but when it works, it works magnificently well. Further improvements in compiler technology might widen the number of cases where EA works well.
X thing = new ...;
if (slowpath) {
unlikely_function(thing);
}
...
Even if unlikely_function isn't inlined, it can still perform scalar replacement, and push the allocation site into the branch (reconstructing the state of the allocated object as it would've been at that point), which is a big improvement.This in turn lets the inliner be smarter about what it does and doesn't inline, vs c2 which tries to greedily inline everything, partly to assist escape analysis
Well you assigned the instance to a global variable. That's how Java knows. If you can see that then so can the Java compiler.