ThreadLocal (Part I) --- Reference type analysis && GC tuning experience in Java

B station up main video

GC tuning experience:
what happened in Xiaomi: frequent GC on the online system, intermittent OOM, after checking for a long time, it was found that some C++ programmers rewritten the finalize method after converting to java; wrote many time-consuming methods in finalize For example, closing the open file and closing the network connection will cause the garbage collection to be very slow and prolong the life cycle of the entire object. If the object is generated very fast, the recovery speed is very slow, which will cause the object to accumulate. Many, causing memory overflow OOM


What kind of reference types are there in Java? :
What are the characteristics of each reference type?
What are the application scenarios of each reference type?

Strong references (General references are strong references): Object o = new Object(); ---- Point a variable to a memory space (point to an object)
Soft Reference (SoftReference):
Insert picture description here

Insert picture description here
m points to SR space [soft reference space] (hard line), SR points to byte array (soft line)
soft reference features: Xmx memory space is not reclaimed, soft reference will be reclaimed
Insert picture description here
weak reference when memory space is not enough :
Insert picture description here
Output:
Insert picture description here
phantom reference (no matter how you get it, you can’t get it, no matter if you return it or not):
Insert picture description here
phantom reference has only one function-direct memory management (Java’s nio, and netty use direct memory [direct byte buffer]) belong to the operating system Yes, memory other than jvm can be accessed directly without copying the jvm. When the
Insert picture description here
virtual reference is recycled, the object M will be placed in the queue first, and the queue will be scanned periodically. If there are objects in the queue, it must be performed Special processing; generally speaking, the objects in the queue point to the direct memory outside the queue. When the JVM recycles the objects in the queue, it must first clear the direct memory outside the JVM pointed to by the objects in the queue, and then clear the queue That object; so the phantom reference only serves as a notification when it is recycled.
Insert picture description here
Insert picture description here
The role of phantom references in recycling is a signal

Insert picture description here
Strong references : very hard, unless garbage collection can reclaim them,
soft references cannot be reclaimed even if memory overflows : soft references are reclaimed if there is not enough memory space.
Weak references : as long as there is garbage collection, if there is no strong reference to this object , Then the weakly referenced object will be recycled, and the
phantom reference will be recycled as soon as it does not exist during the garbage collection period : get can't get it; when it is recycled, a notification will be given and the notification will be placed in the queue, generally Used to manage direct memory
Insert picture description here


Guess you like

Origin blog.csdn.net/nikyae/article/details/110872970