apptentive / apptentive-android

Apptentive Android SDK
http://www.apptentive.com
BSD 3-Clause "New" or "Revised" License
65 stars 64 forks source link

Fix memory leak caused by passing activity context to new Thread #85

Closed solcott closed 10 years ago

solcott commented 10 years ago

Passing the Activity Context to new a Thread is causing a memory leak.

To reproduce the leak on ICS or higher add StrictMode.enableDefaults() to your Application or Activities onCreate. After starting the app rotate the device a few times. You we see something similar to
android.os.StrictMode$InstanceCountViolation: class Your com.example.YourActivity; instances=2; limit=1 in logcat. After every device rotation instances will increment. After a few device rotations you can do a heap dump and then import in into MAT.

Once you have imported the heap dump into MAT got the histogram and search for your activity. Select it and right click on it go to Merge Shortest Paths to GC Roots -> exclude all weak references and you will see something similar the following screen shot 2014-06-17 at 11 02 49 am

There should only be on instance in memory

skykelsey commented 10 years ago

Thanks Scott. I'll pull this in as soon as I can test it out.

skykelsey commented 10 years ago

Hi Scott,

I've pulled this into a feature branch and tested it. It will be included in the next release. Thanks!

skykelsey commented 10 years ago

This is in release 1.5.1.