将处理程序方法泄漏内存里面呢? [英] Will handler inside a method leak memory?

查看:117
本文介绍了将处理程序方法泄漏内存里面呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,因为它持有其外部类的引用在类声明的处理可能会泄漏内存。在这种情况下,我们应该使用静态嵌套类弱引用。

但如果什么处理程序的方法内部声明。我面临以下情况并不能确定它是一个正确的实现。可能有人请解释或者给我一个提示?我甚至不知道我应该寻找。

 私人无效了methodA(){
    处理程序处理程序=新的处理程序();
    handler.postDelayed(新的Runnable(){
        @覆盖
            公共无效的run(){
            的methodB();
        }
    },10 * 1000);私人无效的methodB(){
    // TextView中拥有一个活动的引用
    textView.setText(你好);
}


解决方案

它可以在一定条件下。如果传递的可运行的是一个匿名或内部类,因为在你的榜样,它拥有被垃圾收集'这'和prevents'这'的隐式引用,直到可运行被处理掉队列(因此​​,如果你的方法从来没有运行,就像如果你的处理器线程被不清除队列停止时,它会泄漏)。

在那里你担心了内存泄漏发生或挂到物体上太长的条件的话,那么你需要让你的可运行具有弱引用在构造函数初始化,就像一个静态类:

 私有静态MyRunnable实现Runnable
{
    私人最终的WeakReference< MyClass的> myClass_weakRef;    公共MyRunnable(MyClass的myClassInstance)
    {
        myClass_weakRef =新的WeakReference(myClassInstance);
    }    @覆盖
    公共无效的run()
    {
        MyClass的MyClass的= myClass_weakRef.get();
        如果(MyClass的!= NULL)
            myClass.methodB();
    }
}私人无效治法()
{
    处理程序处理程序=新的处理程序();
    handler.postDelayed(新MyRunnable(本),10 * 1000);
}

I know handler declared in a class may leak memory since it holds a reference to its outer class. In this case, we should use static nested class with weak reference.

But What if a handler is declared inside a method. I faced below case and not sure is it a correct implementation. Could someone please explain or give me a hint? I even don't know what I should search for.

private void methodA(){
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {            
        @Override
            public void run() {
            methodB();
        }
    }, 10*1000);

private void methodB(){
    //textView holds a reference to a activity
    textView.setText("hello");
}

解决方案

It can under certain conditions. If the runnable passed is an anonymous or inner class, as in your example, it holds an implicit reference to 'this' and prevents 'this' from being garbage collected until the runnable is processed off the queue (so if your method never runs, like if your handler thread gets stopped without clearing the queue, it will leak).

In the case where you are worried about the conditions for a memory leak occurring or hanging onto objects too long, then you need to make your runnable a static class that has a weak reference initialized in the constructor, something like:

private static MyRunnable implements Runnable
{
    private final WeakReference<MyClass> myClass_weakRef;

    public MyRunnable(MyClass myClassInstance)
    {
        myClass_weakRef = new WeakReference(myClassInstance);
    }

    @Override
    public void run()
    {
        MyClass myClass = myClass_weakRef.get();
        if(myClass != null)
            myClass.methodB();
    }
}

private void MethodA()
{
    Handler handler = new Handler();
    handler.postDelayed(new MyRunnable(this), 10*1000);
}

这篇关于将处理程序方法泄漏内存里面呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆