Any questions can go into a JVM - Garbage Collection

introduction

  • What memory need to recycle?
  • When the recovery?
  • How to recycle?

Talk of java reference type?

Strong references

   Similar to the Object obj = new Object()kind of strong references, as long as the reference is still strong, the garbage collector never recovered off the object being referenced.

Weak references

   The object is used to describe non-essential objects, but its intensity is softer than the associated reference only to survive until the next collection occurs. Soft references used SoftRefrenceto represent.

False quote

   Whether an object has a virtual presence referenced, it will not affect its survival, can not get through the virtual instance of an object reference. JDK 1.2After use PhantomRefrenceto represent.

Weak references

  Weak references previously described non-essential objects, but its intensity is weaker than the soft references, cited weakly associated objects can only survive until the next time the garbage collector, JDK 1.2with weakRefrenceexpressed.

Garbage collection is the purpose?

  Clear reference is no longer used, automatically free up memory.

GC is how to determine whether the object is recycled?

  GCRoot, If an object GC Rootsbetween the reference no direct or indirect relationship, such as:

  • A loss of any object reference
  • Two circular island each reference object

These objects verdict "reprieve", it can be recycled

What objects can be used as GC Roots do?

  • Class static properties referenced objects
  • Const reference object
  • VM stack referenced objects
  • Native method stacks of reference objects

Talk about the correlation algorithm for garbage collection?

  • Mark - sweep algorithm

  The algorithm from each GC Rootsdeparture, in turn marked with the reference object relations, and finally there will be no marked objects cleared. But this method will bring a lot of space debris, resulting in the need to allocate a large continuous space of time easily trigger FGC.

  • Mark - Collation Algorithm

   Defrag computer-like, first of all will be the GC Rootsstarting mark live objects, and then the live objects, and then finishing the live objects to one end of the memory space, form a continuous space has been used, the last part of the total clean-up outside of the space has been used away, so as not to generate space debris.

  • Mark-Copyalgorithm

  To be able to organize the parallel marks and spaces are divided into two, wherein an activation of a time, simply copy the surviving object to another Space when a garbage collection is not active, the inactive space marked as activated the active space marked as inactive, then remove the original object space.
  Heap memory space is divided Edenand two smaller recorded as inactive, then remove the original meta-object space. Heap memory space is divided into a larger Edenand two smaller Survivor, use a time Edenand Survivora region. In this case Mark-Copyreducing waste memory space.
  Mark-CopyNow as a mainstream YGCgarbage collection new generation of algorithms.

Tell me what you know about the garbage collector?

  • evening

  SeralRecycling is a major for YGCthe garbage collector, serial single-threaded way to complete GCthe task, which Stop The Worldreferred to STWthat garbage collection pauses at some stage throughout the application execution. FGCA relatively long time, often FGCwill seriously affect the performance of the application

  • CMS

algorithm:

  Mark - sweep algorithm, a large amount of space debris

solution:

   -XX: + UseCMSCommpactAtFullCollection = nParameters, at n times FGCafter JVManother in the implementation of the old generation space defragmentation, perform a space defragmentation, but the defragmentation phase space will lead to STW.

step:

  1. Initial labels (Initital Mark)
  2. Concurrent mark (Concurrent Mark)
  3. Relabeled (ReMark)
  4. Clear concurrent (Concurrent Mark)
problem:

. A l, 3 : initiator STW

Solution:
   In order to reduce STWthe number of times, GMSyou can also configure the -XX: + CMSFullGCsBeforeCompaction = n parameter, after n times FGC, JVMthen finishing in the implementation of space debris years old.

b. 2, 4 : the application can execute concurrently, but also more time-consuming operation, but does not affect the application of policy implementation.


  • G1

HotSpot introduced a new generation in JDK7 G1garbage collection, through -XX: + UseG1GCstartup parameters. And CMScompared G1with compression, to avoid debris, G1pause times more controllable, the overall performance is good

What new things did object?

   JavaObject-oriented static strongly typed language, declare and create an object very common, according to a class declares a reference variable pointing to an object is created, and uses this reference variable to manipulate objects, a subject matter which it did in the new?

Bytecode angle:

1.NEW
  • If you can not find the Classobject class loading is performed
  • After loading is successful, the memory allocated on the heap, from Objectmust allocate memory to the beginning of all the properties on the local path
  • Memory allocation is completed, the initialization value of 0
    • References occupy storage space, which is a variable value, 4 bytes
      • It will be performed to an instance object reference variables onto a virtual machine stack stack
2.DUP
  • Copy the reference variable top of the stack, when the stack has two instances of objects within a point stack reference variables.
    • If the method has parameters, the parameters also need to stack push operation. Two different object reference variable, which is pressed into the bottom reference for the assignment,
    • Or saved to the local variable table, another reference variable as a stack handles call related methods.
3.INVOKESPECIAL:
  • Call the object instance, call the method through the stack of reference variables. The class initialization method performed, the initialization method performed but the object.

Perform step angle:

1. Confirm classifier information exists:
  • When JVMreceiving the newinstruction, the first metaspaceclass checks need to create the meta information exists

    • If not, then in parents entrust faction mode, using the current class loader to ClassLoader+ package name + class name Keyto find the corresponding .classfile
      • If none is found, throwClassNotFoundException
      • If found, the class loader is performed, and generate a corresponding Classobject.
2. allocated objects:
  • Calculate the object's memory footprint
    • If the instance variable is a reference member variable, distribution space can only reference variables, i.e., the size of 4 bytes, followed by a division of memory in the heap to a new object
    • When allocating memory space, the need for synchronous operation, such as the use CASfailure retry, etc. to ensure the locking region of the dispensing operation atoms
3. Set the default values:

  Member variable values ​​need to set the default bit value, that various forms of value 0

4. Set the object header:

  Set a new object hash code, the GC information, lock information, meta object belongs to class information and the like. The setting of this process depends on JVMimplementation.

The init method performed:

  Member variable initialization, execution of the example code block, the method calls the constructor of the class, and the assignment of an object within a stack address to the first reference variable.

Guess you like

Origin juejin.im/post/5dfccc5a6fb9a01616436517