Java Study Notes 10, java Study Notes
When a program creates objects, arrays, and other reference objects, the system allocates a memory area for them in the heap memory, and the objects are stored in the memory area,
When the object we create is no longer referenced, the memory will become garbage, and finally wait for the garbage collection mechanism to be recycled. the Java garbage collection Machine
System has the following features:
1. The garbage collection mechanism only recycles objects in the heap memory and does not recycle any physical resources, such as database connections and network I/O resources.
2. The program cannot accurately control the running of garbage collection. Garbage collection will be performed at the right time. When the object is permanently out of reference, the system will
Reclaim the memory it occupies when appropriate.
3. Before the garbage collection mechanism recycles any object, it always calls its finalize () method, which allows a reference variable to re-reference the object.
As a result, the garbage collection mechanism is canceled.
When an object runs in heap memory, it can be divided into the following three States based on the state referenced by the referenced variable:
1. reachable state: After an object is created, if more than one referenced variable references it, the object is in reachable state in the program. The program can
Call the Field and method of the object by referencing variables.
2. Recoverable state: if an object in the program no longer has any reference variable to reference it, it enters the recoverable State. In this state, the system
The garbage collection mechanism of is ready to recycle the memory occupied by this object. Before the object is recycled, the system will call the finalize () method of all recoverable objects.
Clear resources. If the system re-allows a reference variable to reference this object when calling the finalize () method, the object will become reachable again.
Otherwise, the object enters the inaccessibility status.
3. inaccessibility: when the object is disconnected from all referenced variables and the system has called the finalize () method of all objects, this
The object becomes reachable, so this object will permanently lose reference and finally become inaccessibility. when an object is in an inaccessibility state
The system will actually recycle the memory occupied by this object.
For most objects, there is a reference variable in the program to reference this object. In this case, the most common reference method is as follows:
Three classes are provided: SoftReference, PhantomReference, and WeakReference, which represent the three references of the system to the object.
Type: soft reference, virtual reference, and weak reference. Java references objects in the following four ways:
1. Strong reference: This is the most common reference method in Java programs. The program creates an object and assigns this object to a reference variable.
This reference variable is used to operate the actual object. When an object is referenced by one or more referenced variables, it is reachable and cannot be tied.
Garbage collection mechanism.
2. Soft reference: soft reference must be implemented through the SoftReference class. When an object has only soft reference, it may be returned by the garbage collection mechanism.
For soft-referenced objects, when the system memory space is sufficient, it will not be recycled by the system, the program can also use this object, when the system memory
When the space is insufficient, the system may recycle it. Soft references are usually used in memory-sensitive programs.
3. Weak references: weak references are implemented through the WeakReference class. Weak references and soft references are similar, but weak references have lower reference levels
But when the system garbage collection mechanism is running, the memory occupied by the object will be recycled regardless of whether the system memory is sufficient. Of course, and
It does not mean that an object will be recycled immediately when it only has a weak reference.
4. Virtual references: Virtual references are implemented using the PhantomReference class during virtual references. Virtual references are similar to those without references. The Virtual Reference does not have much impact on the object itself.
Objects do not even feel the existence of virtual references. If an object has only one virtual reference, the effect of the object is roughly the same as that of the object that is not referenced. False reference
It is mainly used to track the state of objects being recycled by garbage collection. Virtual references cannot be used independently. Virtual references must be used together with the reference Queue (ReferenceQueue ).
.
The referenced object is represented by the java. lang. ref. ReferenceQueue class. It is used to save the reference of the recycled object. When soft reference, weak reference, and
When you reference a queue, after the system recycles the referenced object, it adds the reference corresponding to the recycled object to the associated reference queue. With soft references and weak
The difference between referencing is that, before an object is released, the Virtual Reference of its object is added to its associated reference queue, which allows the object to be
Take action before recycling.
Soft references and weak references can be used independently, but virtual references cannot be used independently. It does not make much sense to use virtual references independently. The main function of virtual references is
The tracking object is in the garbage collection status. The program can check whether the virtual reference is included in the reference queue associated with the Virtual Reference to understand the virtual
Whether the referenced object is about to be recycled.
Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/43890287 sentiment control _