I noticed that my application is leaking memory. This can be seen in DDMS, and I managed to get a OutOfMemoryError.
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.
Tomasz, you are on the right track. There is NO bug in DDMS and there is NO memory leak in your program.
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.
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.
I kept investigating and what I've found is really suprising. It seems there is no real memory leak. It happens only when app is in debugging mode in DDMS.
DDMS seems to somehow hold references to those finished treads, preventing them from being GC-ed. When i disconnect the phone and connect again, I can see that all "leaked" resources have been released.
It looks like a bug in DDMS.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With