简体   繁体   中英

See what is getting garbage collected

I am developing and optimizing an Android application.

The Eclipse Memory Analyzer Tool (MAT) is great to see what objects aren't being garbage collected, and to patch up those memory leaks but what I want to know is the opposite:

How can I see what objects ARE being garbage collected? I'm not interested in what is being retained.

Why would I want to do that? Well, I am running something in an infinite loop in a background thread. Stuff happens very fast, and garbage collection is being called often. I don't see any memory leaks, but perhaps because a lot of stuff is happening, Android decides that a few hundred commands have been called... now would be a good time to run the garbage collection!

It could be collecting something normal like some Strings or ints that have gone out of scope, for example.

For my goals, I would like to try to prevent as much garbage collection lag as possible by reducing the number of objects I'm creating, but knowing what gets collected so often would be useful. I can then try to prevent the often collected objects from being created in the first place, or retain these objects so that they can be reused without being recreated often.

This isn't possible because the JVM generally does not know (or care) what objects are removed during GC.

In fact, GC doesn't really "collect" the garbage. It actually works in the opposite way, determining which objects are live by following references, starting from the heap roots (eg local references owned by threads).

Only objects encountered during this heap walk are retained. Anything that isn't seen during the walk is ignored and not copied/compacted. These leftovers are the garbage that is effectively "collected", but the JVM does not know or care what those objects are.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM