开发者

Objects are still in memory after activity is destroyed

开发者 https://www.devze.com 2023-04-13 03:24 出处:网络
My activity keeps a certain number of references to preview images from the camera in a Map object. This takes a lot of memory. I\'m monitoring the memory usage using:

My activity keeps a certain number of references to preview images from the camera in a Map object. This takes a lot of memory. I'm monitoring the memory usage using:

    Runtime runtime = Runtime.getRuntime();
    long allocated = runtime.totalMemory();
    long free = runtime.freeMemory();
    long used = allocated  - free;

When I kill my app (back key), the activity is destroyed. When I start my app again, I can see that the memory was not cleared. The "used" figure starts at the same value and then goes up as new frames arrive.

When I look at the "dominator_tree" after I "Dump HPROF", I can see two instances of my activity, both taking a lot of memory.

When I override onDestroy() and I clear() the preview frames map object, this doesn't seem to happen.

I'm probably missing something here but I thought all memory should be deallocated after my activity is destroyed (after the garbage collection process), why do I have to manually clear() the map? Other objects I create don't seem to require this.

I tried开发者_如何学Go setting android:launchMode="singleInstance" but this doesn't seem to have any affect.

Thanks!


in order to understand what's happening, in the dominator tree, on your activity click with the right button, look for PATH to GC ROOT and select the option that exclude weak/soft/phantom references. That would give you an idea why the system could not destroy your activity (if you are leaking something or what else)


Garbage collector will run as needed, at an indeterminate time chosen by the runtime. GC may or may not run when your application exists.

However, if your objects still stay in memory after GC is run it means you are leaking memory. You can check the logs in logcat while debugging to see when it is run.

For a good tutorial how to detecting and fixing leaks check Android Memory Management Video from Google.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号