当前位置:网站首页>Deep understanding of leakcanary
Deep understanding of leakcanary
2022-06-23 22:11:00 【Great inventor】
- Memory leak undefined The traditional memory leak is to forget to release memory manually , The phenomenon that makes the unreleased memory unusable .
- 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 .
- 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 :
- When one Activity When it's recycled , Will execute finalize(). Can pass finalize() Execute judgment Activity Is it recycled .
- 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
- 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 .
- Pass the reference check task Looper.myQueue().addIdleHandler Add to MessageQueue in , Perform reference checking when the main thread is idle .
- 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);边栏推荐
- Icml2022 | robust task representation for off-line meta reinforcement learning based on contrastive learning
- Kubernetes cluster lossless upgrade practice
- What causes the applet SSL certificate to expire? How to solve the problem when the applet SSL certificate expires?
- 北大、加州伯克利大學等聯合| Domain-Adaptive Text Classification with Structured Knowledge from Unlabeled Data(基於未標記數據的結構化知識的領域自適應文本分類)
- The most common usage scenarios for redis
- Knowda: all in one knowledge mixture model for data augmentation in feed shot NLP
- HDLBits-&gt; Circuits-&gt; Arithmetic Circuitd-&gt; 3-bit binary adder
- Redis function discarding Encyclopedia redis:: delete() is deprecated solution
- Don't let your server run naked -- security configuration after purchasing a new server (Basics)
- Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
猜你喜欢

实验五 模块、包和库

Ten thousand words! Understand the inheritedwidget local refresh mechanism

Leetcode must review six lintcode (28348455116385)

北大、加州伯克利大学等联合| Domain-Adaptive Text Classification with Structured Knowledge from Unlabeled Data(基于未标记数据的结构化知识的领域自适应文本分类)

微信小程序中发送网络请求

Peking University, University of California Berkeley and others jointly | domain adaptive text classification with structured knowledge from unlabeled data (Domain Adaptive Text Classification Based o

Selenium批量查询运动员技术等级

Data visualization: summer without watermelon is not summer

Using the provider to transform the shit like code, the amount of code is reduced by 2/3!

ICML2022 | 基于对比学习的离线元强化学习的鲁棒任务表示
随机推荐
Cloud native practice of meituan cluster scheduling system
Notepad++ installing the jsonview plug-in
Data visualization: summer without watermelon is not summer
ICML2022 | 基于对比学习的离线元强化学习的鲁棒任务表示
Improve efficiency, take you to batch generate 100 ID photos with QR code
Tencent cloud database tdsql elite challenge Q & A (real-time update)
Ffmpeg for audio and video commands
How does the hybrid cloud realize the IP sec VPN cloud networking dedicated line to realize the interworking between the active and standby intranet?
Acl2022 | MVR: multi view document representation for open domain retrieval
How to calculate individual income tax? You know what?
How to solve the loss of video source during easynvr split screen switching?
Using the provider to transform the shit like code, the amount of code is reduced by 2/3!
How to improve the content quality of short video, these four elements must be achieved
MySQL de duplication query only keeps one latest record
Tencent cloud server ubuntu18 installs MySQL and logs in remotely
After CVM is configured with IPv6, it cannot be accessed as IPv6 or cannot access IPv6 sites
Knowda: all in one knowledge mixture model for data augmentation in feed shot NLP
Kubernetes cluster lossless upgrade practice
Detailed explanation of logical structure, physical structure and data operation
实验五 模块、包和库