Java Magazine, Mar/Apr 2018
ORACLE COM JAVAMAGAZINE MARCH APRIL 2018 76 inside the jvm ArgEscape 2 An object does not escape method or thread but it is passed as argument to call or referenced by argument and it does not escape during call GlobalEscape 3 An object escapes the method or thread The first option suggests that the object can be replaced by a scalar substitute This elimination is called scalar replacement This means that the object is broken up into its component fields which are turned into the equivalent of extra local variables in the method that allocates the object Once this has been done another HotSpot VM JIT technique can kick in which enables these object fields and the actual local variables to be stored in CPU registers or on the stack if necessary One of the major challenges of the Java platform is the sophistication of the execution model In this case just by looking at the Java source code you might naively conclude that the object r1 does not escape the main method but that r2 is passed as an argument to the sameArea method on r1 and so it escapes the scope of the main method Using the previous classifications it would appear at first sight that r1 should be treated as a NoEscape and r2 should be treated as an ArgEscape however this would be a dangerous conclusion for several reasons First of all recall that method calls in Java are replaced by the Java compiler with invoke bytecodes These operate by setting up the stack with the destination of the call known as the receiver object and with any arguments before the call of the appropriate method is looked up and dispatched that is executed This means that the receiver object is also passed to the method being called it becomes the this object in the method that is called So receiver objects also escape the current scope in this case that would mean that both r1 and r2 would be classified as ArgEscape if EA were to be applied to the code as it appears in the Java source code
You must have JavaScript enabled to view digital editions.