In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the knowledge points of the architecture of Java HotSpot performance engine". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Introduction
Java > TM platform is becoming the mainstream carrier of software development and deployment. The use of the Java platform is growing rapidly in many areas-from credit cards to mainframes, from web applets to large business applications. Therefore, the quality, maturity and performance of Java technology have become critical factors for every developer and user. Sun Microsystems,Inc. Emphasis is being placed on technology that can "lift the railing in the way" in front of many processors and operating systems, with which software developers can run Java-based applications efficiently and reliably regardless of processors and operating systems.
One of the main reasons why people are interested in the Java platform is that unlike programs written in traditional languages, programs based on Java technology are distributed in a portable and secure form. In the past, the use of portable distribution generally meant a decline in performance during program execution. Through the use of modern dynamic compilation technology, this performance decline has been slowed down, and its essence can be said to be "double profit".
To take a simple but important example: we can have a Java technology compiler generate optimized machine code "in the run" for a particular version of the processor (for example, although Pentium and Pentium II processors can run the same machine code, no form of machine code can be optimized for both). Therefore, the bytecode distribution form of the Java programming language can not only provide portability, but also actually provide new opportunities for performance improvement.
This article will introduce the second generation performance technology of Java-Java HotSpot performance engine. The Java HotSpot performance engine innovates in almost every area of its design, using a wide range of techniques that can be used to improve performance; this includes adaptation optimization techniques that detect and accelerate "running" performance-critical (performance-critical) code. Java HotSpot also provides ultra-fast (ultra-fast) thread synchronization to get the maximum performance of thread-safe Java-based programs; it also provides a garbage collector (GC), where GC is not only particularly fast, but also completely "accurate" (and therefore more reliable); in addition, algorithms using the latest technology also reduce or eliminate the sense of pause caused by garbage collection. Finally, because the Java HotSpot performance engine is written in a concise, advanced object-oriented design style at the source code level, maintainability and scalability are further improved.
two。 Overview
Here are the main structural advantages of the Java HotSpot performance engine:
1) better general performance
No handle object (references to objects are implemented as direct pointers for speed)
Faster thread synchronization for the Java programming language
To achieve faster call-out and call-in of C code, C and Java code can share the same activation stack
Compared with compiling JIT in time, the total cost of code space and startup time is greatly reduced.
2) the (best-of-breed) performance most conducive to reproduction
To achieve true native code performance, the native code compiler is optimized
Adaptive "Hot Spot" detection focuses on performance-critical code optimization, thus greatly reducing the total compilation time and memory requirements for compiled code
Method embedding technology eliminates most dynamic method calls for most programs.
Faster method calls to non-embedded methods.
3) accurate, successive (generational) replication garbage collector
Faster object allocation
Accuracy provides more accurate object recovery (unlike conservative (conservative) or semi-accurate (partially-accurate) recyclers that can cause unpredictable memory leaks)
Successive recycling greatly improves the recycling efficiency for most programs.
For most programs, successive recycling also greatly reduces the frequency of pauses caused by recycling "old objects"
Successive recycling also greatly improves performance scalability for applications that use the memory of a large number of "live" objects
Use the tag-collation (mark-compact) algorithm to recycle "old" objects, eliminating memory fragments and increasing locality
The incremental "no pause" garbage collector for "longevity" objects, or even a very large number of "live" objects, essentially eliminates the user visual pause in the process of object collection, which is ideal for wait-sensitive applications (such as servers) and programs with large amounts of data.
4) Advanced advanced design
Transparent debugging and profiling semantics-the Java HotSpot architecture makes native code generation and optimization completely transparent to programmers, providing full profile and debugging information according to the pure bytecode semantics, regardless of the optimization methods actually used internally.
3. Architecture the architecture of the Java HotSpot performance engine culminates the research done in the Sun Microsystems lab over the years. It combines a state-of-the-art memory model, garbage collector, and adaptation optimizer; and it is written in a particularly advanced and object-oriented style.
The following sections describe the important architecture and features of the Java HotSpot performance engine.
4. Memory model
4.1 No handle object
The Java 2 Software Development Kit (SDK) uses indirect handles to represent references to objects. While this makes object relocation easier during garbage collection, it can cause an important performance bottleneck because most access to instance variables of Java programming language objects requires two levels of indirect references. The Java HotSpot performance engine removes the concept of handles: references to objects are implemented as direct pointers, providing C-speed access to instance variables. The garbage collector is responsible for finding and updating all references to objects in place when the object is relocated during memory collection.
4.2 two-word (Tow-word) object header
The Java HotSpot performance engine uses dual machine-word object headers instead of three-word object headers like Java 2 SDK. Because the average object size of the Java programming language is small, this technique plays an important role in saving space (about 8% of the heap size). The word in the first object header contains information such as the identity hash code and GC status; the word in the second object header is a reference to the object's class. Only an array has a third object header field, which is used to represent the size of the array.
4.3 represent mapping data as objects
Classes, methods, and other internal mapping data are represented directly as objects on the heap (although these objects may not be directly accessible by programs based on Java technology). This not only simplifies the memory model, but also allows you to use the same garbage collector as other Java programming language objects to collect this kind of mapping data.
4.4 Native thread support, including task preemption and multiprocessing techniques
The activation stack of each thread method is represented using the thread model of the host operating system. Java programming language methods and native methods share the same stack, allowing quick calls between C and Java programming languages. Fully preemptive Java programming language threads can be supported by using the thread scheduling mechanism of the host operating system.
One of the main advantages of using the thread and scheduling mechanism of the local operating system is that it can transparently use the local operating system to support multiple processing. Because the Java HotSpot performance engine is designed to be insensitive to contention caused by preemptive and / or multiprocessing when executing Java programming language code, Java programming language threads will automatically take advantage of arbitrary scheduling mechanisms and processor allocation strategies provided by the local operating system.
5. Memory garbage collection
5.1 background note
One of the main attractions of the Java programming language for programmers is that it is the first mainstream programming language that provides built-in automatic memory management (or memory garbage collection). In traditional languages, the explicit allocation / release model is generally used for dynamic memory allocation. It turns out that this is not only one of the main causes of memory leaks, program errors, and crashes of programs written in traditional languages, but also a bottleneck to improve performance. it is also a major obstacle to modular and reusable code (it is sometimes almost impossible to determine release points between module boundaries without explicit and incomprehensible collaboration between modules). In the Java programming language, garbage collection is also an important part of the so-called "safe" execution semantics necessary to support the security model.
When a garbage collector can "prove" that an object is inaccessible to a running program, it can automatically "free" memory for that object in the background simply by reclaiming the object. This automatic process not only completely eliminates memory leaks caused by too little release, but also eliminates program crashes and hard-to-find reference errors caused by releasing too much.
Traditionally, compared with the explicit release model, garbage collection has always been regarded as an inefficient and performance degradation process. In fact, using modern garbage collection technology can greatly improve performance, and this performance is actually much better than that provided by explicit release.
5.2 Java HotSpot garbage Collector
The Java HotSpot performance engine has an advanced garbage collector that, in addition to the advanced technical features described below, takes full advantage of concise and object-oriented design to provide a high-level garbage collection architecture framework that can be easily configured, used, or extended to use new collection algorithms.
The main features of the Java HotSpot garbage collector are described below. In general, the combined results of the various technologies used are good for applications that require the highest possible performance, or for long-running applications that do not expect memory leaks and inaccessible memory due to fragmentation. The Java HotSpot performance engine can not only provide garbage collector performance with the latest technology, but also guarantee full memory recovery and completely eliminate memory fragmentation.
5.3 accuracy
The Java HotSpot garbage collector is a full-precision collector, in contrast to many garbage collectors that are conservative or partially-accurate. Although conservative garbage collection is attractive because it is easy to be added to a system that does not support garbage collection, it has some defects.
A conservative garbage collector cannot determine exactly where references to all objects are distributed. As a result, it must conservatively assume that memory words (memory word) that seem to reference an object are actual object references. This means that it can cause some kind of error, such as mistaking an integer for an object pointer; this can have some negative effects. First, when such an error occurs (which is not common in practice), memory leaks can unpredictably occur in a way that is essentially reproduce or debug to application programmers (although crashes caused by dangling object references can still be prevented and the program can execute correctly if there is enough backup memory) Second, because it may have caused an error, a conservative recycler must use handles to indirectly reference the object (which degrades performance), or to avoid repositioning the object; because repositioning an unhandled object requires updating all references to the object, which is impossible when the recycler cannot accurately determine that a superficial reference is a real reference. Failure to relocate an object will result in memory fragmentation of the object and, more importantly, it will hinder the use of the advanced successive replication recycling algorithm described below.
Because the Java HotSpot collector is fully accurate, it provides several strong design guarantees that are impossible on conservative collectors: all inaccessible object memory can be reliably reclaimed
All objects can be relocated, so the object memory can be demarcated; this eliminates the fragmentation of object memory and increases the locality of memory.
5.4 successive replication recycling
The Java HotSpot performance engine uses a sequential replication recycler with advanced technology, which has two main advantages:
Compared with Java 2 SDK, it greatly improves the allocation speed and overall garbage collection efficiency for most programs (usually 5 times higher)
Accordingly, the frequency of "pauses" during garbage collection that can be felt by the user is reduced.
Successive recyclers take advantage of the fact that in most programs, most objects (usually 95%) are very short-lived, that is, they are used as temporary data structures. By isolating newly created objects into an object "nursery", a successive recycler can accomplish the following things: first, because in object kindergartens, new objects are allocated one by one like a stack. As a result, allocation becomes particularly fast because it only involves the update of a single pointer and a single check for kindergarten overflow. Second, by the time kindergartens overflow, the objects in most kindergartens are already "dead". This makes it possible for garbage collectors to simply move a very small number of surviving objects in kindergartens elsewhere, thus eliminating the need to recycle dead objects in kindergartens.
5.5 "Old object" recycler using tag-collation algorithm
Although successive replication collectors can effectively recycle most of the dead objects, longer-lived objects continue to accumulate in the "old object" memory area. Garbage collection of old objects is sometimes necessary from the point of view of insufficient memory or program requirements. The Java HotSpot performance engine can use a standard mark-and-organize recycling algorithm that traverses all diagrams of living objects starting from the "root", then scans memory and collates gaps left by dead objects. Memory fragmentation can be eliminated by tidying up gaps in the recycling heap rather than recycling them into a release list; since the release list search is eliminated, the allocation of old objects will be more reasonable.
5.6 incremental "no pause" garbage collector
The mark-clean collector does not eliminate all user-perceptible pauses, which occur when "old" objects (in machine terminology, objects that have been "alive" for some time) need to be garbage collected. and this pause is proportional to the amount of data of existing living objects. This means that the pause can be arbitrarily large when more data is being processed; this is a very bad performance for server applications, animations, or other soft real-time applications.
The Java HotSpot performance engine provides another old space garbage collector used to solve this problem. The collector is fully incremental and eliminates user-detectable garbage collection pauses. The incremental collector increases smoothly and proportionally, providing relatively constant pause time even when dealing with large object datasets. This creates excellent performance for the following applications:
Server applications, especially high availability applications
Applications that handle datasets of very large "live" objects
Paused applications, such as games, animations, or other highly interactive applications, are not expected to be noticed by the user.
The non-pause recycler uses an incremental old space recovery scheme, which is academically called the "train" algorithm. The algorithm divides the pause during the recycling of old space into many small pauses (typically less than 10 milliseconds), and then spreads these tiny pauses over time, so that the actual program is for the user, it's like there's no pause. Because the train algorithm is not a hard real-time (hard-real time) algorithm, it can not guarantee the upper limit of the number of pauses. In fact, however, a very large number of pauses are extremely rare, and they are not directly caused by large data sets.
As a very welcome useful by-product, non-pause recycling can also improve memory locality. Because the algorithm attempts to relocate tightly coupled (coupled) groups of objects to adjacent memory regions, it can provide the best memory paging and cache locality properties for these objects. This is also very useful for multithreaded applications that operate on different object datasets. 6. Ultra-fast thread synchronization
Another important attraction of the Java programming language is that it provides a language-level thread synchronization. This makes it easy to write multithreaded programs with fine thread synchronization locks. Unfortunately, current synchronization implementations are very inefficient compared to microoperations in other Java programming languages, making fine synchronization operations a major performance bottleneck.
The Java HotSpot performance engine has made a breakthrough in the implementation of thread synchronization, which greatly promotes the improvement of synchronization performance. The result is that synchronization performance becomes so fast that it is no longer an important performance issue for most real-world programs.
In addition to the spatial benefits mentioned in the "memory model" section, the synchronization mechanism also provides its performance benefits by providing ultra-fast and constant-time (constant-time) performance for all uncontended synchronizations, which are dynamically made up of most synchronizations.
The Java HotSpot synchronization implementation is perfectly suited to multiprocessing and should demonstrate excellent multiprocessor performance characteristics.
7. Java HotSpot compiler
7.1 background description
Java programming language is a new programming language with unique performance characteristics. So far, most attempts to improve its performance have focused on how to apply compilation techniques developed for traditional languages. The just-in-time compiler is a basic fast traditional compiler that converts Java bytecode into native machine code "in the run". Just in time the compiler runs on the end user's machine that actually executes the bytecode and compiles each method that is executed for the first time.
There are several problems in JIT compilation. First of all, because the compiler runs on the machine that executes the bytecode within "user time", it will be severely limited by the compilation speed: if the compilation speed is not particularly fast, the user will feel a significant delay in starting or some part of the program. This has to take a compromise, with which it will be difficult to optimize the best, which will greatly reduce compilation performance.
Second, even if JIT has time for full optimizations, such optimizations are less effective for Java programming languages than for traditional languages such as C and C++. There are several reasons for this:
The Java programming language is dynamically "secure", which means that programs do not violate the semantics of the language or directly access unstructured memory. This means that dynamic type testing must be done frequently, for example, when transforming (casting) and when storing to an array of objects.
The Java programming language allocates all objects on the "heap", while in C++, many objects are allocated on the stack. This means that the object allocation efficiency of Java programming language is much higher than that of C++. In addition, because the Java programming language is garbage collected, it has more different types of memory allocation overhead (including potential garbage cleaning (scavenging) and write-isolate (write-barrier) overhead) than C++.
In the Java programming language, most method calls are "virtual" (potentially polymorphic), which is rare in C++. This not only means that the performance of method calls is more important, but also means that it is more difficult to perform static compiler optimizations for method calls (especially global optimizations such as embedded methods (inlining)). Most traditional optimizations are most effective between calls, and the reduced distance between calls in the Java programming language can greatly reduce the efficiency of such optimizations because they use smaller code snippets.
Programs based on Java technology can be changed "in operation" because of their powerful ability of dynamic class loading. This makes it particularly difficult to perform many types of global optimizations, because the compiler must not only be able to detect when these optimizations are invalid due to dynamic loading, but must also be able to undo and / or redo them during program execution without in any way damaging or affecting the execution semantics of Java-based programs (even if they involve active methods on the stack).
The result of the above problems is that any attempt to obtain the advanced performance of the Java programming language must seek a non-traditional solution instead of blindly applying the traditional compiler technology.
The architecture of Java HotSpot performance engine solves the performance problem of Java programming language mentioned above by using adaptive optimization technology. Adaptive optimization technology is the research achievement of object-oriented language implementation by Self Group, a research institution of Sun Company for many years.
7.2Hot spot Hot Spot detection
The adaptive optimization technique takes advantage of the interesting properties of most programs and solves the problem of JIT compilation. In fact, all programs spend most of their time executing a small portion of their code. Instead of compiling the whole program as soon as the program starts, the Java HotSpot performance engine uses the interpreter (interpreter) to run the program as soon as it starts, analyzes the program to detect the key "HotSpot" in the program, and then centralizes the global local code (native-code) optimizer on these hotspots. By avoiding compiling code that is infrequently executed (of most programs), the Java HotSpot compiler focuses more attention on the performance critical parts of the program, thus eliminating the need to increase the overall compilation time. This dynamic monitoring is carried out continuously as the program runs, so it can accurately adjust its performance "in operation" to meet the needs of users.
An ingenious and important benefit of this approach is that by delaying compilation until after the code has been executed for a while ("moment" refers to machine time, not user time! So that you can collect information while the code is being used and use it for more intelligent optimization In addition to collecting hot spot information in the program, other types of information are also collected, such as caller-callee relationship data related to "virtual" method calls.
7.3 method embedding
As mentioned in the background description, the frequency of "virtual" method calls in the Java programming language is an important bottleneck that hinders optimization. When the Java HotSpot adaptation optimizer collects information about program "hotspots" during execution, it can not only compile these "hotspots" into native code, but also execute a large number of methods embedded in these codes.
Embedding has important benefits. It greatly reduces the dynamic frequency of method calls, which saves the time required to execute these method calls. More importantly, the inline generates a much larger block of code for the optimizer. This state can greatly improve the efficiency of traditional compiler optimization techniques, thus eliminating the obstacles to improving the performance of Java programming language.
Embedding promotes the optimization of other code, and it greatly improves the efficiency of optimization. With the further maturity of the Java HotSpot compiler, the ability to operate larger embedded code blocks will make it possible to achieve more advanced optimization techniques.
7.4 dynamic inverse optimization
Although the above embedding is a very important optimization method, it has traditionally been very difficult to achieve for dynamic object-oriented programming languages such as the Java programming language. In addition, although it is difficult to detect "hotspots" and embed the methods they call, it is still not enough to provide the full semantics of the Java programming language. This is because programs written in the Java programming language can not only change the mode of method calls "in the run", but also dynamically load new Java code for a running program.
Embedding is based on global analysis, and dynamic loading makes the embedding more complex because it changes the global relationship within a program. A new class may contain new methods that need to be embedded in place. Therefore, the Java HotSpot performance engine must be able to dynamically reverse optimize (and then re-optimize if necessary) previously optimized "hotspots", even during the execution of "hotspot" code. Without this capability, general embedding will not be executed securely on Java-based programs.
7.5 optimize the compiler
Only performance-critical code is compiled, which "buys time" and can be used for better optimization. The Java HotSpot performance engine uses a fully optimized compiler instead of the relatively simple JIT compiler. The full optimization compiler can perform all first-class optimizations. For example: dead code deletion, promotion of loop invariants, deletion of ordinary subexpressions and continuous transmission (constant propagation) and so on. It also gives optimization performance specific to certain Java technologies. Such as: null-check (null-check) and range-check (range-check) deletion. The register allocator (register allocator) is a global graph that represents the allocator by color, which makes full use of the large register set (register sets). The fully optimized compiler of Java HotSpot performance engine has strong portability, and it relies on relatively small machine description files to describe all aspects of the target hardware. Although the compiler adopts the slower JIT standard, it is still much faster than the traditional optimized compiler. Moreover, the improved code quality is a "reward" for the time saved by reducing the number of times compiled code is executed.
7.6 Summary
To sum up, we can summarize the role of the Java HotSpot adaptation optimizer as follows:
Generally speaking, programs start faster. This is because there is less precompilation than the JIT compiler.
The compilation process unfolds over time, making the compilation pause shorter and less noticed by the user.
The practice of compiling only performance-critical code "buys time" so that it can be used to perform better optimizations.
Because less code is compiled, less memory is required to compile the code.
By making the waiting time before compiling code a little longer, you can gather information to perform better optimizations, such as embedding, this technique will have far-reaching implications.
Make important code run faster by highly optimizing performance critical code.
7.7 impact on software reusability (reusability
One of the main advantages of object-oriented programming languages is to increase the productivity of development by providing a powerful language mechanism for software reuse. In practice, however, such reusability is rarely achieved. Because extensive use of these mechanisms can greatly degrade performance, programmers must use them carefully. An amazing side effect of Java HotSpot technology is that it greatly reduces the impairment cost of this performance. We believe that this will have an important impact on the way object-oriented software is developed, and for the first time it will allow companies to make full use of object-oriented reusability mechanisms without compromising their software performance.
Examples of this effect are easily available. A survey of programmers using the Java programming language will make it clear that many programmers avoid using full "virtual" methods as well as writing larger methods. Because they are convinced that every call to a virtual method leads to performance degradation. At the same time, the fine use of "virtual" methods (that is, non-"static" or "final" methods in the Java programming language) is particularly important for the construction of highly reusable classes, because each of these methods acts like an "hook" that allows new subclasses to modify the operations of the superclass.
Because the Java HotSpot performance engine automatically embeds most virtual method calls, the degree of performance degradation is greatly reduced and, in many cases, eliminated altogether.
The importance of this role cannot be overemphasized. Because the use of important reusability mechanisms can greatly change the performance trade-off, this technique has the potential to fundamentally change the way object-oriented code is written. In addition, with the maturity of object-oriented programming methods, there is an obvious trend towards more subdivided objects and more subdivided methods. Both trends aim to increase the frequency of virtual method calls in future code styles. With the popularity of this advanced code style, the advantages of Java HotSpot technology will become more and more obvious.
8. Java local interface (JNI) support
The Java HotSpot performance engine supports local methods using the standard Java native interface (JNI). Native methods previously written in JNI are up-compatible in both source and binary formats. The initial native method interface will not be supported (JNI is partially introduced because the old interface does not provide binary compatibility with the native method DLLs).
This is the end of the content of "what are the knowledge points of the architecture of the Java HotSpot performance engine". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.