Go Escape analysis of Java memory objects

Source: Internet
Author: User

Escape analysis in English for escape analyses. In the principle of computer language compiler optimization, escape analysis refers to the method of analyzing pointer dynamic range, which is associated with pointer analysis and shape analysis of compiler optimization principle. When a variable (or object) is allocated in a method, its pointer may be returned or globally referenced, which is referred to by other procedures or threads, which is called a pointer (or reference) escape (escape).

In the Java Concurrency Programming book There is an example listing 3-7 referring to this escape. I didn't think about it at first. The main problem with discovering code is that an anonymous class was created in the constructor, and then the anonymous class was published.

Java code
  1. Import java.util.*;
  2. Public class Thisescape {
  3. private final int num;
  4. Public Thisescape (EventSource source) {
  5. Source.registerlistener (
  6. New EventListener () {
  7. public void OnEvent (Event e) {
  8. DoSomething (e);
  9. }
  10. });
  11. num = 42;
  12. }
  13. private void dosomething (Event e) {
  14. if (num! = ) {
  15. System.out.println ("Race condition detected");
  16. }
  17. }
  18. }

If this code is compiled, then Jd-gui can see DoSomething (e); It will become

ThisEscape.this.doSomething (e); This should be the standard way for the Java inner class to call the external class-the name of the external classes. This. Method name

If we dig further, we know that Thisescape will have two class files after compiling. One is Thisescape.class, the other one will be thisescape$1.class to this anonymous class.

Their contents are almost like this.

Java code
  1. Public class Thisescape {
  2. private final int num;
  3. Public Thisescape (EventSource source) {
  4. Source.registerlistener (new <strong><span style="color: #ff0000;"  >thisescape$1</span></strong> (this));
  5. num = 42;
  6. }
  7. private void dosomething (Event e) {
  8. if (num! = )
  9. System.out.println (
  10. "Race condition detected");
  11. }
  12. static void access$(thisescape _this, event event) {
  13. _this.dosomething (event);
  14. }
  15. }

Java code
  1. Class thisescape$1 implements EventListener {
  2. final thisescape this$0;
  3. thisescape$1 (<span style="color: #ff0000;" >thisescape thisescape</span>) {
  4. this$0 = thisescape;
  5. super ();
  6. }
  7. public void OnEvent (Event e) {
  8. thisescape.access$(this$0, E);
  9. }
  10. }

Thus, if new thisescape$1(this) is executed in another thread, it may cause Num to escape the this to have not yet executed num = 42; Race condition detected will be displayed in the console at intervals.

Go Escape analysis of Java memory objects

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.