PrefaceIn Java, everything is considered an object, and references are used to manipulate objects. In JDK1.2, object references are divided into four levels, so that the program can control its life cycle more flexibly. The levels are as follows from high to low: strong > soft > weak > phantom reference. The GC garbage collector (Garbage Collection) has different processing methods for different types. Understanding these processing methods will help us write higher quality code. Today we will learn: 1. Detailed explanation of citation1. StrongReferenceStrong references are the most commonly used references. If an object has a strong reference, the garbage collector will never recycle it. When memory space is insufficient, the Java virtual machine would rather throw an OutOfMemoryError error and cause the program to terminate abnormally than solve the problem of insufficient memory by arbitrarily recycling objects with strong references. For example, in the code String s="abc", the variable s is a strong reference to the string object "abc". As long as you assign a null value to the strong reference object s, the object can be recycled by the garbage collector; because the object no longer contains other strong references at this time;
2. Weak referencesWeakReference is a reference type weaker than soft reference. It is similar to soft reference, but the difference is that weak reference cannot prevent garbage collection. When the garbage collection mechanism is running, if the reference of an object is a weak reference, the object will be recycled regardless of whether there is enough memory space. Weak reference is often used to prevent memory leaks, the most common of which are memory leaks caused by singletons and handlers;
3. Soft referencesSoftReference: Soft reference –> When the virtual machine is out of memory, the object it points to will be recycled; when you need to get the object, you can call the get method; Soft reference objects will not be reclaimed by the JVM quickly. The JVM will determine when to reclaim based on the current heap usage. It will only be reclaimed when the heap usage frequency approaches the threshold. Basic Usage
Basic Features
When the soft reference object is recycled, the ReferenceQueue queue stores the strongly referenced Reference, and then poll() can be used to determine whether there is an object in the current reference queue that has lost its soft reference. If the queue is empty, a null will be returned, otherwise the method returns the previous Reference object in the queue. You can detect which soft reference object is recycled and then clear it;
4. Phantom referencesPhantomReference is the weakest reference. An object holding a phantom reference is almost the same as an object without a reference and may be garbage collected at any time. References obtained through the get() method of a phantom reference will fail (become null). Phantom references must be used together with the reference queue ReferenceQueue; The ReferenceQueue reference queue is used to track the garbage collection process. When the garbage collector collects an object, if it finds that it still has a phantom reference, it will destroy the object after the collection and add the object pointed to by the phantom reference to the reference queue. The only way to determine whether a phantom reference is collected by GC is to determine whether it is added to the ReferenceQueue. This is also the only way to determine whether an object is recycled; There is a finalize() method in the Java Object class. The principle is: once the garbage collector is ready to release the memory space occupied by the object, it will first call the finalize() method, and the memory occupied by the object will not be truly reclaimed until the next garbage collection action occurs. However, the problem is that the virtual machine cannot guarantee when finalize() will be called because the GC running time is not fixed; This problem can be solved by using phantom references. Phantom references are mainly used to track garbage collection activities and are mainly used to achieve more sophisticated memory usage control, which is very meaningful for Android;
The reference obtained by phantomReference.get() is always null, calling the system to recycle garbage, queue.poll() obtains the saved reference object and removes it from this queue; Phantom references cannot obtain the target reference through the get() method, and always return null. Source code:
Summarize
This article is reproduced from the WeChat public account "Android Development Programming" |
>>: When is the cheapest time to buy a mobile phone? Insiders say
According to the recent number of newly confirmed ...
Mixed Health I don't understand the health tr...
In the early stages of entrepreneurship , from bu...
The summer vacation is long and the weather is ho...
A few days ago, many netizens said that they felt...
At 1 a.m., after a few hours of retaliatory brows...
Before doing product operations and promotion wit...
Hot topic titles have always been one of the more...
Under the influence of factors such as consumptio...
Mini programs provide convenience for publicity a...
As we all know, online activities, especially onl...
The day before yesterday, I shared an article: &q...
We want to use the Internet to create a new brand...
The term " user growth " has become ver...