"Deep understanding of Java virtual machines"-Notes old version __java

Source: Internet
Author: User
Tags field table garbage collection
2nd Chapter Java memory area and memory overflow exception
2.1 OverviewWhy you should understand memory management. Because Java programmers give memory control rights to the Java virtual machine, if there is a memory leak and overflow problem, if you do not understand how the virtual machine is using memory, then troubleshooting will become an exceptionally difficult task. The difference between a memory overflow and a memory leak. http://blog.csdn.net/buutterfly/article/details/6617375 memory overflow out of memory, refers to the program in the application of memory, there is not enough memory space for its use, appear out of the memory , such as applying for an integer, but saving it a long to save the number, that is, memory overflow. Memory leak memory leak, refers to the program in the application of memory, can not release the requested memory space, a memory leakage hazard can be ignored, but the memory leak accumulation consequences are very serious, no matter how much memory, sooner or later will be taken up by the light. Memory leak will eventually lead out of the memory.
2.2 Run Time data region http://blog.csdn.net/u012152619/article/details/46968883 2.2.1 Program counter is what to do. Bytecode interpreter works by changing the value of this counter to select the next need to execute bytecode instructions, branch, loop, jump, exception handling, thread recovery and other basic functions need to rely on this counter to complete. In order to restore to the correct execution position after the thread switch, each thread needs to have a separate program counter, the counters between the threads do not affect each other, isolated storage, we call this kind of memory area is "thread private" memory. If the thread is executing a Java method, this counter records the executing virtual machine byte-code instruction address. Simple answer: almost no memory. Used to remove an execution instruction. The 2.2.2 Java Virtual machine stack is also thread-private, and its lifecycle is the same as the thread. What the Java Virtual machine stack does. It serves the Java method. The virtual machine stack describes the memory model that is executed by the Java method: Each method creates a stack frame (the stack frame) to store information such as local variable tables, operand stacks, dynamic links, method exits, and so on. Each method from the call to the execution of the completion of the process, corresponds to a stack frame of the virtual machine stack into the stack to the process of stack. The Local variables table holds the various basic data types (Boolean, Byte, char, short, int, float, long, double), object reference, and ReturnAddress types (pointing to the address of a byte-code instruction) at compile time. The memory space required by the local variable table completes the allocation during compilation. Simple answer: Each thread executes each method in the stack to request a stack frame, each stack frame includes the local variable area and the operand stack, used to store the method call in the process of temporary variables, parameters and intermediate results. 2.2.3 Native method Stacks The local methods stack (Native method stack) and the virtual machine stack play a very similar role, the difference between them is only the virtual machine stack for the virtual machine to execute Java method (that is, bytecode) services, The local method stack serves the native method that the virtual machine uses. Simple answer: Used to support the execution of the native method, storing the state of each native method call. Sun hotspot the virtual machine stack with the local method stack. The 2.2.4 Java heap Java heap is an area of memory that is shared by all threads and is created when the virtual machine is started. The only purpose of this memory area is to hold the object instance, where almost all object instances allocate memory. The Java heap is the main area of garbage collector administration. Now collectorThe basic use of generational collection algorithm, so the Java heap can be subdivided into: the new generation and the old age, and then a little bit of Eden Space, from Survivor space, to survivor space. The purpose of further partitioning is to better reclaim memory, or to allocate memory more quickly.
In Java, the heap is divided into two distinct regions: the Cenozoic (Young) and the old. New Generation (Young) = 1/3 heap space size. Age (old) = heap space size of 2/3. The New Generation (young) is divided into three areas: Eden, from Survivor, to Survivor. Edem:from:to = 8:1: 1 The purpose of this division is to enable the JVM to better manage the objects in the heap memory, including the allocation of memory and recycling. The JVM uses only Eden and one of the Survivor areas to serve the object at a time, so there's always a Survivor area free whenever you want. Therefore, the new generation of real memory space is 9/10 (that is, 90%) of the Cenozoic space. The 2.2.5 method region method region, like the Java heap, is the internal method region of a JVM instance, which is a shared memory area of each thread, and is used to store data such as class information, constants, static variables, and Just-in-time compiler-compiled code that have been loaded by the virtual machine. The memory recovery target for this area is primarily for the collection of constant pools and the unloading of types. The 2.2.6 run constant-load pool (Runtime Constant Pool) is part of the method area. Used to hold the various literal and symbolic references generated by the compilation period, which are stored in the Run-time pool of the class loading the LIFO method area. 2.2.7 Direct Memory
2.3 Hotspot Virtual Machine Object QuestIt focuses on the data in virtual machine memory, how to create, how to layout, and how to access it. The whole process of object assignment, layout and access of hotspot virtual machine in Java heap is discussed. The size of the memory required to create the object for the 2.3.1 object is fully determined after the class load completes, and the task of allocating space for the object is equivalent to dividing a certain size of memory from the Java heap. When you create an object, you need to consider how to partition the available space. After the memory allocation is complete, the virtual machine needs to initialize the allocated memory space to 0 values. This step ensures that the object's instance fields are used directly in Java code without assigning an initial value, and that the program can access the 0 value of the data type of those fields. Execution of the new instruction then executes the <init> method, initializing the object to the programmer's will, so that a truly usable object is fully generated. 2.3.2 Object memory layout in hotspot virtual machines, the layout in which objects are stored in memory can be divided into 3 areas: the header (header), the instance data (Instance data), and the alignment fill (Padding). The access location of the 2.3.3 object currently has two kinds of using handle and direct pointer.
2.4 Combat OutOfMemoryError anomalies
2.4.1 Java Heap Overflow
2.4.2 Virtual machine stack and local method stack Overflow
2.4.3 method area and run frequent pool overflow
2.4.4 Native Direct Memory overflow
3rd Chapter garbage Collector and memory allocation strategy
3.1 OverviewGC needs to do 3 things: 1. Which memory needs to be recycled 2. When to recycle 3. How to Recycle
Why do you want to know about GC and memory allocations? When you need to troubleshoot various memory overflows, memory leaks, and when garbage collection becomes a bottleneck for the system to achieve higher concurrency, we need to implement the necessary monitoring and tuning of these "automated" technologies.
Garbage collection is for the Java heap and method area
are 3.2 objects dead?"Dead" objects need to be recycled. How to determine if an object is "dead". 3.2.1 Reference Counting algorithm: To add a reference counter to the object, whenever there is a place to reference it, counter value plus 1, when the reference is invalid, the counter value is reduced by 1, any time the counter is 0 object is impossible to be used again. The mainstream Java Virtual machine does not use a reference counter algorithm to manage memory, the most important reason is that it is difficult to solve the problem of circular references between objects.
3.2.2 Analysis algorithm is the mainstream of commercial programming languages, using this algorithm. Algorithm thinking: Through a series of objects called "GC Roots" as the starting point, starting from these nodes downward search, the search path is referred to as a reference chain (Reference Chain), when an object to the GC Roots is not linked to any reference chain, it is proved that this object is not available.
Which objects can be used as GC roots? 1. Object 2 referenced in the virtual machine stack (the local variables table in the stack frame). Object 3 referenced by a class static property in the method area. Object 4 in the method area that is referenced by a constant. The object summary referenced by JNI (that is, the general native method) in the local method stack is when the method is run, the object referenced in the method The object referenced by the static variable of the class, the object referenced by the constant in the class, the object referenced in the native method.
3.2.3 again talk about quoting
Reference type: 1. Strong references (strong Reference) are not recycled as long as a strong reference exists. Similar to "Object obj = new Object ()" 2. Soft reference (Soft Reference) 3. Weak reference (Weak Reference) 4. Virtual reference (Phantom Reference)
3.2.4 to survive or die. Even in the accessibility analysis algorithm, the object can not be reached, is not "Must die", at this time they are temporarily in the "probation" stage, to really declare an object to die, at least two times to go through the marking process: if the object in the analysis of accessibility after the GC Roots a linked reference chain, it will be marked for the first time and filtered once, if the object is necessary to execute the Finalize () method. When the object does not overwrite the Finallize () method, or the Finalize () method has been called by the virtual machine, the virtual machine treats both cases as "unnecessary execution".
Garbage collection of the 3.2.5 recovery method area (or permanent generation in a hotspot virtual machine) mainly reclaims two parts: discarded constants and unwanted classes. Recycling discarded constants is very similar to retrieving objects in the Java heap. For example, to reclaim the literal amount in a constant pool, the string "abc", the current system does not have any string object that references the "ABC" constant in the constant pool, and it is reclaimed. A symbolic reference to a class (interface), method, field in a constant pool is similar to this.
It is relatively easy to determine whether a constant is an "obsolete constant", and the condition of determining whether a class is a "useless class" is a lot more demanding. The "useless class" satisfies the following 3 conditions: 1. All instances of the class are recycled 2. The ClassLoader that loaded the class has been recycled 3. The corresponding Java.lang.Class object of this class is not referenced anywhere, and cannot be accessed by reflection at any point in the class.
3.3 garbage Collection algorithmIntroduce the thought and development process of the algorithm
The 3.3.1 tag-purge algorithm algorithm is divided into "mark" and "purge" phases: First mark all the objects that need to be reclaimed, and then uniformly recycle all the marked objects after the mark completes.
The algorithm has two deficiencies: 1. Efficiency problems, marking and cleaning up two processes are less efficient 2. Space problems, after the mark clears will produce a large number of discontinuous memory fragments, too much space debris may cause later when the program is running process needs to allocate larger objects, Unable to find enough contiguous memory and had to trigger another garbage collection action in advance.
3.3.2 Replication algorithm for recycling generation to solve efficiency problems, the "Copy" collection algorithm appears. It divides the available memory by capacity into two blocks of equal size, using only one piece at a time. When this piece of memory is used up, the surviving object is copied to another piece, and then the memory space used is cleaned up once. The cost is to reduce the memory to half the original.
3.3.3 Labeling-Sorting algorithm for recycling old age thinking: Divided into "Mark", "Collation", "clear" 3 steps 1. First Mark all objects that need to be recycled 2. Let all the surviving objects move to one end 3. Unexpected memory to clean off the end of the boundary directly
The 3.3.4 collection algorithm divides memory into Cenozoic and aged generations according to the different survival cycle of objects. In the Cenozoic, a large number of objects are found dead in each garbage collection, only a small number of surviving, then the replication algorithm, only to pay a small number of living objects replication costs can be completed collection. In the old age, because of the high survival rate and the lack of extra space to allocate it, you must use mark-clean or mark-organize.
algorithm implementation of 3.4 hotspot
3.4.1 enumeration root node when the execution system pauses, there is no need to check all execution contexts and global reference locations without leaking, and the virtual machine should have the means to know directly where the object reference resides.
The 3.4.2 Security Point program does not run at all points in time to start a GC, which can only be paused when it reaches a safe point. The selection of the safety point is basically chosen by the program "Whether it has characteristics that allow the program to execute for a long time". The most obvious feature of "long Time Execution" is instruction sequence taking, such as method call, circular jump, abnormal jump, etc., so the instruction with these functions will produce sagepoint.
3.4.3 Security Zone

3.5 Trash Collector
3.6 Memory allocation and recycling policiesJava Automatic memory Management is an automated solution to two problems: 1. Allocate memory to Objects 2. Reclaim memory allocated to an object
3.6.1 object priority in Eden allocation in most cases, objects are allocated in the Cenozoic Eden region. When the Eden area does not have enough space to allocate, the virtual machine initiates a MINORGC.
3.6.2 large object directly into the old age the virtual machine provides a-xx:pretenuresizethreshold parameter, so that objects larger than this setting value are allocated directly in the old age. The aim is to avoid a large amount of memory duplication between the Eden area and two survivor areas (because the new generation uses the replication algorithm to collect memory).
3.6.3 long-term survival of the object will enter the old age if the object of Eden was born and after the first minor GC survived, and can be survivor accommodated, will be moved to the survivor space, and Object age set to 1. Object in the survivor area every "endure" once minor GC, age increases 1 years old, when its age increases to a certain extent (the default 15 years old), will be promoted to the old age.
3.6.4 Dynamic Object Age judgment if the sum of all objects in the survivor space is greater than half of the survivor space, objects older than or equal to that age can go directly into the old age without waiting for the ages required in the maxtenuringthreshold.
3.6.5 Space Allocation Guarantee
6th Chapter class file Structure
6.1 OverviewThe program we write compiles into binary local machine code (Native code)
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.
6.3 class file StructureThe class file format uses a pseudo structure similar to the C language structure to store data, which has only two types of data: unsigned numbers and tables. 1. Unsigned numbers belong to the basic data type 2. A table is a composite data type that has more than one unsigned number or other table as a data item, and all tables are accustomed to the end of "_info". Tables are used to describe the data for a hierarchical relational composite structure.
6.3.1 Magic number and class file version the first 4 bytes of each class file are called Magic Numbers (Magic number), and its only function is to determine whether the file is a class file that can be received by a virtual machine. The 5th and 6th bytes are minor version number (Minor version) 7th and 8th bytes are the major version (Major version) version of the JDK can be backward compatible with previous versions of the class file and cannot be up compatibility.
The constant pool entry after the 6.3.2 constant pool followed by the primary and secondary version number. First, it is a U2 type of data that represents a constant Chirong count value (constant_pool_count). There are two main types of constants in a constant pool: literal (Literal) and symbolic references (symbolic References): 1. Literal: A constant concept that is closer to the Java language level, such as a literal string, a constant value declared as final, etc. 2. Symbolic references: The concept of compiler principles, including the following 3 types of constants: A. Class and interface fully qualified name (fully qualified name) B. field name and descriptor (descriptor) c. The name and descriptor of the method do not save each method in the class file. The final memory layout information for the field, so the symbolic references of these fields and methods cannot get a real memory entry address without a run-time conversion and cannot be directly used by the virtual machine.
6.3.3 Access Flag This flag is used to identify the access information at some class or interface level, including whether this class is a class or interface, is defined as a public type, is defined as an abstract type, and if it is a class, is declared final.
The 6.3.4 class index, the parent class index, and the interface index collection class file determine the inheritance of this class by three data. 1. Class index: Used to determine the fully qualified name of this class 2. Parent class Index: the fully qualified name 3 used to determine the parent class of this class. Interface index collection: used to describe what interfaces this class implements
The 6.3.5 Field Table collection field (field) includes class-level variables and instance-level variables, but does not include local variables declared inside the method.
6.3.6 Method Table Collection
6.3.7 Property Sheet Collection
6.4 Byte Code instruction introduction
6.5 Public design and private implementation

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.