Java Learning Note 10

Source: Internet
Author: User

When a program creates a reference-type entity such as an object, an array, and so on, the system allocates a chunk of memory in the heap memory, and the object is stored in the memory area.


When the object that we create is no longer referenced, the memory becomes garbage, and finally waits for the garbage collection mechanism to be recycled, and the Java garbage collector


System has the following characteristics:


1, garbage collection mechanism is only responsible for recovering objects in the heap memory, does not reclaim any physical resources, such as database connections, network IO and other resources.


2, the program can not accurately control the operation of garbage collection, garbage collection will be at the appropriate time, when the object permanently lost the reference, the system will be in


Reclaim the memory it occupies when it is appropriate.


3. Before the garbage collection mechanism reclaims any objects, it will always call its finalize () method, which allows a reference variable to refer to the object again, from


This causes the garbage collection mechanism to cancel the recycle.


When an object is running in heap memory, it can be divided into the following three states, depending on the state it refers to in the referenced variable:


1, up to the state: When an object is created, if more than one reference variable refers to it, then the object in the program can be reached, the program can


Call the field and method of the object by referencing the variable.


2. Recoverable state: If an object in the program no longer has any reference variable referencing it, it enters the recoverable state, in which case the system


Garbage collection mechanism is ready to reclaim the memory that the object occupies, and the system invokes the Finalize () method of all recoverable state objects before the object is reclaimed


for resource cleanup. If the system re-enables a reference variable to refer to the object when it calls the Finalize () method, the object will again become reachable


Otherwise, the object will enter the unreachable state.


3. Unreachable state: When an object's association with all reference variables is severed, and the system has called the Finalize () method of all objects, it still does not make the


When an object becomes unreachable, the object will permanently lose its reference and eventually become unreachable, and when an object is in an unreachable state, the system


The system will actually reclaim the memory that the object occupies.


For most objects, there is a reference variable referencing the object in the program, and the most common way to refer to it is in addition to the JAVA.LANG.REF package.


3 classes are available: SoftReference, phantomreference, and WeakReference, respectively, representing the system's 3 references to the object


Type: Soft references, virtual references, and weak references, Java references to objects are in the following 4 ways:


1, strong reference: This is the most common method of reference in Java programs, the program creates an object, and assigns the object to a reference variable, the program passes


The reference variable to manipulate the actual object. When an object is referenced by one or more reference variables, it is in a state of reach and cannot be tied


garbage collection mechanism.


2, soft reference: Soft references need to be implemented through the SoftReference class, when an object has only soft references, it may be garbage collection mechanism back


For a soft reference object, when the system memory space is sufficient, it will not be reclaimed by the system, the program can also use the object, when the system memory


When there is not enough space, the system may reclaim it. Soft references are typically used in memory-sensitive programs.


3. Weak reference: Weak citation is implemented through the WeakReference class, weak references and soft references are similar, but weak references have lower reference levels, for only weak


Referenced object, but the system garbage collection mechanism runs, regardless of whether the system memory is sufficient, will always reclaim the memory occupied by the object. Of course, and


This is not to say that when an object has only a weak reference, it is immediately recycled.


4. Virtual reference: The virtual citation is implemented by the Phantomreference class, and the virtual reference is exactly similar to no reference. Virtual references do not have too much impact on the object itself


The object does not even feel the existence of a virtual reference. If an object has only one virtual reference, it is roughly the same as a non-referenced effect. Virtual Primer


The virtual reference cannot be used alone, and the virtual reference must be combined with the reference queue (Referencequeue) in order to be used primarily to track the state of the object being garbage collected


Use.


The reference object is represented by the Java.lang.ref.ReferenceQueue class, which is used to hold a reference to the object that was recycled when the union uses soft references, weak references, and


When a queue is referenced, the system adds a reference to the associated reference queue after the referenced object is reclaimed. With soft references and weak


By contrast, a virtual reference adds a virtual reference to its object to its associated reference queue before the object is disposed, which allows the object to be


Take action before recycling.


Soft and weak references can be used alone, but virtual references cannot be used alone, and using virtual references alone does not make much sense, and the primary role of virtual references is


The tracking object is garbage collected, and the program can understand if the virtual reference is already included in the reference queue associated with the virtual reference


Whether the referenced object is about to be reclaimed.



Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/43890287 Emotional Control _  

Java Learning Note 10

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.