简体   繁体   English

Android内存泄漏使用匿名类实现和特定于线程的局部变量

[英]Android Memory Leak Using anonymous class implementation with local variables specific to thread

In my application I want to do specific task in a separate thread.I want to know where it will cause any memory leak. 在我的应用程序中,我想在一个单独的线程中执行特定任务。我想知道它将导致任何内存泄漏的位置。

public class MainActivity extends Activity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        runOnThread();
      }

      private void runOnThread() {
        new Thread() {
        private int value;
          @Override
          public void run() {
            while (true) {
              SystemClock.sleep(1000);
              callotherfunction(value);
            }
          }
        }.start();
      }
    }

So does this implementation cause Memory leak.And on more thing is that Having local variables in thread is it harmful? 那么这种实现是否会导致内存泄漏呢?更重要的是,线程中具有局部变量是否有害? Will local variables in threads create memory leak. 线程中的局部变量会导致内存泄漏。

I do not know what the problem is in your code, but I invite you to use the AsynTask if you need to do operations on other threads/background. 我不知道您的代码出了什么问题,但是如果您需要在其他线程/后台执行操作,我邀请您使用AsynTask

Doc: https://developer.android.com/reference/android/os/AsyncTask.html Doc: https : //developer.android.com/reference/android/os/AsyncTask.html

Example in which a dialog is also opened which closes at the end of operations: 还打开一个对话框的示例,该对话框在操作结束时关闭:

In your class where have to do operation in background: 在您的班级中,必须在后台进行操作:

/** Create variable */
private Dialog dialog;
private LoadAsyn loadAsync;

/** Call the AsyncTask */
dialog = ProgressDialog.show(getActivity(), "", "Caricamento...", false, true);
dialog.setCancelable(false);
loadAsync = new LoadAsyn();
loadAsync.execute((Void) null);

This is the class where is defined the AsynTask operation: 这是在其中定义AsynTask操作的类:

/** Define AsyncTask Class */
public class LoadAsyn extends AsyncTask<Void, Void, Boolean> {
    @Override
    protected void onPreExecute(){}

    @Override
    protected void onProgressUpdate(Void[] values) {};

    @Override
    protected Boolean doInBackground(Void... params) {

        //In this block all operation to do in background

        return true;

    }

    @Override
    protected void onPostExecute(final Boolean success) {
        dialog.dismiss();
        //Operation to do after execution
    }

    @Override
    protected void onCancelled() {
        loadCompAsync = null;
    }
}
}

The variables will take up memory as long as your thread is running. 只要线程正在运行,这些变量将占用内存。 If your thread is never stopped and deallocated, then it will continue to take up memory. 如果您的线程从未停止过并被释放,那么它将继续占用内存。

You are fine as long as you have some exit condition that will make the thread stop. 只要您具有使线程停止的退出条件,就可以了。 When thread is stopped, it will be garbage collected if you have no references to it. 当线程停止时,如果您没有对它的引用,它将被垃圾回收。

Even so explicitly creating threads is not the recommended way to run background tasks in android. 即使如此显式创建线程也不是在android中运行后台任务的推荐方法。 Use AsyncTask instead, or take a look at RxJava. 请改用AsyncTask,或看看RxJava。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM