Reputation: 1015
I have a memory allocation error while running an android app I am developing. This only happens when I rotate the screen repeatedly, each rotation resulting in a little more memory being leached until the app crashes.
I was kind of reluctant to make this post because the problem itself seems quite simple to solve (avoid static context references and static resource references, I think) but in the last 10 hours I've had no luck.
I've had a look at some heap dump files with MAT but have no idea what it all means, I'm a first time user of that.
I haven't posted any code as it's partially sensitive and I'm not sure which parts of the code to post so I will give a brief outline of it here.
The activity causing the problem instantiates a sound manager class but this class is instantiated within another activity also and causes no problems there. The UI is all created in XML and consists of 25 Button elements with the same 9patch background, 3 TextViews and a Coach with 5 images within.
The MAT says that suspect problem 1 is android.widget.FrameLayout and suspect problem 2 is byte []
Any help would be insanely appreciated and if any more information is required I'll go ahead and edit to include it.
Thank you so much, Tim.
After some more trial and error (commenting out every bit of code and introducing each part bit by bit) I learned that my problem was caused by this line, which related to Google AdMob interstitial ads:
interstitial.loadAd(adRequest);
changing the following line solved the issue:
interstitial = new InterstitialAd(this);
to
interstitial = new InterstitialAd(getApplicationContext());
Upvotes: 0
Views: 496
Reputation: 13761
This answer won't give you a definitive solution, not because I'm not willing, but because it's impossible (and even harder without not just viewing your code, but knowing it very well). But from my experience I can tell you that those kind of memory leaks doesn't occur just due to directly referenced objects - objects you declare (and keep referencing another classes/objects) in turn depends on many other classes and so on, and probably you're seeing a memory leak due to an incorrect handling of any of your instances which at the same time reference to other instances.
Debugging memory leaks is a often a very hard work, not just because as I said above it sometimes doesn't depend directly on what you've declared, but also because finding a solution might not be trivial. The best thing you can do is what you already seem to be doing: DDMS + HPROF. I don't know how much knowledge you have, but although it's not a universal method, this link helped me so much to find memory leaks in my code.
Although it seems trivial, the best way to debug those kind of things is progresively remove portions of your code (overall, those which implies working with instances of other classes) and see how the HPROF report change.
By the way, keep in mind that on each screen rotation, unless specifically configured the opposite, will call your onCreate()
method each time, so I presume there's some instance created in that method that is not being freed on each screen rotation.
Upvotes: 1