被完成后Thread对象当作垃圾回收对象、垃圾、完成后、Thread

2023-09-05 02:50:17 作者:你要的专一我学不会ˇ

我注意到,我的应用程序正在泄漏内存。这可以看出,在DDMS,我可以设法得到一个OutOfMemoryError异常。

I noticed that my application is leaking memory. This can be seen in DDMS, and I can managed to get a OutOfMemoryError.

我发现泄漏源。活动之一具有在后台运行的线程。该线程停止的onDestroy()。它结束运行,因为它可以在DDMS可见。

I found the source of the leak. One of the activities has a thread running in the background. This thread is stopped in onDestroy(). It finishes running, as it can be seen in DDMS.

现在,如果线程开始时,发生泄漏,活动不是垃圾被破坏后收集的,因为它是由线程引用。 如果线程一点都没有启动,一切正常。

Now, if thread is started, the leak occurs, Activity is not garbage collected after being destroyed, because it is referenced by the thread. If thread is not started at all, everything is ok.

下面是简单的例子证明这一点:

Here's simple example demonstrating this:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    volatile boolean finished = false;
    byte[] memoryEater = new byte[4 * 1024 * 1024];

    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            while (!finished) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            Log.d(getClass().getName(), "Thread finished");
        }
    });

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finished = true;
    }

    public void startActivity(View view) {
        startActivity(new Intent(this, MainActivity.class));
    }

    public void startThread(View view) {
        thread.start();
    }
}

添加一个按钮用于启动新的活动,一个是启动一个线程。开始新的活动。回去之后,存储器将被清理仅当线程尚未启动

Add one button for starting new activity and one for starting a thread. Start new activity. After going back, the memory will be cleaned only if thread has not been started.

什么是这种现象的原因是什么?

What is the cause of this behaviour?

推荐答案

我刚刚想出了这个相同的问题。

I have just figured out this same problem.

托马斯,你是在正确的轨道。有一个在DDMS无错误并且在你的程序中没有内存泄漏。

Tomasz, you are on the right track. There is NO bug in DDMS and there is NO memory leak in your program.

真正的问题是,你正在运行的程序在调试模式(在Eclipse中)。不知怎的,当机器人在调试模式下运行,线程是不是垃圾的run()方法已经退出,即使收集。我想这可能是Android的需要持有的线程进行一些调试功能进行了介绍。

The really problem is you are running your program in DEBUG mode (under Eclipse). Somehow when Android is running in DEBUG mode, Threads are not garbage collected even after the run() method has been exited. I guess it is probably Android needs to hold on to the Thread for some debugging features to work.

但是,如果你运行你在运行模式下(仍在Eclipse的)应用程序,主题垃圾收集发生。该线程将被释放完全,你的活动将被完全释放。

But if you run you application in RUN mode (still under Eclipse), Thread garbage collection takes place. The Thread will be freed completely and your Activity will be freed completely.

 
精彩推荐
图片推荐