In-depth understanding of Java Virtual Machine Chapter 23rd summary
Java memory area and memory overflow
memory allocation diagram in a Java Virtual machine :
The characteristics of each region are summarized in the following table:
Additional notes:
- When multiple threads are likely to allocate memory on the heap, there may be a synchronization problem with memory allocations, with two solutions, one is synchronous memory allocation, and the other is the use of Tlab, which is to allocate buffers for each thread in the Java heap by pre-allocating a small chunk of the private local thread. This allows the thread to allocate memory on its own tlab, thus avoiding the overhead of synchronization. However, synchronization is still required when the Tlab is allocated full redistribution Tlab;
- To determine whether a class belongs to a useless class, the condition that "can" be reclaimed is: 1 All instances of the class have been reclaimed, 2 the ClassLoader that loaded the class have been reclaimed, and 3 the class is not referenced anywhere in the Java.lang.Class object. Note that it is only possible, not to be recycled.
memory allocation : The way in which the virtual machine is used is determined by whether the memory is structured, and whether the memory is structured is determined by the recovery algorithm.
- Pointer collisions: Assuming that all the allocated memory is on one side, while the idle is on the other, the middle of the two uses a pointer as the cutoff point, and when the memory needs to be allocated only the pointer can be moved, which is called a pointer collision. Serial, parnew, etc. with compact collector with this method
- Idle list: Allocated memory and idle memory in a virtual machine are not structured and interleaved, so you need to maintain a list to indicate which memory is available, and when you need to allocate memory, you need to find the available size of memory by querying the list. Such a way is called a free list. This method is used by the MARK-SWEEP algorithm-based collector such as CMS
The memory layout of the object in the hotspot virtual machine is shown in the following table:
In the Java specification, only a reference to an object is specified for the reference type, but there is no way to access the referenced data. As a result, there are different ways to access different virtual machines, mainly in two ways:
- Using a handle: a chunk of the Java heap is used as a handle pool to store the handle, a handle that includes a pointer to the object instance data, and a pointer to the object data type, and the address of the object's handle is stored in reference. Reference accesses objects indirectly through a handle. The advantage of this is that when the object moves, only the pointer in the rectangle is changed, and the corresponding reference does not need to be changed;
- Direct access: Reference stores the object address, which allows direct access to the data through reference. The data object in the Java pair contains a pointer to the object data type, such as the type pointer mentioned above. The benefit of this approach is that the access speed is fast and the cost of pointer positioning is less than the way the handle is used. This is the way HOTSPOTVM is used.
Two ways to use the diagram description, such as: Picture Source http://www.th7.cn/Program/java/201604/846729.shtml
Garbage collection algorithm
There are two algorithms that can be used to determine whether an object is dead or not:
- Reference counting algorithm : For an object whose number of references is incremented one time, the count is incremented by 1, minus 1 when the reference fails. When its count is 0 o'clock, the object is considered dead. The algorithm is characterized by high efficiency, but it is difficult to solve the problem of mutual reference between objects. MS-based COM technology with this algorithm, as well as Python
- Accessibility Analysis Algorithm : The core of the algorithm is to detect all referenced objects starting from GC roots. If there is no reference chain between an object and the GC roots, the object is considered dead. This algorithm is used in Java, C #, and so on.
the objects that can be used as GC roots are:
- Objects referenced in the virtual machine stack (local variable table in the stack frame)
- Object referenced by class static property in method area
- Objects referenced by constants in the method area
- Objects referenced in the native method
There are four types of reference strength in Java:
- Strong references: Strong references are never reclaimed by the garbage collector
- Soft reference: The system provides a SoftReference class to represent, but not required, objects. The recovery time is that if the reference object after the memory is not enough, then reclaim such references, if not enough, then oom
- Weak references: Represented by the WeakReference class, such references can only survive until the next garbage collection. When the garbage collector is working, objects that are associated only with weak references are reclaimed regardless of whether the current memory is sufficient. That is, a weak reference must be recycled whenever a GC occurs. It differs from a reference chain that has not been to GC roots: These objects can still be accessed through a weak reference, but objects without a reference chain will never be accessible again.
- Virtual reference: Implemented by Phantomreference, which has no effect on the life time of the object, and its existence means that it can receive a system notification when the object referenced by the virtual reference is recycled.
As shown in the GC workflow of the system , in general, an object being recycled can go through two tagging processes and may save itself in the Finalize method to avoid being recycled.
Several typical garbage collection algorithms :
Specific implementation details of the garbage collection algorithm in the HotSpot VM: For accurate results, the GC needs to freeze all threads during the scan. The current mainstream Java virtual take is accurate GC, that is, the system knows the data of each memory location is exactly what data type, in the case of hotspot, it takes a data structure called Oopmap to implement such mapping records. With this information, the virtual machine directly knows where the object's references are stored, thus avoiding a one-to-one check of the memory and speeding up the GC scan. Each instruction of a program can cause a change in the reference relationship or memory data, which can lead to changes in Oopmap, so that if each instruction generates a corresponding OOPMAP data then it is quite space-consuming, so the concept of a safe point is proposed (safepoint) That GC scans are performed only when each thread is running to the corresponding security point of the thread, so that the oopmap is generated for instructions on the security point, which reduces the number of oopmap. The selection of the safety point to take into account the GC frequency and system performance of the comprehensive impact, the general choice of method calls, loop jump, abnormal jump, etc. "with the program for long-running characteristics" point. There are both preemptive and active interrupts in order for the thread to go to the security point to pause for GC scanning. There is another problem here, if you encounter a thread such as a sleep state, then it will not move around, if it happens not at a safe point of sleep, then it means that it will never go to the security point, so the security zone is raised (saferegion) concept. That is, the points in this area are safe points. After the thread enters the security point, it signs itself into the security zone and must wait until the GC has finished executing before leaving the security zone.
Individual garbage collector:
Several of the most common memory allocation rules :
- Object Precedence in Eden Zone: When the Eden area has insufficient memory, the system will initiate a faster minor GC
- Large objects go straight into the old age: for large objects such as long arrays and strings, they are assigned directly to the old age zone. Therefore, for such a short life cycle of large objects, it is very likely to cause GC, should be avoided as far as possible.
- Long-term survival of the object will enter the old age: a If in the survivor area experienced multiple minor GC also survived, the default 15 times, will be moved into the old generation district.
- Dynamic Object Age judgment: This bar is combined with the previous one, if the size of all objects of the same age in survivor and more than half of survivor, then objects older than or equal to that age will be moved into the older generation area without waiting 15 times
- Space Allocation Guarantee: A replication collection algorithm for the new generation. If the parameters allow, when the minor GC is executed, if the surviving objects cannot be placed in the survivor, then many objects will be placed directly into the old age zone. If the old age does not have enough space, then a full GC will take place to get more space
Java Memory and GC