Java escape analysis and code examples

Concept introduction

We all know that objects created in Java are allocated to heap memory, but the fact is not so absolute. Through the analysis of the process of Java object allocation, we can know that there are two places that will cause objects created in Java to be on the heap. These two points are escape analysis in Java and TLAB (thread local allocation buffer) thread private cache.

Introduction to basic concepts

Escape analysis is a cross function global data flow analysis algorithm that can effectively reduce the synchronization load and memory heap allocation pressure in Java programs. Through escape analysis, the Java hotspot compiler can analyze the use range of the reference of a new object and decide whether to allocate the object to the heap.

In the optimization principle of computer language compiler, escape analysis refers to the method of analyzing the dynamic range of pointer, which is related to the 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 referenced globally, which will be referenced by other processes or threads. This phenomenon is called escape of pointer (or reference). Generally speaking, if the pointer of an object is referenced by multiple methods or threads, we say that the pointer of the object has escaped.

Java is supported in Java se 6u23 and later versions, and the escape analysis option is enabled by default. Java's hotspot JIT compiler can perform escape analysis on the code when the method is overloaded or the code is loaded dynamically. At the same time, the characteristics of Java object allocation on the heap and built-in threads make escape analysis an important function of Java.

Code example

What are the benefits of escape analysis for the java compiler? We know that Java objects are always allocated in the heap, so the creation and recycling of Java objects are very expensive to the system. One of the criticisms of the Java language is that Java does not support the allocation of objects on the stack. The bottleneck of swing memory and performance consumption in JDK6 is that GC traverses the reference tree and reclaims memory. If the number of objects is large, it will bring great pressure to GC and indirectly affect performance. Reducing the number of temporary objects allocated in the heap is undoubtedly the most effective optimization method. There is a common scenario in Java applications. Generally, a local variable is declared in the method body, and the variable does not escape during the method execution life cycle. According to the JVM memory allocation mechanism, First, an instance of the class is created on heap memory (object), and then push the reference of this object into the call stack to continue execution. This is the way before JVM optimization. Of course, we can use escape analysis to optimize the JVM. That is, for the stack reallocation method, first we need to analyze and find the variables that have not escaped, and allocate the instantiation memory of this variable class directly in the stack without entering the heap. The allocation is completed After that, continue to call the execution in the stack. Finally, the thread execution ends, the stack space is recycled, and the local variable object is also recycled. The main difference between the optimization in this way and the scheme before optimization lies in the storage medium of the object. Before optimization, it is in the heap, and after optimization, it is in the stack, so as to reduce the allocation of temporary objects in the heap (more time-consuming), so as to optimize the performance.

Use escape analysis for performance optimization (- XX: + doescapeanalysis enable escape analysis)

This code can allocate memory on the stack because there is no pointer escape, that is, the reference does not expose the method body.

Stack and heap memory allocation comparison

The JVM parameters are - server - xmx10m - xms10m - XX: - doescapeanalysis - XX: + printgc. The running results are as follows:

The JVM parameters are - server - xmx10m - xms10m - XX: + doescapeanalysis - XX: + printgc. The running results are as follows:

performance testing

Optimize JVM output using escape analysis (- server - XX: + doescapeanalysis - XX: + printgc)

JVM output results optimized without escape analysis (- server - xmx10m - xms10m - XX: - doescapeanalysis - XX: + printgc)

The analysis results show that the performance is optimized by 1 / 6.

summary

The above is all about the detailed explanation of Java escape analysis and code examples in this paper. I hope it will be helpful to you. Interested friends can continue to refer to this site: detailed explanation of Java implementation producer consumer problems and reader writer problems, complete code example of Java algorithm implementation red black tree, function of Java programming interface call and code sharing, etc. you can leave a message at any time and the editor will reply to you in time. Thank you for your support.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>