Activity内部Handler引起内存泄露的原因分析

有时在Activity中使用Handler时会提示一个内存泄漏的警告,代码通常如下:

{private TextView tvHelloWorld;private Button btnSetText;private Handler mHandler = new Handler();(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnSetText = (Button) findViewById(R.id.btn_set_text);tvHelloWorld = (TextView) findViewById(R.id.tv_hello_world);btnSetText.setOnClickListener(new OnClickListener() {(View v) {mHandler.post(new Runnable() {() {tvHelloWorld.setText(“Runnable”);}});}});}}

Lint Warning原文如下: This Handler class should be static or leaks might occur (com.example.testhandler.MainActivity.1)

Issue: Ensures that Handler classes do not hold on to a reference to an outer class Id: HandlerLeak

Since this Handler is declared as an inner class, it may prevent the outer class from being garbage collected. If the Handler is using a Looper or MessageQueue for a thread other than the main thread, then there is no issue. If the Handler is using the Looper or MessageQueue of the main thread, you need to fix your Handler declaration, as follows: Declare the Handler as a static class; In the outer class, instantiate a WeakReference to the outer class and pass this object to your Handler when you instantiate the Handler; Make all references to members of the outer class using the WeakReference object.

1. 内存泄露原因分析

由于这个Handler作为内部类声明在Activity内部,普通的内部类对象隐式地保存了一个指向外部类对象的引用,所以这个Handler对象保存了一个指向Activity对象的引用。而这个Handler对象的生命周期可能比Activity生命周期长,比如当有一个后台线程持有该Handler,别且该线程在执行一个长时间任务。所以当该Handler没有被JVM垃圾回收器回收时,它就阻止了它引用的外部类Activity对象的回收,这里就导致了内存泄露。

2. 如何解决这种内存泄露问题

在该内存泄露的Lint Warning中给出了解决该问题的方法。将Handler类声明为静态内部类,即解除内部类对象与其外部类对象之间的联系。创建一个外部类的WeakReference,并在实例化Handler对象时使用它。代码实现如下:

{private TextView tvHelloWorld;private Button btnSetText;private Handler mHandler = new InternalHandler(this);(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnSetText = (Button) findViewById(R.id.btn_set_text);tvHelloWorld = (TextView) findViewById(R.id.tv_hello_world);btnSetText.setOnClickListener(new OnClickListener() {(View v) {mHandler.post(new Runnable() {() {tvHelloWorld.setText(“Runnable”);}});}});}{private WeakReference<Activity> weakRefActivity;/*** A constructor that gets a weak reference to the enclosing class. We* do this to avoid memory leaks during Java Garbage Collection.*/public InternalHandler(Activity activity) {weakRefActivity = new WeakReference<Activity>(activity);}(Message msg) {Activity activity = weakRefActivity.get();if (activity != null) {}}}}3. SoftReference、WeakReference和PhantomReference

SoftReference、WeakReference和PhantomReference是java.lang.ref类库中的一组类。当垃圾回收器正在考察的对象只能通过某个Reference对象才“可获得”时,这3个类为垃圾回收器提供了不同级别的间接性提示。

对象是可获得的(reachable),,是指此对象可在程序中的某处找到。这意味着你在栈中有一个普通的“引用A”,而它正指向此“对象A”,也可能是“引用B”指向“对象B”,而“对象B”含有“引用C”指向“对象A”,也可能是更多的中间链接。如果一个对象是“可获得的”,垃圾回收器就不能释放它,因为它仍然为你的程序所用。如果一个对象不是“可获得的”,那么你的程序将无法使用到它,所以将其回收是安全的。

如果想继续持有某个对象的引用,想以后还能够访问到该对象,同时也想在内存消耗殆尽的时候垃圾回收器回收它,这时就应该使用Reference对象。

SoftReference、WeakReference和PhantomReference由强到弱排列,表示不同级别的“可获得性”。 SoftReference用以实现内存敏感的高速缓存。 WeakReference是为实现“规范映射”(canonicalizing mappings)而设计的,它不妨碍垃圾回收器回收映射的“键”(或“值”)。“规范映射”中对象的实例可以在程序的多处被同时使用,以节省存储空间。 PhantomReference用以调度回收前的清理工作,它比Java终止机制更加灵活。

我不但的回首,伫足,然后时光扔下我轰轰烈烈的向前奔去。

Activity内部Handler引起内存泄露的原因分析

相关文章:

你感兴趣的文章:

标签云: