I have a memory leak using ListActivity in Android

后端 未结 2 1784
面向向阳花
面向向阳花 2020-12-11 06:36

I have an application that uses a Service and some list activities. When the activities are opened, I can see the heap usage increase in DDMS, when the activities are closed

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-11 07:30

    If it repeatedly (10-15 times) open the activity then close the activity, the heap size (both MB and # Objects) balloons!

    Have you run the garbage collector? There's a button for that in DDMS.

    Once you have run the garbage collector and it seems to be coming up with nothing to collect (as logged in LogCat), you can always dump the heap and inspect it with MAT or jhat.

    Am I using ListActivity incorrectly?

    Delete all occurrences of getApplicationContext() in your activity and replace them with this. The activity is a Context, and you want to be using the Activity in those places.

    Similarly, I don't know why you are calling getBaseContext() in the Service, rather than just using this, since Service too is a Context.

提交回复
热议问题