0

While coming directly to home activity, I am passing intent flag FLAG_ACTIVITY_CLEAR_TOP, still I can see reference of other activities in hprof record.

In hprof report, I can see most of memory leak is due to following: android.media.AudioManager. or SpellCheckListener on editText

Please help me to resolve this memory leak. Clear top should have finished all the activities.

If clear top finishes the activities then from where audiomanager or spellchecklistener is coming into picture. In my code, I am not using anywhere audiomanager or spellchecklistener.

Abhishek Jain
  • 6,296
  • 7
  • 26
  • 34
  • 1
    The memory leak could be anything, without further details no answer can be given. Watch the entire clip to get a clue on how to find your leak: http://www.youtube.com/watch?v=_CruQY55HOk – Friesgaard Jan 21 '14 at 09:22
  • 1
    Did you force garbage collection before the memory dump? You will otherwise see things that are not yet collected although they will once there is need for memory. Clear top is also more or less irrelevant. Your `Activity` must be finished (which they usually are once you leave them) and your code must not hold references. – zapl Jan 21 '14 at 09:23
  • @zapl If it is coming in hprof, then it means reference still there. Please correct me if I am wrong. – Abhishek Jain Jan 21 '14 at 09:25
  • 1
    You record the current state with that. Including objects that are not yet colleted. I.e. you are wrong if you assume that hprof will only see referenced / uncollectable objects. SpellChecker is probably here because you show an `EditText` somewhere and `AudioManager` does the click sounds on e.g. buttons. – zapl Jan 21 '14 at 09:27

3 Answers3

0
Intent intObj=new Intent(context, MainActivity.class);
            intObj.putExtra("finish", true); // if you are checking for this in your other Activities
            intObj.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
                            Intent.FLAG_ACTIVITY_CLEAR_TASK |
                            Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intObj);
learner
  • 3,092
  • 2
  • 21
  • 33
0

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 very well your code). 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, that's why you're surprised seeing a class that you didn't even declare.

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.

nKn
  • 13,691
  • 9
  • 45
  • 62
0

AudioManager leaks might have various reasons. A working solution for me was to use the workaround that can be found on github.

Further discussion is available here: Android Context Memory Leak ListView due to AudioManager

Community
  • 1
  • 1
Sebastian Roth
  • 11,344
  • 14
  • 61
  • 110