JVM garbage collection_1. How to determine the object can be recycled

How to judge the object can be recycled

Reference counting

Disadvantages: In the case of circular references, the counts of the two objects are both 1, resulting in the two objects being unable to be released

img

Reachability analysis algorithm

  • The garbage collector in the JVM explores all surviving objects through reachability analysis
  • Scan the objects in the heap to see if the object can be found along the reference chain starting from the GC Root object. If it cannot be found, it means it can be recycled
  • Can be used as the object of GC Root
    • Objects referenced in the virtual machine stack (local variable table in the stack frame).
    • Objects referenced by class static properties in the method area
    • Objects referenced by constants in the method area
    • Objects referenced by JNI (the Native method in general) in the native method stack

Five references

img

Strong citation

Only when the GC Root does not reference the object, will the strongly referenced object be recycled

  • As shown in the figure above, when the B and C objects do not reference the A1 object, the A1 object will be recycled
Soft reference

When GC Root points to a soft reference object, the object referenced by the soft reference will be recycled when the memory is insufficient

  • As shown above, if the B object no longer references the A2 object and the memory is insufficient, the A2 object referenced by the soft reference will be recycled
Use of soft references
public class Demo1 {
    
    
	public static void main(String[] args) {
    
    
		final int _4M = 4*1024*1024;
		//使用软引用对象 list和SoftReference是强引用,而SoftReference和byte数组则是软引用
		List<SoftReference<byte[]>> list = new ArrayList<>();
		SoftReference<byte[]> ref= new SoftReference<>(new byte[_4M]);
	}
}

If insufficient memory is found during garbage collection, the soft reference itself will not be cleaned up when the object pointed to by the soft reference is reclaimed

If you want to clean up the soft references , it is necessary to make a reference queue

public class Demo1 {
    
    
	public static void main(String[] args) {
    
    
		final int _4M = 4*1024*1024;
		//使用引用队列,用于移除引用为空的软引用对象
		ReferenceQueue<byte[]> queue = new ReferenceQueue<>();
		//使用软引用对象 list和SoftReference是强引用,而SoftReference和byte数组则是软引用
		List<SoftReference<byte[]>> list = new ArrayList<>();
		SoftReference<byte[]> ref= new SoftReference<>(new byte[_4M]);

		//遍历引用队列,如果有元素,则移除
		Reference<? extends byte[]> poll = queue.poll();
		while(poll != null) {
    
    
			//引用队列不为空,则从集合中移除该元素
			list.remove(poll);
			//移动到引用队列中的下一个元素
			poll = queue.poll();
		}
	}
}

**The general idea is: **Check whether there is a soft reference in the reference queue, if there is, remove the soft reference from the collection where it is stored (here is a list collection)

Weak reference

When only a weak reference refers to the object, the object referenced by the weak reference will be recycled during garbage collection, regardless of whether the memory is sufficient

  • As shown in the figure above, if the B object no longer references the A3 object, the A3 object will be recycled

The use of weak references is similar to soft references , except that SoftReference is replaced by WeakReference

Phantom reference

When the object referenced by the virtual reference object is recycled, the virtual reference object will be put into the reference queue, and the virtual reference method will be called

  • A manifestation of virtual reference is to release the memory allocated by direct memory . When the referenced object ByteBuffer is garbage collected, the virtual reference object Cleaner will be placed in the reference queue, and then the Cleaner method of Cleaner will be called to release the direct memory.
  • As shown above, the B object no longer references the ByteBuffer object, and the ByteBuffer will be recycled. But the memory in direct memory has not yet been reclaimed. At this time, you need to put the virtual reference object Cleaner into the reference queue, and then call its clean method to release the direct memory
Finalizer reference

All classes inherit from the Object class, and the Object class has a finalize method. When an object is no longer referenced by other objects, it will first put the finalizer reference object into the drinking queue, then find the object it refers to based on the finalizer reference object, and then call the finalize method of the object. After the call, the object can be garbage collected

  • As shown above, the B object no longer refers to the A4 object. This is the finalizer object will be put into the reference queue, and the reference queue will find the object it refers to based on it. Then call the finalize method of the referenced object. After the call, the object can be garbage collected
Reference queue
  • Soft reference and weak reference can cooperate with reference queue
    • After the objects referenced by weak and phantom references are recycled, these references will be put into the reference queue to facilitate the recycling of these soft/weak reference objects together
  • Phantom reference and finalizer reference must cooperate with reference queue
    • Virtual references and finalizer references are associated with a reference queue when they are used

2. Garbage collection algorithm

Mark-clear

img

Definition : As the name implies, the mark-sweeping algorithm refers to that in the process of garbage collection performed by the virtual machine, the marking algorithm is used to determine the recyclable objects, and then the garbage collector clears the corresponding content according to the identifier, and makes the corresponding space for the heap memory

  • The freeing up of the memory space here is not to clear the bytes of the memory space to 0, but to record the start and end address of this memory. The next time the memory is allocated, this memory will be directly overwritten.

Disadvantages : A large amount of memory fragmentation is prone to occur , which may not be able to meet the memory allocation of large objects. Once the object cannot be allocated, it will cause the jvm to start the gc. Once the gc is started, our application will be suspended, which will cause the application to respond Slow down

Mark-up

img

Mark-define objects that are not referenced by GC Root will be recycled, and the memory space occupied by them will be clear. Then sort out the remaining objects, which can effectively avoid problems caused by memory fragmentation, but because the overall need to consume a certain amount of time, the efficiency is low

copy

img

img

img

img

Divide the memory into two areas of equal size, FROM and TO (empty in TO). First put the objects referenced by GC Root from FROM into TO, and then reclaim objects not referenced by GC Root. Then swap FROM and TO. This can also avoid the problem of memory fragmentation, but it will take up twice the memory space.

Guess you like

Origin blog.csdn.net/aaaaa1111111199/article/details/113103641