In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how Java objects allocate memory on the stack". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how Java objects allocate memory on the stack".
1 escape analysis
The advanced optimization technology in JVM, such as the analysis of similar inheritance relations, is not to optimize the code directly, but to provide a basis for other optimization measures.
Analyze the dynamic scope of an object, and when an object is defined in a method, it may
Method escape
To be referenced by an external method, such as passed as a parameter to another method
Thread escape
Accessed by external threads, such as assigning values to instance variables that can be accessed in other threads
So the escape degree of the Java object from low to high is:
Do not escape = "
Method escape = "
Thread escape
If you can determine an object
Does not escape outside the method or thread (that is, other methods or threads cannot access the object)
Or a lower degree of escape (only escaping the method, not the thread)
Different degrees of optimization can be taken for the object instance.
2 optimization scheme
2.1 allocation on stack (Stack Allocations)
Due to complexity and other reasons, this optimization has not been done in HotSpot yet, but it is used by some other virtual machines (such as Excelsior JET).
JVM's GC module reclaims objects that are no longer used in the heap, but the following recycling action
Tag to filter out recyclable objects
Reclaim and organize memory
It takes a lot of resources.
If it is determined that an object will not escape from the thread, it is a good idea to have the object allocate memory on the stack, and the memory space occupied by the object can be destroyed as the stack frame goes off the stack.
In general applications, local objects that will not escape at all and objects that will not escape threads account for a large proportion. If allocation on the stack can be used, a large number of objects will be automatically destroyed with the end of the method, and the pressure of the GC system will be greatly reduced.
Allocation on the stack supports method escape, but not thread escape.
2.2 Scalar substitution (Scalar Replacement)
2.2.1 Scalar
If a data can no longer be decomposed into smaller data, the original data types in JVM (such as int, long and reference types) can not be further decomposed, and these data are scalars.
2.2.2 amount of polymerization
If a data can continue to be decomposed, it is called Aggregate, for example, the Java object is the aggregate quantity.
2.2.3 Scalar substitution
Disassemble a Java object and restore its member variables to the original type according to the access of the program.
If escape analysis can prove that an object will not be accessed externally by the method, and that the object can be decomposed, then when the program is actually executed, it may not create the object, but directly create several of its member variables used by this method.
After splitting the object:
Allows the member variables of an object to be allocated, read and write on the stack (the data stored on the stack is most likely to be allocated by JVM to the high-speed register of the physical machine).
Optimize the conditions for subsequent progress
2.2.4 applicable scenarios
Scalar substitution can be regarded as a special case of allocation on the stack, which is simpler (without considering the allocation of the complete structure of the object), but it requires a higher degree of escape, and it does not allow the object to escape from the scope of the method.
2.3 synchronous cancellation (Synchronization Elimination)
Thread synchronization is a relatively time-consuming process, and if escape analysis can determine that a variable will not escape from the thread, that is, it will not be accessed by other threads, then there will certainly be no thread competition in the reading and writing of the variable, and the synchronization measures implemented on the variable can also be safely eliminated.
The paper on escape analysis was published in 1999, but it was not until JDK 6 hotspot that it began to support escape analysis, and it is not yet mature, mainly because the computational cost of escape analysis is so high that the performance benefit can not be guaranteed to exceed its consumption. In order to judge whether an object will escape or not, a series of complex data flow sensitive interprocedural analysis is needed to determine the impact on the object when each branch of the program is executed. The high-pressure analysis algorithm of inter-process analysis is the weakness of just-in-time compilation. Just imagine, if it is found that few objects that do not escape can be found after the escape analysis, then the time spent in these runs will be wasted, so JVM can only use algorithms that are not so accurate but have relatively low time pressure to complete the analysis.
C and C++ native support on the stack allocation (without the use of new), flexible use of stack memory, Java is indeed a vulnerable group.
In the Valhalla project, which is still in the experimental stage, a new inline keyword is designed to define the inline type of Java and to mark the value type of C #. With this identification and constraint, escape analysis will be much easier to do in the future.
3 code verification in practice
3.1 No optimized code at all
Public int test (int x) {int xx = x + 2; Point p = new Point (xx, 42); return p.getX ();}
3.2 optimize step1: inline constructor and getX () method
Public int test (int x) {int xx = x + 2; / / allocate P object Point p = point_memory_alloc () in the heap; / / Point constructor is inlined after P.X = xx; p.y = 42; / / Point::getX () is inlined after return p.x;}
Optimized step2: scalar replacement
After the escape analysis, it is found that the Point object instance will not escape to any extent within the scope of the test () method, so it can be replaced by scalars: the internal x and y are directly replaced and decomposed into local variables in the test () method, thus avoiding the creation of Point object instances.
Public int test (int x) {int xx = x + 2; int px = xx; int py = 42 return px;}
Step3: invalid code elimination
Data flow analysis shows that the value of py does not actually have any effect on the method, so you can safely eliminate invalid code to get the final optimization result, as shown below:
Public int test (int x) {return x + 2;}
Observing the test results, the programs after the implementation of escape analysis can often get good results in MicroBenchmarks, but in practical applications, especially in large programs, it is found that the effect of escape analysis may be unstable, or the analysis process is time-consuming but can not effectively identify non-escape objects, resulting in a decline in performance (the benefits of just-in-time compilation), so for a long time, even server compilers Escape analysis is also not enabled by default (starting from JDK 6 Update 23, escape analysis is enabled by default in the server compiler.) Even in some versions, such as JDK 6 Update 18, this optimization was completely disabled until JDK 7, when it was turned on by default by the server compiler
If necessary or confirmed to be beneficial to the program, you can use parameters:
-XX:+DoEscapeAnalysis manually opens the escape analysis
When enabled, you can use the parameters:
-XX:+PrintEscapeAnalysis to view analysis results
With escape analysis support, users can use the following parameters:
-XX:+EliminateAllocations enables scalar replacement
+ XX:+EliminateLocks enables synchronous elimination
-XX:+PrintEliminateAllocations to check the replacement of scalars
At this point, I believe you have a deeper understanding of "how Java objects allocate memory on the stack". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow 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.
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.