Note that there are some explanatory texts on larger screens.

plurals
  1. POThread objects not garbage collected after being finished
    primarykey
    data
    text
    <p>I noticed that my application is leaking memory. This can be seen in DDMS, and I managed to get a OutOfMemoryError.</p> <p>I found the source of the leak. One of the activities has a thread running in the background. This thread is stopped in <code>onDestroy()</code>. It finishes running, as it can be seen in DDMS.</p> <p>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.</p> <p>Here's simple example demonstrating this:</p> <pre><code>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(); } } </code></pre> <p>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.</p> <p>What is the cause of this behaviour?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload