Four types of Java referencesIn Java, there are four different reference types: strong reference, soft reference, weak reference and phantom reference. (1) Strong Reference is the most common type of reference. It refers to a reference relationship that is commonly found in a program. As long as a strong reference exists, the garbage collector will not recycle the referenced object. For example: In the above code, obj is a strong reference pointing to a newly created Object object. As long as obj exists, the garbage collector will not reclaim the object. (2) Soft reference is a reference type that is weaker than strong reference. Before the system is about to overflow memory, the object pointed to by soft reference will be recycled as much as possible. For example: In the above code, softRef is a soft reference pointing to a newly created Object object. When the system is low on memory, the garbage collector may reclaim the object. (3) Weak Reference is a reference type that is weaker than Soft Reference. When the garbage collector is working, as long as a weak reference is found, the referenced object will be recycled regardless of whether the system memory is sufficient. For example: In the above code, weakRef is a weak reference pointing to a newly created Object object. When the garbage collector finds that the object has only a weak reference, it will recycle it. (4) Phantom Reference is the weakest reference type, which has almost no reference function. Phantom Reference is mainly used to track the state of objects being garbage collected. It is impossible to obtain the instance of the object through Phantom Reference. For example: In the above code, phantomRef is a virtual reference pointing to a newly created Object object. When the garbage collector is ready to recycle the object, it will add the virtual reference to the queue. Use scenarios:
Introduction to LeakCanaryLeakCanary is an open source library for detecting memory leaks in Android applications. It can help developers find and solve memory leaks in a timely manner during application operation, and improve application stability and performance. Using LeakCanary is very simple. You only need to add the dependency to the build.gradle file of the application and initialize it in the Application class. LeakCanary will automatically monitor the memory leaks of the application and send a notification when a memory leak is detected. LeakCanary works by monitoring the object reference relationships in the application. When an object is created, LeakCanary will track its reference chain. If it finds that the object is not released correctly, it will trigger the detection and reporting of memory leaks. LeakCanary will generate a detailed memory leak report, including the reference chain of the leaking object and related context information, to help developers quickly locate and fix memory leaks. Its workflow is as follows:
LeakCanary helps developers to promptly detect and resolve memory leaks in Android apps through monitoring, analysis and notifications, which helps improve the performance and stability of apps. LeakCanary usage(1) Add the LeakCanary dependency to the project's build.gradle file: (2) Initialize LeakCanary in the onCreate() method of the Application class: (3) Run the app and perform some operations in the app so that LeakCanary can detect memory leaks. (4) When LeakCanary detects a memory leak, it displays a notification in the notification bar. Click the notification to view detailed memory leak information, including the reference chain of the leaking object and the location where the leak occurred. By using LeakCanary, developers can promptly detect and resolve memory leaks in their applications, improving application performance and stability. |
<<: Exploration and practice of intelligent film performance optimization
>>: Compose-Multiplatform Practice on Android and iOS
I believe that when we surf the Internet, we will...
With the end of the May Day holiday, everything i...
People usually do not age at a uniform rate, and ...
Recently, Qianjinteng suddenly became popular bec...
Organic food, also known as ecological or biologi...
On May 31, the Moments were suddenly flooded with...
No chicken soup or theory. 20 tips Taobao skill 1...
What exactly is advertising, trying to convince y...
Through Train is not only a promotional tool that...
Recently I saw an interesting topic: "Why do...
By this time, final exams from elementary school ...
When it comes to promotion, I guess the first ste...
If you only "talk about brand " but not...
I wonder if you still remember the "lotus bi...
Many veterans should know what project Feng Chao ...