"Deep understanding Java Virtual Machine" 2nd Edition notes (complete) __java

Source: Internet
Author: User
Tags garbage collection instance method integer numbers
The 1th chapter goes near Java 1.2 Java Technology SystemJava programming language, Java Virtual machine, Java API Class library these three parts are collectively referred to as JDK (Java Development Kit), the JDK is the minimum environment to support Java program development. 1.4 Java Virtual machine history 1.4.1 Sun classic/exact VMThe Exact VM is named for its use of accurate memory management, which means that the virtual machine can know exactly what type of data is in place in memory. For example, in memory there is a 32-bit integer 123456, whether it is a reference type to point to 123456 of the memory address or a value of 123456 integer, the virtual machine will be able to distinguish, so that the GC can accurately determine whether the data on the heap can also be used. Due to the use of accurate memory management, the Exact VM can discard the previously classic VM based on handler object lookup so that each anchored object burns an indirect lookup overhead and promotes performance. 1.4.2 Sun HotSpot VMThe hotspot code detection capabilities of the HotSpot VM enable you to find the most compiled code by executing the counter, and then notify the JIT compiler to compile it in a method-by-unit way. If a method is called frequently, or the number of effective loops in the method is large, the standard compilation and OSR (stack substitution) compilation actions are triggered respectively. 1.4.6 Apache harmony/google Android Dalvik VMDalvik VM is not a Java virtual machine, it does not follow the Java Virtual Machine specification, can not directly execute Java class files, using a register schema rather than the common stack architecture in the JVM. But it is also inextricably linked to Java, its implementation of the Dex (Dalvik executable) file can be transformed through the class file, using Java syntax to write applications, you can directly use most of the Java APIs. 1.5 Looking to the future of Java technologyAn important advantage of functional programming is that such programs are naturally suitable for running in parallel. 2nd Chapter Java memory area and memory overflow exception 2.2 Run Time data region 2.2.4 Java HeapThe only purpose of this memory region (Java heap) is to hold the object instance, where almost all object instances allocate memory. 2.2.5 Method AreaThe method area, like the Java heap, is an area of memory shared by individual threads that stores data such as class information, constants, static variables, and Just-in-time compiler-compiled code that have been loaded by the virtual machine. 2.3 Hotspot Virtual Machine Object Quest creation of 2.3.1 ObjectsChoose which allocation method is determined by whether the Java heap is structured, and whether the Java heap is structured and determined by whether the garbage collector used has a compression collation function. Therefore, in the use of serial, parnew and so on with the compact process collector, the system uses the allocation algorithm is a pointer collision, and the use of CMS based on the mark-sweep algorithm collector, usually using the idle list. The other is to divide the memory allocation in different spaces by thread, that is, each thread allocates a small chunk of memory in the Java heap, called the local thread allocation buffer (thread allocation buffer, Tlab). Which thread allocates memory, is allocated on the tlab of the thread, and only needs to be locked synchronously when Tlab has run out and allocates a new tlab. memory layout for 2.3.2 ObjectsThe object header of the hotspot virtual machine includes two parts of information, the first part is used to store the Run-time data of the object itself, such as hash code, GC generational age, lock status flag, thread held lock, biased thread ID, biased timestamp, etc. The length of this data is 32bit and 64bit in the 32-bit and 64-bit virtual machines (with no compression pointer turned on), which is officially called "Mark Word". Another part of the object header is a type pointer, a pointer to its class metadata that the virtual machine uses to determine which class the object is an instance of. access positioning for 2.3.3 objectsThe biggest benefit of using a handle is that a stable handle address is stored in the reference, and the instance data pointer in the handle is changed only when the object is moved (a very common behavior when the object is moved by garbage collection), and the reference itself does not need to be modified. 2.4 Combat: OutOfMemoryError Anomaly 2.4.1 Java Heap OverflowEnsure that GC roots to objects with a accessible path to prevent the garbage collection mechanism from clearing these objects. The emphasis is on confirming that the objects in memory are necessary, that is, whether a memory leak (Memory leak) or a memory overflow (Memory Overflow) is first identified. 2.4.2 Virtual machine stack and local method stack OverflowIf you use the virtual machine default parameters, the stack depth is completely 1000~2000 in most cases, and the depth should be sufficient for normal method calls (including recursion). However, if you create a memory overflow caused by multithreading, if you cannot reduce the number of threads or replace 64-bit virtual machines, you can only get more threads by reducing the maximum heap and reducing the stack capacity. 2.4.3 Method area and run frequent pool overflowIn JDK 1.6, the Intern () method copies the first encountered string instance to the permanent generation, returning the reference to this string instance in the permanent generation, while the string instance created by StringBuilder on the Java heap, which is necessarily not the same reference, returns FALSE. The Intern () implementation of JDK 1.7 (and some other virtual machines, such as JRockit) does not replicate the instance, but records the first occurrence of the instance reference in the constant pool, so intern () returns the same reference as the string instance created by StringBuilder. 3rd Chapter garbage Collector and memory allocation strategy 3.1 Overviewprogram counters, virtual machine stacks, local method stacks 3 zones are born with threads and are extinguished by threads. How much memory is allocated in each stack frame is basically known when the class structure is determined, therefore, the memory allocation and recovery of these areas are deterministic, in these areas there is no need to consider the problem of recycling, because the end of the method or the end of the thread, memory will naturally follow the recovery. are 3.2 objects dead? an algorithm for the analysis of 3.2.2In the Java language, objects that can be roots as GC include the following:
The object referenced in the virtual machine stack (the local variable table in the stack frame).
The object referenced by a class static property in the method area.
The object referenced by the constant in the method area.
The object referenced by JNI (that is, the commonly said native method) in the local method stack. 3.2.4 to survive or DieThe so-called "execution" here means that a virtual opportunity triggers this method, but does not promise to wait for it to run, because if an object is slow to execute in the Finalize () method, or if a dead loop occurs (more extreme), Will most likely cause other objects in the F-queue queue to wait permanently and even cause the entire memory recovery system to crash. The Finalize () method is the last chance for an object to escape death, and later the GC will make a second, small-scale mark on objects in F-queue, if the object is to successfully save itself in Finalize ()-just reconnect with any object on the reference chain. For example, you assign yourself (this keyword) to a class variable or to a member variable of an object, and it is removed from the collection of "will be recycled" at the second token, and if the object does not escape at this time, it is essentially recycled. implementation of 3.4 hotspot algorithm 3.4.2 Safety PointAs a result, the selection of the security point is essentially based on whether the program "has features that allow the program to perform for a long time" as a standard-because each instruction executes very briefly, programs are less likely to run for long periods of time because the length of the instruction stream is too long, and the most obvious feature of "long Execution" is the command sequence reuse, For example, method calls, circular jumps, exception jumps, and so on, so the instructions with these functions will produce safepoint. There are now few virtual machine implementations that use preemptive interrupts to suspend threads and respond to GC events. 3.4.3 Security ZoneThe so-called program does not execute is not allocated CPU time, the typical example is the thread is in the sleep state or blocked state, when the thread can not respond to the JVM interrupt request, "go" to a safe place to interrupt the suspend, the JVM is also obviously not likely to wait for the thread to be allocated CPU time. A security zone is a piece of code that does not change the reference relationship. GC is safe to start anywhere in this area. We can also think of safe region as an extended safepoint. 3.5 Trash CollectorThere is no provision in the Java Virtual Machine specification for how the garbage collector should be implemented, therefore, the garbage collectors provided by different vendors and different versions of virtual machines can be very different, and generally provide parameters for users to assemble the collectors used in various eras according to their own application characteristics and requirements. 3.5.1 Serial CollectorThe serial collector is still the default Cenozoic collector that the virtual machine runs in client mode. 3.5.3 Parallel Scavenge collectorThe feature of the Parallel scavenge collector is that its focus is different from that of other collectors, and the focus of collectors such as CMS is to minimize the downtime of user threads when garbage collection is possible, and Parallel The goal of the scavenge collector is to achieve a controllable throughput (throughput). The so-called throughput is the CPU used to run the user code time and CPU total consumption time ratio, that is, throughput = Run user code time/(run user code time + garbage collection time), the virtual machine for a total of 100 minutes, the garbage collection spent 1 minutes, that throughput is 99%. 3.5.6 cms collectorThe two steps of initial marking and re tagging still require "Stop the world". The initial tag simply marks the object that GC Roots can directly relate to, and the concurrent marking phase is the process of GC Roots tracing, The re-marking phase is intended to modify the tag records of the part of the object that caused the markup to change as the user program continues to function during the concurrent markup, and the pause time of this phase is generally slightly longer than the initial tag stage, but is far shorter than the time of the concurrent tag. It has been proved that the CMS collector in increments is very general, in the current version, I-CMS has been declared as "deprecated", that is, no longer advocate user use. If the CMS during the operation of reserved memory can not meet the needs of the program, there will be a "Concurrent Mode failure" failure, when the virtual machine will start Backup plan: Temporarily enable the serial old collector to repeat the garbage collection of the older era, so the pause time is very long. 3.5.7 G1 CollectorG1 tracks the value of the garbage accumulation in each region (the amount of space that is retrieved and the experience of the time it takes to reclaim it), maintaining a priority list in the background, each time according to the allowable collection times, The region (that is, the origin of the Garbage-first name) is the first to reclaim the highest value. 3.6 Memory allocation and recycling policies 3.6.2 Big objects go straight into the old ageThe most typical large object is the very long string and the array (the byte[in the example I listed) is a typical large object. 3.6.5 space Allocation guaranteeThe rule after JDK 6 Update 24 changes to a full GC if the old age's contiguous space is greater than the total size of the Cenozoic object or the average size of the previous promotions. Minor GC. 4th Virtual machine performance monitoring and fault handling tools command line tools for 4.2 jdk 4.2.4 Jmap:java Memory Image ToolOr, under the Linux system, send a process exit signal by Kill-3 command to "scare" the virtual machine and get the dump file. visualization tools for 4.3 jdk 4.3.1 Jconsole:java Monitoring and Management consoleThe Readbytes method checks that the execution token is returned immediately when the stream is not updated, which consumes only a small CPU resource. An action that does not return a thread to execute a token will run out of execution time on the empty loop until the thread switches, which consumes more CPU resources. The reason for the deadlock is that the integer.valueof () method, based on reducing the number of object creation and memory savings, is cached between [-128, 127], and when the valueof () method passes in the argument, the object in the cache is returned directly. That is, the 200 integer.valueof () method called in the code returns only two different objects. the 5th chapter of the case analysis and actual combat 5.2 Case Study 5.2.1 Program deployment Strategy on high-performance hardwareWe just need to ensure that the cluster has affinity, that is, the equalizer according to a certain rule algorithm (generally according to the SessionID allocation) will be a fixed user request to a fixed cluster node for processing can be, so that the program development phase of the basic need not for the cluster environment to do any special consideration. 5.2.3 Overflow error due to external heap memoryYou know that the operating system has limits on the memory that can be managed by each process, and the 32-bit Windows platform used by this server is 2GB, which is zoned 1.6GB to the Java heap, and direct Memory memory is not counted in the 1.6GB heap, so it can only be part of the remaining 0.4GB space. The key to the overflow in this application is that, while garbage collection is in progress, the virtual machine will be able to recycle direct memory, but direct memory cannot, as the new generation and old age, discover that there is not enough space to notify collectors of garbage collection, it can only wait until the old age full GC, and then "by the way" helps it clean out the discarded objects of memory. 5.2.4 External command causes slow systemThe execution of this shell script is invoked through the Java Runtime.getruntime (). Exec () method. This invocation can achieve the purpose, but it is a very resource-intensive operation in the Java Virtual machine, and even if the external command itself is finished quickly, the overhead of creating the process on a frequent basis is considerable. When the Java virtual machine executes this command: First clone a city that has the same environment variable as the current virtual machine, then use the new city to execute the external command, and then exit the city. If you perform this operation frequently, the system will consume a lot, not only the CPU, but also the heavy memory load. 5.2.6 improper data structure leads to excessive memory footprintIn the hashmap<long,long> HASHMAP structure, only the two long integer data stored by key and value are valid data, a total of 16B (2x8b 2\times8b). After the two long integers are packaged as Java.lang.Long objects, they have a 8B mark Word, 8B Klass Pointer, and a long value that stores the data in 8B. After these two long objects compose Map.entry, the another 16B object header, then a 8 B next field and a 4B int hash field, in order to align, you must also add a 4 B blank fill, and finally the 8B reference to this entry in HashMap, adding two long integer numbers, The actual memory consumed is (long (24B) x2) +entry (32B) +hashmapref (8B) =88b (Long (24B) \times 2) + Entry (32B) + HashMap Ref (8B) = 88B, space efficiency is 16B/8 8b=18% 16b/88b=18\%, it's too low. after reading the source code of HashMap, they all understand 5.3 Combat: Eclipse Run Speed tuning optimization of 5.3.3 compile time and class loading timeCompile time refers to the time-consuming of a virtual machine's JIT compiler compiling hotspot code. We know that in order to realize the cross-platform features of the Java language, the Java code is compiled into a class file stored in the byte code (bytecode), the virtual machine through the interpretation of the bytecode command, compared to C/s compiled cost of the binary code, the speed is much slower. 5.3.4 Adjust memory settings to control garbage collection frequencyBecause the tests we did were to test the start time of the program, the influence of class loading and compilation time on the test was greatly magnified. Strictly speaking, it does not include user threads that are executing native code, because native code generally does not change the reference relationship of Java objects, so it is not necessary to suspend them for garbage collection. When Eclipse starts, most of the full GC is due to old age capacity expansion, and is partly caused by a permanent generation space extension. 6th Chapter class file Structure 6.2 The cornerstone of independent sexJava virtual machines are not bound to any language, including Java, and are only associated with the particular binary format of the class file, which contains Java Virtual machine instruction sets and symbol tables, as well as a number of other ancillary information. structure of the 6.3 class fileWhen a data item that requires more than 8 bytes of space is encountered, it is divided into several 8-bit bytes for storage, as in the previous order of the high position. Whether it is unsigned or a table, when it is necessary to describe multiple data of the same type but with an indefinite number of times, a forward capacity counter is often used in the form of several consecutive items of data, in which case a series of contiguous types of data is called a collection of one type. 6.3.1 The version of the magic number and class fileMany file storage standards use the magic number for identification, such as image format, such as GIF or JPEG, in the file header are the number of magic. The magic number of class file is very "romantic", the value is: 0xCAFEBABE (Coffee baby. , the magic value was identified when Java was called "Oak" language (around 1991). 6.3.2 Constant PoolWhen the class file format specification is set, it is a special consideration for the designer to empty the No. 0 constant, the purpose of which is to satisfy some of the subsequent index values that point to the constant pool, in a particular case, to express the meaning of "do not reference any of the constant pool items," which allows the index value to be 0来 represented. 6.3.6 Method table collectionThe Java code in the method, compiled into a bytecode instruction by the compiler, is stored in a property named "Code" in the collection of method attributes, which is the most extensible data item in the class file format. A signature is a collection of field symbol references in a constant pool for each parameter in a method, because the return value is not included in the signature, so there is no way in the Java language to overload an existing method by relying solely on the difference of the return value. 6.3.7 Property Sheet Collection Method parameters (including the hidden parameter "This" in the instance method), the parameters of the explicit exception handler (Exception Handler Parameter, which is the exception defined by the catch block in the Try-catch statement), Local variables defined in the method body need to be stored using a local variable table. The Javac compiler assigns slot to individual variables based on the scope of the variable. Turn access to this keyword to access to a generic method parameter by javac the compiler, and then automatically pass in the instance method when the virtual machine invokes it. Therefore, at least one local variable in the local variable table of the instance method, which points to the current object instance, also reserves the first slot bit to hold the reference of the object instance, and the method parameter value is calculated from 1. At this point, the value of x copies a copy to the slot of the last local variable table. Don't confuse the exception table that you just explained earlier. The function of the exceptions property is to enumerate the detected exceptions (Checked exceptions) that may be thrown in the method, which is the exception that is enumerated after the throws keyword when the method is described. The start_pc and length properties represent the byte-code offsets at the beginning of the life cycle of the local variable and the length of its scope coverage, and the combination of the two is the scope of the local variable within the bytecode. The current choice for the Sun Javac compiler is to use final and static to modify a variable (which, by custom, is called "constant" is more appropriate), And the data type of the variable is the basic type or java.lang.String, the Constantvalue attribute is generated to initialize, if the variable is not final decorated, or is not a basic type and string, it will be selected in the <clinit Initialization in the > method (Class Builder, Class init). All classes, methods, and fields generated by non-user code should be set at least one entry in the synthetic property and the acc_synthetic flag bit, with the exception of the instance constructor <init> method and the Class builder <clinit> method. This paper omits the steps to confirm the legality of the behavior logic of bytecode by data flow analysis at runtime, but in the compile phase, a series of validation types (verification Types) are recorded directly in the class file, which replaces the type derivation process by examining these validation types. The runtime cannot treat a generic type as if it were a user-defined generic type, such as C #, which has a true generic support, for example, when the runtime does reflection, the generic information cannot be obtained. Signature attribute is to make up for this flawThe additional, now Java API can get generic type, the final data source is this attribute. 6.4 Byte Code instruction introductionBecause the Java virtual machine uses an operand stack rather than a register architecture, most directives do not contain operands, only one opcode. 6.4.10 Sync CommandMethod-level synchronization is implicit, that is, it is not required to be controlled by a byte code instruction, it is implemented in method invocation and return operations. When a method call, the calling instruction checks whether the method's acc_synchronized access flag is set, and if it is set, the execution thread requires that the pipe be successfully held before the method can be executed. Finally, release enhancement (Monitor) When the method completes (whether it is finished normally or not). Synchronizing a sequence of instruction sets is usually represented by a synchronized statement block in the Java language, where the instruction of the Java Virtual machine has monitorenter and monitorexit two instructions to support the semantics of the Synchronized keyword. The correct implementation of the SYNCHRONIZED keyword requires the Javac compiler to collaborate with the Java Virtual machine. 6.5 Public design and private implementationTranslates the imported Java Virtual Machine code into the Cheng host CPU's native instruction set (that is, JIT code generation technology) at load or execution time. 7th Chapter Virtual machine class loading mechanism 7.2 Types of loading timeIn fact, there is no reference entry for the Constclass class in the Notinitialization class file, which has no connection after the two classes have been translated into class. 7.3 class loading process 7.3.1 LoadingConverts the static storage structure represented by this byte stream into the RUN-TIME data structure of the method area. 7.3.2 VerificationThis phase of validation is based on binary byte throttling, only after this phase of validation, the byte stream will enter the memory of the method area for storage, so the next 3 verification phase is based on the method area of the storage structure, no longer directly manipulate the byte stream. 7.3.3 ReadyThere are two confusing concepts in this phase that need to be emphasized, first of all, that this time the memory allocation includes only the class variable (the variable that is modified by the static), not the instance variable, which will be assigned to the Java heap along with the object when the object is instantiated. Class 7.4 LoaderThe virtual machine design team takes the "fully qualified name of a class to describe the binary byte stream" in the class loading phase to the outside of the Java virtual machine, so that the application can decide how to get the required class. 7.4.1 classes and Class loadersFor any class, it is necessary to establish its uniqueness in the Java Virtual machine, together with the class loader that loads it, and each class loader has a separate class namespace. In two rows of output, from the first sentence, you can see that This object is really a class org.fenixsoft.classloading.ClassLoaderTest instantiated object, but from the second sentence can be found that this object and class Org.fenixsoft.classloading.ClassLoaderTest do belong to the class Type check, it returns false. This is because there are two classloadertest classes in the virtual machine, one is loaded by the System Application class loader, and the other is loaded by our custom ClassLoader, though all from the same class file, but still two separate classes , the result is naturally false when the object belongs to the type check. 7.4.2 Parent Delegation ModelExtended class loader (Extension ClassLoader): This loader is implemented by Sun.misc.launcher$extclassloader, which is responsible for loading <java_home>\lib\ext \lib\ In the Ext directory, or all class libraries in the path specified by the JAVA.EXT.DIRS system variable, the developer can use the Extended class loader directly. The parent delegation model works by: If a class loader receives a request for class loading, instead of trying to load the class on its own, it does so by delegating the request to the parent class loader, which is the case for every class loader, so all load requests should eventually be routed to the top-level boot ClassLoader. The child loader will attempt to load itself only if the parent loader has feedback that it cannot complete the request (it does not find the required class in its search scope). The Java class, along with its classloader, has a hierarchical relationship with precedence. For example, Class Java.lang.Object, which is stored in Rt.jar, regardless of which class loader is loading the class, is ultimately delegated to the boot class loader at the top of the model to load, so the Object class is the same class in the various ClassLoader environments of the program. Conversely, if the parent delegation model is not used, the individual classloader is loaded by itself. If the user wrote a class called Java.lang.Object and placed it in the classpath of the program, there would be multiple different object classes in the system, and the most basic behavior in the Java type system would not be guaranteed and the application would become confusing. If the reader is interested, you can try to write a Java class that has the same name as the one in the Rt.jar class library, and you will find that it can be compiled normally, but never be loaded. The parental delegation model is important to ensure the stable operation of the Java program, but it is very simple to implement, and the code that implements the parental delegation is concentrated in the Java.lang.ClassLoader LoadClass () method, which is clearly understandable: check if it has been loaded before, If not loaded, the parent loader's LoadClass () method is invoked, and if the parent loader is empty, the startup class loader is used as the parent loader by default. If the parent class fails to load, the ClassNotFoundException exception is thrown, then the Findclass () method is invoked to load it.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.