When memory is allocated in Java object creation

I. Introduction knowledge bedding

    1, escape objects: objects created in a method that was not referenced outside the object is called object does not escape.

  2, JDK1.6 later HotSpot virtual machine runtime objects escape analysis support.

  3, JVM configuration parameters:

. 1   . 1) -XX: + PrintFlagsInitial - default parameters to view information in the JVM
 2  
. 3   2) - XX: + DoEscapeAnalysis - escape analysis target opening (JDK8 enabled by default)
 . 4  
. 5   . 3) - XX: -DoEscapeAnalysis - Close object escape analysis
 . 6  
. 7   . 4) -XX: + PrintGC - basic information output of the GC
 . 8  
. 9   . 5) -XX: + PrintGCDetails - Details of the GC output
 10  
. 11   . 6) -Xmx5m / -Xms5m - maximum / minimum heap configuration

 

 

Second, memory allocation when the object is created

  1, there may be allocated on the heap may also be allocated on the stack when the object was created.

  2, small objects and creates internal methods may not escape allocated on the stack.

  3, JDK8 turned on by default escape analysis, implementation will improve the performance of the JVM.

  4 , when a design object, if the shared object is not multi-thread, a method of sharing a plurality of, at this time, references to objects should make use of local variables.

Third, the impact of escape analysis on and off the execution performance of the JVM

  First, let's look at an example.

 1 public class TestObjectInstance01 {
 2     public static void main(String[] args) {
 3         long start = System.currentTimeMillis();
 4         for (int i = 0; i < 100000000; i++) {
 5             //调用alloc()方法
 6             alloc();
 7         }
 8         long end  = System.currentTimeMillis();
 9         System.out.println("运行时间:"+(end-start));
10     }
11     
12     private static voidthe alloc () {
 13 is          // Create a byte can store only one array object 
14          byte [] = ARR new new  byte [. 1 ];
 15          ARR [0] = 10 ;
 16      }
 . 17 }

  We configured the maximum / minimum JVM heap parameters, and to open the escape analysis:

-Xmx5m -Xms5m -XX:+DoEscapeAnalysis -XX:+PrintGC

  operation result:

  Then we closed the escape analysis:

-Xmx5m -Xms5m -XX:-DoEscapeAnalysis -XX:+PrintGC

  operation result:

 

  Result analysis:

    We can see that, when turned on escape analysis, operating performance JVM is much higher than when not turned on. Because objects allocated on the stack does not need to start the GC to recover, when a method call stack, the object is automatically destroyed.

 

 

  

Guess you like

Origin www.cnblogs.com/shi-zhe/p/11580372.html