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 SoftRefrence
to 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.2
After use PhantomRefrence
to 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.2
with weakRefrence
expressed.
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 Roots
between 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 Roots
departure, 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 Roots
starting 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-Copy
algorithm
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 Eden
and two smaller recorded as inactive, then remove the original meta-object space. Heap memory space is divided into a larger Eden
and two smaller Survivor
, use a time Eden
and Survivor
a region. In this case Mark-Copy
reducing waste memory space.
Mark-Copy
Now as a mainstream YGC
garbage collection new generation of algorithms.
Tell me what you know about the garbage collector?
- evening
Seral
Recycling is a major for YGC
the garbage collector, serial single-threaded way to complete GC
the task, which Stop The World
referred to STW
that garbage collection pauses at some stage throughout the application execution. FGC
A relatively long time, often FGC
will seriously affect the performance of the application
- CMS
algorithm:
Mark - sweep algorithm, a large amount of space debris
solution:
-XX: + UseCMSCommpactAtFullCollection = n
Parameters, at n times FGC
after JVM
another in the implementation of the old generation space defragmentation, perform a space defragmentation, but the defragmentation phase space will lead to STW
.
step:
- Initial labels (Initital Mark)
- Concurrent mark (Concurrent Mark)
- Relabeled (ReMark)
- Clear concurrent (Concurrent Mark)
problem:
. A l, 3 : initiator STW
Solution:
In order to reduce STW
the number of times, GMS
you can also configure the -XX: + CMSFullGCsBeforeCompaction = n parameter, after n times FGC, JVM
then 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 G1
garbage collection, through -XX: + UseG1GC
startup parameters. And CMS
compared G1
with compression, to avoid debris, G1
pause times more controllable, the overall performance is good
What new things did object?
Java
Object-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
Class
object class loading is performed - After loading is successful, the memory allocated on the heap, from
Object
must 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
- References occupy storage space, which is a variable value, 4 bytes
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
JVM
receiving thenew
instruction, the firstmetaspace
class 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 nameKey
to find the corresponding.class
file- If none is found, throw
ClassNotFoundException
- If found, the class loader is performed, and generate a corresponding
Class
object.
- If none is found, throw
- If not, then in parents entrust faction mode, using the current class loader to
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
CAS
failure 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 JVM
implementation.
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.