In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail what JVM escape is. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
What is a JVM escape?
Without looking at the concept, let's take a look at a piece of code, as follows:
First of all, I'm telling you, this code sends an escape! Why? Look, I'll analyze it!
We all know that objects in Java are allocated to the heap by default, and the garbage collection mechanism collects objects that are no longer used in the heap. However, before this, you need to filter recyclable objects, so it will result in recycling objects and sorting memory, which is time-consuming and expensive. And this is one of the craziest complaints about Java. In fact, Java does not support the allocation of objects on the stack.
The StringBuilder object returned by the function above is originally an internal variable of the method, but it is returned directly at this time, so that StringBuilder may be changed by methods or variables elsewhere, so that its scope is not just the escapeXttblog method, although it is a local variable, it has an "escape accident".
JVM escape analysis
Now that there is a JVM escape, we should have a mechanism to analyze which code may have a "escape accident". So a new discipline was born, which is JVM escape analysis.
Escape analysis (Escape Analysis) is a cross-function global data flow analysis algorithm that can effectively reduce the synchronous load and memory heap allocation pressure in Java programs. Through escape analysis, the Java Hotspot compiler can analyze the scope of use of a reference to a new object and decide whether to assign the object to the heap. Escape analysis is a cutting-edge optimization technology in Java virtual machines at present, but whether it is suitable or not depends on the actual situation.
Escape analysis refers to the method of analyzing pointer dynamic range, which is related to 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, thus being referenced by other methods or threads, a phenomenon called Escape of pointers (or references). Generally speaking, if an object's pointer is referenced by multiple methods or threads, then we say that the object's pointer has escaped.
How to avoid JVM escape?
Like the "escape accident" above, we just need to change the return value of the escapeXttblog method if we want to avoid it.
After changing the returned StringBuilder to String, the StringBuilder is not detached from the method and there will be no escape.
Three common JVM escape codes
Combined with the above examples and concepts, I sorted out three common escape codes, as follows:
The root cause of the escape is due to the limitations of Java itself (objects can only be allocated to the heap). In order to reduce the number of temporary objects allocated in the heap, I will define a local variable in a method body, and this variable does not escape during method execution. According to the JVM tuning mechanism, an instance of the class is first created in the heap memory, and then the reference of this object is pushed into the call stack to continue execution, which is the way before JVM optimization. Then, I use escape analysis to optimize JVM. That is, for the redistribution of the stack, we first find out the unescaped variable and store the variable directly in the stack without entering the heap. After the allocation is completed, we continue to execute in the call stack, and finally the thread execution ends, the stack space is reclaimed, and the local variables are also recovered. In this way, it is in the heap before optimization and in the stack after optimization, thus reducing the allocation and destruction of objects in the heap and optimizing performance.
However, the escape analysis will consume time, so the performance may not be improved, and because the escape analysis is time-consuming, the current implementation uses algorithms that are not so accurate but the time pressure is relatively small to complete the escape analysis. this may lead to unstable results, so it is necessary to deal with it as appropriate according to the actual situation.
A technology is good or bad, not by mouth, suitable for their own is the best.
This is the end of the article on "what is JVM escape". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.