"In-depth understanding of Java Virtual Machine JVM advanced features ..." Core notes

Source: Internet
Author: User

deep understanding of Java Virtual Machine JVM advanced features and Best Practices (second Edition) core notes JAVA Environment:


Java Virtual machine Advanced Features:One: Java memory area and memory exception

A): Run data area

1: Procedure counter (Program Counter Register), also known as "PC Register"

A: Used to indicate which instruction needs to be executed. (in assembly language, after the CPU gets the instruction, the program counter automatically adds 1 or the address of the next instruction according to the transfer pointer, so it loops until all instructions have been executed.) )

B: Because in the JVM, multithreading is switched by thread in turn to get CPU execution time. Each thread has a separate program counter, which belongs to the private one.

C: If the thread executes a Java method, this counter records the virtual machine bytecode instruction address being executed, and if the native method is being executed, the value in the program counter is undefined.

D: Because the size of the space stored in the program counter does not change with the execution of the program, there is no memory overflow phenomenon (OutOfMemory) for program counters.

2: Virtual machine stack (VM stack), also known as Java stack

A: Each method execution creates a stack frame that stores local variable tables, operand stacks, constant references, dynamic links, method exits, and other additional information;-XSS parameter setting.

B: For a variable of the base data type, it stores its value directly, and for a variable of the reference type, a reference to the object is stored. The size of the local variable table can be determined by the compiler, so the size of the local variable table will not change during program execution.

C: If the thread request is greater than the allowed depth of the virtual machine, Stackoverflowerror will be thrown, and if the virtual machine stack can be dynamically extended (also allowing a fixed length), the expansion is not enough memory to throw OutOfMemoryError.

3: Local method Stack (Native)

A: Similar to the virtual machine stack, the difference is that it serves the native method;

B: Some virtual machines (such as Sun HotSpot) combine the local method stack with the virtual machine stack;

4: Heap, or Java heap

the heap in A:java is used to store the object itself as well as an array (of course, the array references are stored in the Java stack);

The B:java Heap is the main area of garbage collector management, and the Java heap is created when the virtual machine is started and shared by all threads;

C:java Heap is divided into: Cenozoic and Laosheng generation;

D: In the implementation, can be both fixed size and can be extensible (through-XMX and-XMS control, settings do not automatically expand), if the heap memory is not enough will be thrown outofmemoryerror;

5: Method Area

A: In the method area, information about each class is stored (including the name of the class, method information, field information), static variables, constants, compiler-compiled code, and so on.

B: Alias Non-heap, non-heap, the purpose should be to distinguish Java heap, set-xx:permsize and-xx:maxpermsize limit method area size;
Many people in C:hotspot are accustomed to think of method areas as "permanent generations", but they are not equivalent in nature!
The hotspot uses a permanent generation to implement the method area, so that the GC can manage the method area just like the Java heap, eliminating the code that writes memory management for the method area.                  However, other virtual machines (JROCKIT,J9, etc.) do not exist for a permanent generation, in principle, how to implement the method area, is not constrained by the virtual machine specification, for the hotspot now also has to abandon the permanent generation and gradually changed to native memory to achieve the party The planning of the French district; The string constant pool that was originally placed in a permanent generation has been removed from the jdk1.7 hotspot;

D: OutOfMemoryError will be thrown when the method area fails to meet the memory allocation;

E: Part of the method area when running a constant pool;

6: Direct memory, limited by physical memory size;

II) garbage collector and memory allocation policy

1: Program counter, virtual machine stack, local method stack are born with thread, die with thread, need to recycle heap and method area;

2: Is the object dead? Has the object been recycled?

1) Reference counting method: To add a reference counter to the object, whenever the object has a reference, the counter is added 1, the reference failure is reduced by 1, when the reference counter is 0 o'clock, the object can no longer be used (recycling) (many people's answer).
However, at least the mainstream Java Virtual machine does not use this algorithm to manage memory, mainly because it is difficult to resolve circular references between objects.

      2) Accessibility analysis algorithm: The mainstream is the algorithm to determine whether an object survives.
            The basic idea of this algorithm is to use some of the column's ' GC Roots ' objects as the starting point from which to start searching, search through       & nbsp     path becomes ' reference chain ', when an object to GC roots no reference chain dies, that is, from GC roots to this object unreachable. License             This object is not available and these objects will be judged recyclable.
            But these objects are not ' undead ' (probation stage), to declare an object dead, at least two times to go through the mark. GC roots will be             for the first time and filter by the criteria that this object is not required to execute the Finalize () method when the object does not overwrite              finalize () or Finalize () have been called by the virtual machine, both of which are considered "no need to execute". In addition, it is necessary to execute, that               The object will be put into the queue of F-queue, the Finalize () method is the last chance for an object to escape death (assign the object to & nbsp           a variable); Later, the GC makes a small second mark on the objects in F-queue, and two times the object is actually recycled.



3) Recovery method area:

The A:java virtual machine specification can not require virtual machines to implement garbage collection in the method area, and the method area (permanent generation) garbage collection efficiency is low.

B: Permanent garbage collection is divided into two parts: obsolete constants and useless classes.
Recycling obsolete constants is very similar to retrieving objects in the Java heap, but constants are removed from the Chang (recycle) when no other local references are used, as is the case with symbolic references such as Class (interface) fields in the method area.
The following 3 conditions are considered ' useless classes ': (whether or not it is recycled, the virtual machine provides some setup parameters)
1: Instances of this class have been 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 means of the launch of the class


4) Garbage collection algorithm:

A: Tag-purge algorithm: divided into two stages: Mark and clear. There are two problems with 1: Inefficient 2: A large amount of discontinuous memory fragmentation is generated.

B: Copy algorithm: Run efficiently.

C: Tag-collation algorithm: This algorithm is commonly used in the old age.

D: Generational collection algorithm:
The current garbage collection of commercial virtual machines is collected by generation of generations;
The memory is divided into several blocks according to the different life cycle of the object.
In general, the Java heap is divided into the new generation and the old age, according to the characteristics of the different times,
In the new generation, each garbage collection has a large number of objects die, so the use of replication algorithm;
In the old age, the target survival rate was higher, and the labeling-clearing or labeling-sorting algorithm was used.

5) Garbage collector: If garbage collection algorithm is the method of memory recycling, then garbage collector is the concrete implementation; there are 7 kinds.

a:serial Collector: Single thread. The oldest collector in the history of development. Simple and efficient, jdk1.3 is the only choice before the new generation.

b:parnew Collector: Multi-threaded version of serial; is the first generation of a new collector in a virtual machine running in service mode. jdk1.3

C:parallel Scavenge Collector: The new generation collector using the replication algorithm is also a parallel multi-threaded collector; jdk1.4.

d:serial Old collector: is the older version of the Serial to send the machine, single-threaded, using the tag-sorting algorithm, the main meaning is to give Cl Virtual machine usage in ient mode; jdk1.5

e:parallel old collector: Parallel scavenge collector older version, multi-threading, using tag-collation algorithm; jdk1.6

f:cms Collector: A collector that targets the shortest recovery pause time, based on the tag-purge algorithm. Concurrent collection of low pauses. jdk1.5

j:g1 Collector: jdk1.7, a garbage collector for service-side applications that may replace the CMS collector in the future;

Features: 1 parallel and concurrency, 2 generation collection, 3 space integration, 4 predictable pauses;
Operation steps: 1: initial tag, 2: Concurrency tag, 3: Final tag, 4: Filter and recycle;

6) Memory allocation and recovery policy:

A. Object precedence is allocated on Eden:

in most cases, the object is allocated on the new generation of Eden, but there is no space on Eden, and the virtual opportunity to initiate a minor GC (which occurs in the new generation of garbage collection actions, more frequently, and generally faster); the older GC (Major GC or Full GC), which occurred in the old age of GC, appeared major GC, often accompanied by at least one Minor GC (but not absolute);

B. Large objects go straight into the old age: Large objects are Java objects that require a lot of contiguous memory space, most typically long strings and arrays

C. The long-term survival of the object will enter the old age:

There is an Object age calculator, every 1 times minor GC age is added 1 years old, when the age reaches the default (15 years old) will be promoted to the old age, this age threshold can be set-xx:maxtenuringthreshold;

Write a blog a bit tired, not finished to be continued.


"In-depth understanding of Java Virtual Machine JVM advanced features ..." Core notes

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.