即使 onPostExecute 完成并阻止活动被 GC 处理,AsyncTask 也不会停止运行

发布于 2024-12-01 04:50:14 字数 1367 浏览 0 评论 0原文

我的活动执行大量网络 I/O,因此我启动一个 AsyncTask(因为您无法在 UI 线程上执行网络 I/O)来完成此任务。我的问题是,当我调用 finish() 退出应用程序时,该应用程序永远不会被垃圾收集并继续在后台运行。我假设(可能是错误的)该任务永远不会被 GC 处理,因为 AsyncTasks 仍在运行,正如 Eclipse 调试窗口所示。我已经阅读了我能找到的所有内容,这似乎是一个没有解决方案的问题。

以下是详细信息:

在 OnCreate 中,我创建了一个 MessageHandler,如下所示:

mMessageHandler = new Handler()
{
@Override
public synchronized void handleMessage(Message msg)
{
  switch( (int)msg.arg1)
  {
         case kMessageDoSomething:
           doSomething();
           break;
                          .
                          .

我为每个 AsyncTask 都有一个类,例如在 myAsyncTask.java 中

public class myAsyncTask extends AsyncTask<Void, Object, Object>
{
    @Override
protected Object doInBackground(Void... params)
{
         doSomeNetworkIO();
         return null;
    }

@Override
protected void onPostExecute(Object result)
{
    super.onPostExecute( result );
    msg = new Message();
    msg.arg1 = MyActivity.getInstance().kMessageDoSomething;
    MyActivity.getInstance().mMessageHandler.sendMessage( msg );
    }

,然后回到 myActivity 中的

new myAsyncTask().execute();

doSomthing() 方法中,我检查 myAsyncTask 的 AsyncTask.Status ,它已完成,但调试器仍然显示任务正在运行,并且当我调用 finish() 时,myActivity 永远不会终止,除非我使用 AppKiller 手动终止它。

我真正的问题是,我是否在追逐红鲱鱼?也就是说,正在运行的线程的存在是否会阻止活动被垃圾收集?

My activity does a lot of network I/O, so I start an AsyncTask (since you can not do network I/O on the UI thread) to accomplish this. My problem is that the when I call finish() to exit my application, the app is never garbage collected and continues to run in the background. I am assuming (perhaps incorrectly) that the task is never GC'd because the AsyncTasks are still running as the Eclipse debug window shows me. I have read everything I can find on this and it appears to be a problem without a solution.

Here are the details:

in OnCreate I create a MessageHandler as follows:

mMessageHandler = new Handler()
{
@Override
public synchronized void handleMessage(Message msg)
{
  switch( (int)msg.arg1)
  {
         case kMessageDoSomething:
           doSomething();
           break;
                          .
                          .

I have a class for each AsyncTask, for example in myAsyncTask.java

public class myAsyncTask extends AsyncTask<Void, Object, Object>
{
    @Override
protected Object doInBackground(Void... params)
{
         doSomeNetworkIO();
         return null;
    }

@Override
protected void onPostExecute(Object result)
{
    super.onPostExecute( result );
    msg = new Message();
    msg.arg1 = MyActivity.getInstance().kMessageDoSomething;
    MyActivity.getInstance().mMessageHandler.sendMessage( msg );
    }

and back in myActivity

new myAsyncTask().execute();

in the method doSomthing() I check the AsyncTask.Status of myAsyncTask and it is FINISHED, yet the debugger still shows the task as running and when I call finish() myActivity never terminates unless I use AppKiller to kill it manually.

My real question here is, am I chasing a red herring? that is does the presence of running threads stop the activity from being garbage collected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霓裳挽歌倾城醉 2024-12-08 04:50:14

AsyncTask 有一个线程池,因此即使您的工作完成,线程也不会终止,这可能就是您在调试器中看到的情况。该池由 finalize() 关闭,因此在对象被 GC 处理之前它不会关闭。您可能想要做的是确保您的 AsyncTask 不保存对活动的引用,反之亦然。您可以在 onDestroy() 上分离它们。

请记住,无论 GC 如何,如果 Android 内存不足,它只会终止未使用的 Activity 的进程,因此内存最终会被回收。

The AsyncTask has a thread pool, so even if your work is finished threads are not terminated, that is probably what you are seeing in the debugger. The pool is shut down by finalize(), so it doesn't shut down until the object is GC-ed. What you might want to do is make sure your AsyncTask doesn't hold a reference to the activity and vice versa. You can detach them onDestroy().

Keep in mind that regardless of GC, if Android is low on memory, it will just terminate the process of your unused activity, so memory will be eventually reclaimed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文