Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of JVM memory escape?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "What is the principle of JVM memory escape", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "What is the principle of JVM memory escape"!

Summary of premises

JVM memory allocation is mainly in the runtime data area (Runtime Data Areas), and runtime data area is divided into: method area, heap area, PC registers, Java virtual machine stack (that is, stack area, official documents or called Java virtual machine stack), local method area, memory escape is mainly caused by changes in the dynamic scope of objects, so the analysis of memory escape is to analyze the dynamic scope of objects.

method escape

What's a way to escape? For example, after defining an object in a method (local within the method), this object is referenced by an external method, such as returning it as a return value and passing it to other places. When the method is executed at the end of GC, the object in this method should have been recycled, but it is found that the object is still alive and cannot be recycled. It is called method escape:

public static StringBuffer getStringBuffer(String str1,String str2) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(str1); stringBuffer.append(str2); return stringBuffer;}

Although stringBuffer in the above code is a local variable within the method, because stringBuffer is returned as a return value, stringBuffer may be changed by other methods, and the scope is not only within the method, so it escapes outside the method. Yeah, it's a prison break.

How do I prevent stringBuffer from escaping the method? So don't return stringBuffer object directly can not it! For example, the following code:

public static StringBuffer getStringBuffer(String str1,String str2) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(str1); stringBuffer.append(str2); return stringBuffer.toString();}

Thread escape: In the above example, the object is returned directly, and the object may be accessed by an external thread, such as assigning to a class variable, etc., called thread escape.

In general, when a pointer to an object is referenced by more than one method or thread, we say that the pointer to the object has escaped.

Optimization: Proving that an object does not escape outside a method or thread. Allocation on stack:

In heap space for memory allocation, GC in heap space can filter objects, reclaim objects, clean up memory need to waste time, if escape analysis can determine that some objects are certainly not out of the method, you can directly let this object on the stack allocation memory, the object with the end of the execution of the method stack frame and destruction, reducing the pressure of GC.

Synchronous cancellation:

Thread synchronization itself is time-consuming. If it is determined that a variable will not escape the thread and cannot be accessed by other threads, there will be no competition for reading and writing of this variable, and the synchronization measures for this variable can be cleared.

Scalar substitution:

Scalars: Java primitive data types (int, char, long, etc.) cannot be decomposed any further, so they can be called scalars.

Aggregate: If a piece of data can continue to be decomposed, it is called aggregate, and objects are typical aggregates.

If escape analysis proves that an object cannot escape methods, cannot be accessed externally, and that the object can be decomposed, then the program may not create the object when it actually executes, but instead create the decomposed scalar of the object. This eliminates the need to allocate space for objects, and only allocates memory on the stack for decomposed variables.

All in all:

Escape analysis is time-consuming, so the performance may not improve a lot, because its time-consuming, the algorithm used is not so accurate but the time pressure is relatively small algorithm to complete, which may lead to unstable results, to be used with caution.

Due to the complexity of stack allocation implementation caused by the current implementation method of HotSpot virtual machine, this optimization has not been implemented in HotSpot virtual machine for the time being. OSR has not been implemented on Hotspot for the time being.

Related JVM parameters:

-XX:+DoEscapeAnalysis Turn on escape analysis, -XX:+PrintEscapeAnalysis Turn on escape analysis, you can view the analysis results through this parameter.

-XX:+EliminateAllocations Turn on scalar substitution-XX:+EliminateLocks Turn on synchronization elimination-XX:+PrintEliminateAllocations After scalar substitution is turned on, view scalar substitution.

At this point, I believe that everyone has a deeper understanding of "what is the principle of JVM memory escape", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report