JVM Escape Analysis Doescapeanalysis

Source: Internet
Author: User
JVM Escape Analysis

The JVM is composed of stacks, heaps, method areas, local stacks, etc.


Stack: Each method is executed at the same time to create a stack frame for storing local variable table, Operation Stack, dynamic link, method exit and other information. Each method is called until the completion of the process, corresponding to a stack frame in the virtual machine stack from the stack to the process of the stack.


Heap: When instantiating an object, the object is allocated to the heap, and then the reference to the heap is pressed into the stack.


escape: When a pointer to an object is referenced by more than one method or thread, we call this pointer a runaway, generally returning an object and assigning a value to a global variable will generally occur.


Escape Analysis: the method used to analyze this escaping phenomenon is called escape analysis.


Escape Analysis optimization-Stack allocation: the allocation on the stack means that the generated instances of local variables (no escapes) are allocated on the stack, are not allocated in the heap, the allocation is completed, the execution continues within the call stack, the last thread ends, the stack space is reclaimed, and the local variable objects are recycled.


Code testing.

Import java.lang.management.ManagementFactory;
Import java.util.List;


/**
 * Escape analysis optimization-stack allocation on the stack
 , meaning that the method within the local variable (no escape) generated instances are allocated on the stack, not allocated in the heap, after the allocation is complete, continue to execute within the call stack, the last thread ends, the stack space is recycled, the local variable object is also recycled.
 * Typically generated instances are placed in the heap, and then the pointer or reference to the instance is pressed into the stack.
 * The virtual machine parameter settings are as follows, indicating that the run-  time analysis consumes less than 10 milliseconds
 *-server-xmx10m-xms10m
   -XX:+DOESCAPEANALYSIS-XX:+PRINTGC
 * 
 * Virtual machine parameters are set as follows, indicating that no escape analysis is consumed for more than 1000 milliseconds
 *-server-xmx10m-xms10m
   -XX:-DOESCAPEANALYSIS-XX:+PRINTGC
 * @author 734621
 * * *
 /Public
class Onstack {public
  static void Alloc () {
	  byte[] b = new Byt E[2];
	  B[0] = 1;
  }
  
  public static void Main (String [] args) {
	  long B = System.currenttimemillis ();
	  for (int i=0;i<100000000;i++) {
		  alloc ();
	  }
	  Long e = System.currenttimemillis ();
	  System.out.println ("Time to Consume:" + (E-b));
	  
	  list<string> paramters = Managementfactory.getruntimemxbean (). getinputarguments ();
	  for (String p:paramters) {
		  System.out.println (p);}}
}

Printing results:

Results of the escape analysis

[GC (Allocation Failure) 2816k->484k (9984K), 0.0013117 secs]
Time to consume: 7
-xmx10m
-xms10m
-xx:+doescapeanalysis
-xx:+printgc
-dfile.encoding=gbk

The results of the no-escape analysis are as follows:

[GC (Allocation Failure) 3320k->504k (9984K), 0.0003174 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002524 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002618 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001474 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002843 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002922 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002190 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003259 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002738 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001946 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0028288 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0004558 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0106963 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002351 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001471 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002494 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002187 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002732 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001847 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002922 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002773 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002999 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002017 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001205 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002905 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002952 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002676 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001647 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001319 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001319 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002744 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002931 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001762 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001480 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002884 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001659 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002990 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003104 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0004854 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002767 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002489 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001392 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002272 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002641 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002826 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003180 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002714 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002166 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002749 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003793 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002362 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002714 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002764 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002981 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002723 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002324 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002647 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002591 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002875 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001820 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002729 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002931 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002251 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002676 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003130 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002143 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002881 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002603 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002556 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003966 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002749 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002949 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0006170 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0249173 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002620 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001914 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0028737 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0006000 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003945 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002313 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002881 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002544 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002140 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001773 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002650 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002943 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002201 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003274 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001381 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002442 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003031 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003465 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001577 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003189 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002609 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002348 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002216 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0009793 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001263 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002843 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002588 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002175 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0025132 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002579 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002491 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0005171 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003189 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002497 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002471 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001747 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0104052 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002840 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0009805 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0105928 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002620 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0038738 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002116 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002157 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0110542 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0104225 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002899 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002474 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001946 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003013 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002776 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003992 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003031 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002597 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003230 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003916 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002820 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002509 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002650 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002442 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0055639 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0109589 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0009693 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0020453 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0037897 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0109237 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002914 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002685 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0109944 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002720 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002644 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002638 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002471 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003101 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002518 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002858 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002752 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003453 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002609 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0108493 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002298 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0066162 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003078 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002615 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002673 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002532 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002659 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001762 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002937 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002234 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0009092 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002987 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002149 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002568 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002362 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002521 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001650 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003233 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002360 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001700 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002248 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0004145 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0008594 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0029256 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003189 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003497 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003242 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002116 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002837 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002931 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0002553 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0003579 secs]
[GC (Allocation Failure) 3320k->504k (9984K), 0.0001850 secs]
Time to consume: 1150
-xmx10m
-xms10m
-xx:-doescapeanalysis
-xx:+printgc
-dfile.encoding=gbk


The above test can be seen that the stack on the allocation can significantly improve efficiency



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.