当前位置:网站首页>Deep understanding of leakcanary

Deep understanding of leakcanary

2022-06-23 22:11:00 Great inventor

  1. Memory leak undefined The traditional memory leak is to forget to release memory manually , The phenomenon that makes the unreleased memory unusable .
  2. jvm Memory leak undefinedjvm A memory leak is a memory leak that we no longer need , Avoid the phenomenon of garbage collection .undefinedandroid Memory leaks in refer to Short - lived objects are held by long - lived objects , This leads to the failure of garbage collection .
  3. How to judge that an object is no longer used undefined Reference counting : The advantage is simplicity and efficiency , The disadvantage is that there is a problem of circular reference .undefined Accessibility analysis :
  4. When one Activity When it's recycled , Will execute finalize(). Can pass finalize() Execute judgment Activity Is it recycled .
  5. Manual gc Runtime.getRuntime().gc()

stay Java in , There are several objects that can be used as GC Root:undefined Java Virtual machine stack ( Local variable table ) Object referenced in .undefined Static references to objects in the method area .undefined Thread objects that are still alive .undefined Native In the method JNI Referenced object .

Specific process

  1. LeakCanary Registered a AndroidLifecyleCallback, In every one of them Activity After execution onDestory After the Activity To monitor . The core method of monitoring is RefWatcher.watch(). stay watch Method wraps the object to be observed , Created a WeakReference The object of .
  2. Pass the reference check task Looper.myQueue().addIdleHandler Add to MessageQueue in , Perform reference checking when the main thread is idle .
  3. The specific check operation is to check whether the return of the weak reference is null , And check the queues related to weak references , If the weak reference is recycled , Quote relevant WeakReference Will be added to the queue . Use this method to check for memory leaks .

Strong citation It won't be recycled by the garbage collector . Even if the current memory space is not enough ,JVM It won't be recycled , But throw OutOfMemoryError error

String str = "hello";    //  Strong citation 
str = null;              //  Cancel strong reference 

Soft citation When using soft references , If there's enough memory , Soft references can continue to be used , Instead of being recycled by a garbage collector ; Only when there is not enough memory , Soft references will be recycled by the garbage collector .

SoftReference<String> softName = new  SoftReference<>("haha");

Weak reference Objects with weak references have a shorter life cycle . Because when JVM Garbage collection , Once a weak reference object is found , Whether the current memory space is sufficient or not , Will recycle weak references . But because the garbage collector is a lower priority thread , So it's not always possible to find weak references quickly .

WeakReference<String> weakName = new WeakReference<String>("hello");
WeakReference(T referent, ReferenceQueue<? super T> q)

4. Virtual reference If an object only holds virtual references , So it's equivalent to not quoting , It can be collected by the garbage collector at any time . Its function is to judge whether an object has been properly garbage collected . The program can judge whether a virtual reference has been added to the reference queue , To see if the referenced object is going to be garbage collected .

ReferenceQueue<String> queue = new ReferenceQueue<String>();
PhantomReference<String> pr = new PhantomReference<String>(new String("hello"), queue);
原网站

版权声明
本文为[Great inventor]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/12/202112181145226779.html