It is quite feasible. For example, you have this pseudo-code:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new DownloadTask().execute(); } public void showInfo() { } class DownloadTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... params) { return null; } @Override protected void onPostExecute(Void data) {
The code above will cause a memory leak.
Here is an explanation:
When you create a DownloadTask as shown above, the java call DownloadTask is an inner class . The inner class implicitly contains a reference to the outer class, in this case MainActivity . Moreover, when you run asintasku, this asynthesis will be held by the system until it is completed. For example, loading takes 30 seconds. Within 30 seconds you rotate your device. When the device is rotated, the MainActivity re-created , and often the old activity will be destroyed. But in this case, the old activity is not destroyed, because the old MainActivity instance is held by DownloadTask and DownloadTask is executed by the system. You will skip one instance of the action.
To fix this, you should change the code above:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new DownloadTask(this).execute(); } public void showInfo() { } } class DownloadTask extends AsyncTask<Void, Void, Void> { WeakReference<MainActivity> mainActivityWeakReference; public DownloadTask(MainActivity activity) { mainActivityWeakReference = new WeakReference<MainActivity>(activity); } @Override protected Void doInBackground(Void... params) { return null; } @Override protected void onPostExecute(Void data) { if (mainActivityWeakReference.get() != null) { mainActivityWeakReference.get().showInfo(); } } }
In this case, when a new MainActivity is created, the old one is not held by DownloadTask (due to a weak reference attribute), so the old one will be destroyed by the Android Garbage Collector in the future. You should also check every time you use a weak reference object, because you do not know exactly when the GC will destroy this object.
Here is my own blog about another memory leak situation. Memory leak when using a static inner class
I hope for this help.