当前位置:网站首页>How to effectively avoid memory leakage when customizing the handler?
How to effectively avoid memory leakage when customizing the handler?
2022-07-24 16:33:00 【Chen Xiaoxiang who wants to fly】
How to avoid customization handler Memory leak
Hello !
A term is used to explain
Inner class ( Click the jump ):
https://blog.csdn.net/chaseDreamer_/article/details/102859467
brief introduction
stay Android In the system ,Handler It is one of the core components of a message sending and processing mechanism , Other major components are Looper and Message,MessageQueue.
Message and Runnable Class is the carrier of message .MessageQueue It is the queue of message waiting .Looper Is responsible for getting messages from the queue .
effect
- Scheduling (scheule) Messages and executable runnable, It can be executed immediately or at a certain time in the future
- Let a certain behavior (action) Execute on other threads
About leakage
Handler It is a common way of asynchronous message processing provided by the system , Generally, it will not cause memory leakage , As for why it may lead to memory leakage , The memory leak here often refers to a leak Acitivity And so on
public class HandlerTestActivity extends Activity{
public Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
What's wrong with this writing ? The problem is that Handler The instance of adopts the writing method of inner class , It is HandlerTestActivity The inner class of this instance , stay Java in , There is a feature about inner classes : stay java in , Both non static inner classes and anonymous inner classes implicitly hold a reference to an outer class , As a result, external classes cannot be recycled by the system after use , This causes memory leaks .
Components with a shorter life cycle refer to components with a longer life cycle .Handler Is a typical example , Take the above code for example .HandlerTestActivity May be leaked , That is, the component is useless , For example, call finish() after , The garbage collector has been slow to recycle this Activity. The reason lies in the handler The inner class references it , And the handler Instances may be MessageQueue Quote . In fact, under normal circumstances, there will be no memory leakage , Unless handler Queue waiting too long , Want to solve handler Cause memory leaks , You need to understand the cause of the leak ,(handler hold activity, then message hold handler, then MessgQuene hold message), Understand this holding chain , that handler hold activity It can be solved by weak references or static inner classes , and removeCallbacksAndMessages Is to clear the following references , In fact, if your handler There are no time-consuming operations , Mission accomplished handler Will release Activity The reference of will not cause memory leakage ; Or your handler There is a 2 Second operation , stay Activity Exited 2s after , The release of the Activity References to , This is a short-term memory leak , It will also release , So you can ignore , But if yours handler If it's a dead cycle or something , It will lead to memory leakage .
From the above statement , We can think about the corresponding solutions :
1. Guarantee Activity By finish() The message queue of this thread does not have this Activity Of handler References to inner classes . This scene is extremely common , because handler It is often used to send delayed messages . One remedy is when this kind needs to be recycled , Manually empty the messages in the message queue :mHandler.removeCallbacksAndMessages(null);
2. Or let this handler Don't hold Activity And other external component instances , Let the Handler Become a static inner class .( Static inner classes do not hold instances of outer classes , Therefore, the external instance method cannot be called )
3. stay 2 Based on the method , In order to call external instance methods , Pass in an external weak reference )
4. take Handler Put it into a separate top-level class file .
Here we need to know about Java The knowledge quoted inside :
- Strong citation (Strong Reference) The default reference . If an object has a strong reference , The garbage collector will never recycle it . Empty in memory
When time is short ,Java The virtual machine would rather throw OutOfMemory Error of , Causes the program to terminate abnormally , It will not strongly reference objects to solve the problem of insufficient memory . - Soft citation (SoftReference) If there's enough memory , The garbage collector won't recycle it , If there's not enough memory , It will reclaim the memory of these objects .
- Weak reference (WeakReference) Once the garbage collector finds an object with only weak references , Whether the current memory space is enough or not , Will reclaim its memory .
- Virtual reference (PhantomReference) If an object only holds virtual references , Then it's the same as without any reference , Can be recycled at any time .
The third kind of , Need some extra code , More general .
public class HandlerTestActivity extends Activity {
private static class MyHandler extends Handler {
private final WeakReference<HandlerTestActivity> mActivity;
public MyHandler(HandlerTestActivity activity) {
mActivity = new WeakReference<HandlerTestActivity>(activity);
}
@Override
public void handleMessage(Message msg) {
ShanActivity activity = mActivity.get();
if (activity != null) {
//do Something
}
}
}
The fourth way , Extract and package separately .
/** * Implement callback weak reference Handler * Prevent memory leakage caused by internal holding * Incoming Callback Variables implemented anonymously cannot be used , Must be used with this Handle Object lifecycle one * Otherwise it will be released immediately */
public class WeakRefHandler extends Handler {
private WeakReference<Callback> mWeakReference;
public WeakRefHandler(Callback callback) {
mWeakReference = new WeakReference<Handler.Callback>(callback);
}
public WeakRefHandler(Callback callback, Looper looper) {
super(looper);
mWeakReference = new WeakReference<Handler.Callback>(callback);
}
@Override
public void handleMessage(Message msg) {
if (mWeakReference != null && mWeakReference.get() != null) {
Callback callback = mWeakReference.get();
callback.handleMessage(msg);
}
}
}
Because it is a weak reference , When this class needs to be recycled , Can be recycled directly .
WeakRefHandler The use of is as follows :
private Handler.Callback mCallback = new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
switch(msg.what){
}
return true;
}
};
private Handler mHandler = new WeakRefHandler(mCallback);
I was dreaming , Thank you for watching my blog , If there is any mistake , Please feel free to contact me ,QQ:897589417
边栏推荐
- Quickly view the version of redis in the server
- ArcGIS create vector
- MODIS data WGet Download
- About SQL data query statements
- EF LINQ Miscellany
- [machine learning basics] - another perspective to explain SVM
- Wentai technology's revenue in the first quarter soared by 184.6% year-on-year, and its net profit soared by 256.21%!
- Software - prerequisite software
- How to deal with the start and end times in mybatics
- Getting started with ARP
猜你喜欢

Custom view - Custom button

剑指 Offer 22. 链表中倒数第k个节点

多线程(基础)
[email protected]"/>ZBar source code analysis - img_ scanner. c | [email protected]

EMQ Yingyun technology was listed on the 2022 "cutting edge 100" list of Chinese entrepreneurs

如何防止跨站点脚本 (XSS) 攻击完整指南

Envi5.3 open GF-1 WFV data

Duplicate content in lookup table

ArcGIS create vector

TCP协议调试工具TcpEngine V1.3.0使用教程
随机推荐
2019q1 global smartphone shipments: Huawei vivo increased significantly, while Apple plummeted 30.2%!
[machine learning basics] - another perspective to explain SVM
EMQ Yingyun technology was listed on the 2022 "cutting edge 100" list of Chinese entrepreneurs
Codeworks round 693 (Div. 3) C. long jumps problem solution
我们为什么要推出Getaverse?
EF LINQ Miscellany
Envi5.3 open GF-1 WFV data
Win10 download address
EF data migration
普林斯顿微积分读本02第一章--函数的复合、奇偶函数、函数图像
ZBar source code analysis - img_ scanner. c | [email protected]
安全的证券公司有哪些?我想在手机上买股票
Software recommendation - website construction
[LeetCode]38.报数——题解(执行用时击败91% ,内存消耗击败 97%)
TCP protocol debugging tool tcpengine v1.3.0 tutorial
EF miscellaneous
Jupyter uses tips
Leetcode:162. looking for peak [two points looking for peak]
Replace the image source of Debian full version with Alibaba cloud image source
With regard to performance testing, dry goods hit "suggestions collection"