JVM's garbage collection and memory allocation strategy

I. Overview

  Design garbage collector, consider the following three things:

  (1) which need to be recovered memory

  (2) when to recycle

  (3) how to recycle

  Java memory runtime respective partial region are: program counter, Java Virtual Machine stack, native method stacks, Java heap, the method area in which the program counter, the virtual machine stack, native method stacks three regions with threads born, with threads death, to enter and exit the stack frame of the stack with the process while the strip is performed without flocculation and stack pop operations. Each stack frame how much memory is allocated in the class structure substantially known per finalized. Memory allocation and recovery of these areas are equipped with certainty.

  Java heap and method area of ​​the two regions has a significant uncertainties: a class that implements multiple interfaces of memory needed may be different, different branches of a method performed the required memory may not be the same, only in during operation, the program can we know exactly what objects are created, how many objects are created, this part of the memory allocation and recovery is dynamic, how the garbage collector is concerned it is this part of the memory management.

Second, determine what needs to recover memory?

(1) counting state determination reference object (dead or alive)

  Reference counting live objects is determined: adding a reference count in a subject, a place whenever it is referenced, the counter value is incremented by 1 when referring to the failure, the counter value is decremented by one; any time counter object is not 0 It may again be used. Reference counting though they take some extra memory space to be counted, but it is simple in principle, determine high efficiency. But it can not solve the problem of circular references.

  A = B, B = A, case A, B reference each other, they cause the reference count is not 0, the reference count method thereof will not be recovered.

If (2) reachability analysis algorithm to determine the object alive?

  The basic idea of ​​this algorithm through a series of root object called "GC Roots" as a starting node set, start searching downward from these nodes according to the reference relationship, the search process is taking the path known as the "chain of references", if a the object is not connected to any reference GC Roots chain, then the object is no longer referenced.

  GC Roots collection are:

  1) target virtual machine stack referenced, how a local variable, temporary variable.

  2) the method the object attribute references the static area, such as a class variable.

  3) The method of the object referenced in the constant region, such as a quoted string constant pool.

  4) inside the virtual machine references, such as the Class object corresponding to the basic data types.

  5) All objects are held by the synchronization lock (synchronized).

  Note: Different user selected garbage collector and the current recovery of the memory area, so an area where the object is entirely possible to be referenced by other regions of the heap objects, then these objects need to be associated region is also added to the GC Roots in.

(3) type of reference

  1) Strong Application: Similar to Object obj = new Object (), such references relationship regardless of any circumstances, the garbage collector will never recover lost objects such referenced.

  2) Soft Quote: Some helpful, but not required objects. Objects are only soft references associated in system memory overflow will occur before exceptions, these objects will recover within the scope of the column into a second recovery, if this recovery has not enough memory, memory overflow exception will be thrown .

  3) weak references: the weak reference object can only be associated to survive until the next garbage collection occurs. When the garbage collector to work, regardless of the adequacy of current memory will only recover lost objects are associated with weak references.

  4) the imaginary reference: Unable to get a virtual object instance by reference, is provided for the sole purpose of a phantom reference associated with the object system just to receive a notification when the object is recovered collector. Listening to recover important objects; can be judged by the virtual reference frequency gc and if it is too large, memory usage may be a problem was a result of the system gc frequent calls.

  

Guess you like

Origin www.cnblogs.com/hdc520/p/12540063.html