directory

  • 1. The description of the JVM
  • 2. Class loading subsystem
  • 3. Run time data area
  • 4. Vm stack
  • 5. The heap area
  • 6. Method of area
  • 7. Object instantiation and memory access
  • Overview of garbage collection
  • 9. Classic garbage collector
  • 10. The G1 and ZGC

1. Overview of garbage collection

1.1. What is garbage

Garbage refers to an object that has no pointer to it during program execution. This object is garbage that needs to be collected

If the garbage is not collected in time, the garbage will remain in the memory until the end of the application, and the reserved space cannot be used by other objects, which may cause memory overflow

1.2 Why GC

1. If garbage collection is not performed, garbage will use up the memory

2. Move the fragments of memory to the other end of the heap so that the JVM can allocate the decluttered memory to new objects

2. Related algorithms of garbage collection

There are two phases of GC: the mark phase and the clean phase

2.1 Garbage marking stage algorithm (object survival judgment)

An object is declared dead when it is no longer referenced by any living object

There are two ways to judge the survival of objects: reference counting algorithm and reachability analysis algorithm

2.1.1 Reference Counting Algorithm

Holds a reference counter property of integer for each object, which is used to record when the object is referenced

Advantages: simple implementation, easy identification of garbage object, high judgment efficiency, no delay in recycling

Disadvantages: Separate field storage counters are required, increasing storage space overhead

Each assignment requires updating the counter, adding time overhead

Reference counters are unable to handle circular references, resulting in the Java garbage collector not using such algorithms

2.1.2 Accessibility Analysis Algorithm (Root search algorithm)

It can effectively solve the problem of circular reference in reference counting algorithm

GC Roots: Follow set, a set of references that must be active

Basic idea:

The reachability analysis algorithm takes GC Roots as the starting point and searches the reachability of the target object linked by GC Roots from top to bottom

After using the reachability analysis algorithm, the living objects in memory are directly or indirectly connected to the object set, and the path searched is called the reference chain

If the target object is not connected by any reference chain, it is unreachable, meaning that the object is dead and can be marked as a garbage object

In a reachability analysis algorithm, only objects that can be linked directly or indirectly to an object set are alive

GC Roots include the following elements (two stacks + two zones + synchronous locks)

  • Objects referenced in the virtual stack, such as parameters used in methods called by individual threads, local variables, etc
  • Objects referenced in the local method stack,
  • The object referenced by the class static property in the method area. Example: Java class reference type static variable
  • An object referenced by a constant in a method area, such as a reference in a string constant pool
  • All objects held by synchronized
  • Internal references of the Java VIRTUAL machine
  • Jmxbeans that reflect what’s going on inside the virtual machine

In addition to these fixed collections of GC roots, there are other objects, such as generational collection and local collection, depending on the garbage collector the user chooses and the memory region currently being reclaimed

If the pointer retrieves a certain area of the Java heap, the object may be referenced by other elements of the heap. In this case, the associated object should also be considered in GC Roots to ensure the accuracy of the reachable analysis

2.1.3 Conditions for determining GC ROOTS

If it is a pointer that stores objects in the heap but does not store objects in the heap, it is a GCROOT

Note: Reachability analysis must be performed in a consistent snapshot, without which the accuracy of the analysis results cannot be guaranteed. This is also an important reason to stop the wold when GC is running.

3. Object finalization mechanism

The Java language provides an object finalization mechanism that allows developers to provide custom processing logic for objects before they are destroyed

When the garbage collector finds that there is no reference to an object, that is, garbage collection of the pair of objects, it always calls the Finalize () method of the object first

The finlize method should not be called actively, but should be called by the garbage collector for the following reasons:

  • Finlize () may cause objects to revive

  • The execution time of Finalize () method is not guaranteed. It is completely determined by GC thread. In extreme cases, if GC does not happen, Finalize method will have no chance to execute

  • A bad Finalize () method can affect GC performance

4. Three states of objects in the VM

  • Reachable: Reachable objects, starting with nodes

  • Resurrectible: All references to objects are released, but objects may be resurrected in Finalize ()

  • Untouchable: When the finalize() method of objects is called and there is no resurrection, then the objects will be in the untouchable state. Unretouchable objects cannot be resurrected.

5. Related algorithms of the clearance phase

5.1 Mark-sweep algorithm

Execution: When the heap is about to run out of effective memory, the entire program is stopped, called Stop the world, and two tasks are performed. The first is mark, the second is clear

Tag: Collector traverses from the reference to the node and marks the objects it can traverse, typically in the object header as reachable objects

Clear: Collector traverses memory linearly from beginning to end. If an object is not marked as reachable in its header, it is reclaimed

Advantages: simple implementation,

Disadvantages: inefficient, requiring twice traversing the heap memory space

The process needs to be stopped during GC, resulting in poor user experience

The free memory generated by this reclamation algorithm is not continuous, resulting in free memory fragments, which need to maintain a free linked list and consume additional memory space

The meaning of clear, clear is not really clear, but the address of the object to be cleared to add to the free list.

5.2 Copying Algorithms

Living memory space can be divided into two pieces, each time using only one piece of memory region, that will be used when garbage collection memory live objects are copied to the unused another fast memory space, after clearance is using all objects in the memory, the role of the exchange of two memory, finally complete garbage collection

Advantages: No marking and clearing process, simple implementation, efficient operation

The replication process does not generate memory fragmentation, and the reclaimed memory space is continuous

Disadvantages: Requires twice the memory space, will waste memory space

In G1 gc, which is split into a large number of regions, copying rather than moving means that the GC needs to maintain the relationship of object references between regions, which can be costly in both memory and time

5.3 Mark-Compact

Stage 1: As with the mark-clear algorithm, all referenced objects are marked from the same node

Stage 2: Compress all living objects to one end of memory, drain them in order, and then clear all space outside the boundary

Advantages: Eliminating the disadvantage of scattered memory in the mark-clear algorithm. When we need to allocate memory to a new object, we only need to hold the starting address of memory.

Eliminates the high cost of halving memory in the replication algorithm

Disadvantages: Low efficiency in terms of efficiency

When you move an object, if the object is referenced by another object, you need to sort out the reference address,

You need to pause the user application throughout the movement: STW

5.4 Comparison of the three algorithms

5.5 Generational Collection Algorithm

Different collection methods are used for objects with different life cycles, and different garbage collection algorithms are used according to different age generations

Young generation: the region is smaller than the old generation, the object life cycle is short, the life cycle is short, and the collection is frequent

The replication algorithm is the fastest, and the two survivor designs are changed

In the old age, the area is larger, the object is larger, and the recycling is not as frequent as the young generation

Generally, tag clearing algorithm or tag compression algorithm is used in combination

The cost of the Mark phase is proportional to the number of viable objects

The cost of the Sweep phase is proportional to the size of the area managed

The overhead of the Compact phase is proportional to the number of objects

5.6 Incremental Collection Algorithm

The above algorithm will occur STW, if the garbage collection time is long, the program will be suspended for a long time

You can alternate the garbage collection thread with the application thread, collecting only a small area of memory each time, and then switching to the application thread again and again, reducing the pause time

Disadvantages: Intermittent execution of application code reduces system pause time, but the overall cost of garbage collection increases due to thread switching and up-down conversion consumption, resulting in reduced system throughput

5.7 Partitioning Algorithm

The larger the heap, the longer the gc takes, and you can reduce the pauses generated by a GC by splitting a large memory area into smaller modules, reasonably collecting smaller intervals at a time, based on the target pause time, rather than the entire heap

Each small area is used separately and recycled independently. The advantage of this algorithm is that multiple small areas can be reclaimed at a time

6. Concepts related to garbage collection

6.1 1.System.gc() or Runtime.geTruntime ().gc() call,

The display triggers the full GC, which simultaneously collects old and new generation objects in an attempt to free the memory of discarded objects

6. 2 Memory overflow

There is no free memory, and the garbage collector cannot provide more memory

Cause of vm memory overflow

  • The memory Settings of the Java VIRTUAL machine are insufficient
  • A large number of large objects are created in the code and cannot be collected by the garbage collector for a long time (there are references)

6.3. Memory leaks

A memory leak occurs when objects are no longer used by the program, but the GC cannot reclaim them

Although a memory leak does not immediately cause a program to crash, once a memory leak occurs, the available memory in the program will be gradually eaten up until all the memory is exhausted, resulting in Outofmemory

Schematic of a memory leak

Examples of memory leaks

1. Singleton mode

The life of a singleton is as long as that of an application, so a singleton that holds a reference to an external object cannot be reclaimed, resulting in a memory leak

2. Some resources that provide close are not closed, resulting in memory leaks

Database links, network links, and IO links must be manually closed, otherwise they cannot be reclaimed

STOP THE WORLD

During gc, application pauses occur, and the entire application is paused without any response