views:

441

answers:

2

I'm porting a game to Android (there's a lot of code and very little of it is mine), and DalvikVM is telling me (through LogCat) all about the garbage collection. At some point in the code, I get a stream of "GC freed x objects / x ms" messages, basically informing me that ~150,000 objects have just been deleted and it's taking a full second.

I want to know where these came from! I am pretty sure I'm not creating that many objects intentionally.

So, is there a way to get... basically the opposite of that message? Something that prints a log message when any object is created?

That way I could step over the code, checking how many messages are generated, and seeing which parts of code are generating the objects. I suspect some form of object creation in part of a loop, but if possible this would be an easy way to tell for sure.

I'm using Eclipse 3.4.2, if that's relevant.

Any ideas?

+2  A: 

Why don't you profile the existing game code on the Sun JVM (assuming you're porting a Java game)? Then you can take advantage of JProfiler, Yourkit, etc. and see what huge set of objects are being cleared up. (Use -XX:+PrintGCDetails to see the GC runs for when to look for it.)

If it turns out to not be in the game, you've pretty painlessly figured that out, and can then turn your attention to your Android code. And unfortunately that I don't know much about.

Kai
+3  A: 

This is possible using the ddms tool that comes with Android (not the Eclipse version). Using that, look at the allocation tracker tab. You can start tracking allocations of all objects along with stack traces of where they were allocated. However, this tool can produce a -lot- of information and the particular information you want is not always easy to parse or find. If you have a version for the Sun JVM, I would recommend using the tools Kai mentioned, they are a lot more developed. If you have to do it in Android, using the allocation tracker will give you a start.

Soonil