当前位置:网站首页>JVM GC garbage collection brief

JVM GC garbage collection brief

2022-07-07 20:00:00 Whiteye too white

GC

in the light of java Pile recycling .

Reachability analysis algorithm : from gc root Search for reference chains .

Eden Trigger when area is full minor gc, The surviving object enters survivor District ( Copy algorithm ), Age plus 1, When the age of the object is greater than the threshold ( Default 15) when , Into the old age . You can set the threshold through parameters , The virtual machine will be dynamically adjusted during operation . When a certain age exceeds survivor Half of the area ( Default 50%) when , Take this age and MaxTenuringThreshold As the new threshold .

The new generation ( Mark - Copy algorithm )

  • Mark living objects - Because there are fewer surviving objects
  • Memory size eden:to:from = 8 : 1 : 1
  • minor gc after ,Eden The objects that live in the area are copied to “survivor To”;“survivor From” The living objects in the zone , When the age reaches the threshold, we enter the old generation , Otherwise it will be copied to “survivor To” in .survivor From and survivor To swap .
  • survivor From Out of space , The part that cannot be put down will enter the elderly generation ahead of time .

Old age ( Mark - eliminate - Sorting algorithm )

When there is not enough memory in the old age full gc( Mark - eliminate ), many times full gc After finishing . Frequency parameter CMSFullGCsBeforeCompaction The default is 0 Time .

Strong citation :gc No recovery .
Soft citation : Only recycle when memory is insufficient .
Weak reference :gc Recycling .
Virtual reference : Before recycling , Get the system notification , Some treatment can be done before recycling .

java Memory model

  1. Program counter : Control program execution .
  2. Java Virtual machine stack : Store local variable table 、 The stack of operands 、 Dynamic connection 、 Method export, etc .
  3. Native Method Stack : Store local (native) Methods information .
  4. Java Pile up : Stored object instance .
  5. Method area : Storing static data ( Constant 、 Static variables, etc ). Contains the runtime constant pool : Store constants .

Java Heap reclamation Algorithm

  1. Reference counting ( Most virtual machines do not )
  2. Reachability analysis algorithm
    according to “ Root object ” Search its “ References to the chain ” Judge whether an object is reachable , Filter unreachable objects , If the object exists finalize() Method , also finalize() Method has not been implemented , Then put the object in the queue , from Finalizer The thread executes its finalize() Method to release the object . If the object in the queue is executing finalize() Re referenced before release , Will be moved out of the release queue .JDK9 After being removed .

Recovery object judgment

  1. Reclaim string constants that do not have references ( String constant pool in heap , The runtime constant pool is in the method area )
  2. Class not in use , just “ Allowed to recycle ”
    At the same time satisfy :
    All instances of this class are recycled ;
    The class loader for this class has been recycled ;
    Of the class Class The object does not exist and is referenced , Unable to launch access .

GC The trigger condition

Minor GC The trigger condition :Eden Area is full .

Full GC The trigger condition :

  1. call System.gc when , System recommendation implementation Full GC, But not necessarily .
  2. There is not enough space in the old age .
  3. Insufficient method space .
  4. adopt Minor GC The average size of backward into old age is larger than the available memory of old age .
  5. from Eden District 、From Space District direction To Space When copying , The object size is greater than To Space Available memory , Then transfer the object to the old age , And the available memory of the old age is smaller than the size of the object .

【 Add 】 JDK 7 Before ,HotSpot When using the permanent generation to implement the method area , Implementation is completely in line with this logical concept . And in the JDK 7 And after ,HotSpot Already put the string constant pool originally in the permanent generation 、 Static variables, etc. are moved to the heap , At this time, class variables will follow Class Objects are stored together in Java In the pile .

原网站

版权声明
本文为[Whiteye too white]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071749177601.html