Shilu
Shilu

Reputation: 81

Java 7 escape analysis is not working

For the below given code , I see lot of GC activity. As per my understanding this is a suitable scenario for EA. Why EA is not effective. DummyObject has nothing allocated inside it. JVM options used : -server , -verbosegc.

   static void anayzeEA()
{
    for(int i = 0 ; i < 100000000; i++) {
        DummyObject obj = new DummyObject();
        if(obj.hashCode() == 97787) { //to prevent the obj being optimized            
         System.out.println(obj.hashCode());
        }
    }

}

Upvotes: 2

Views: 724

Answers (3)

Hardcoded
Hardcoded

Reputation: 6494

Java API says:

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

So you are generating objects, producing non-predictable hashCodes for each and compare them to some value. Additional it's a native method, so the JIT doesn't know what's happening inside.

Escape Analysis may be good, but there's currently no support for crystal balls. ;-) Try overriding it with your own method, returning 12345.

Upvotes: 0

Nitsan Wakart
Nitsan Wakart

Reputation: 2909

See related Q&A here which suggests you can download a debug JDK and use command line options: -XX:+UnlockDiagnosticVMOptions -XX:+PrintEscapeAnalysis -XX:+PrintEliminateAllocations

to print out the escape analysis events as they happen.

Upvotes: 1

Shilu
Shilu

Reputation: 81

Some observations

It seems like the obj.hashCode() is a native call and the object may escape . Changing obj.hashCode() to obj.getMyCode() (a method which returns the System.currentTimeMillis()% staticObjCount) made it work . No GC activity observed. However following method never got escape analysis in effect with all the suggestions mentioned
here

 public static long test1() 
{
    long r = 0;
    byte[] arr = new byte[(int)System.currentTimeMillis() % 1024]; 
    if(arr.length == 998 ) {
        ++r;
    }
    return r;
}

JVM options used -server -verbosegc -XX:CompileThreshold=1

Test1 is called over a number of times . Same old story. Java allocates memory in heap, GC comes and makes everything slow. was too excited about this feature.

Upvotes: 0

Related Questions