Couple of questions regarding threads and objects

Greets folks.

I’m trying to work out some issues in my animation loop. I can post code if desired; however, I’m not really sure the code is helpful just yet.

I need to figure out a way to determine a) how to know if objects are being created, b) how to know WHERE objects are being created, and c) how to eliminate object creation in my animation thread.

That said, let me give you the psuedo code.

Most of my graphics are stored as an Image in an Image[] so that I can access them by index. I have precalculated transitions on my ground terrain, so the procedure for drawing transition tiles is straight lookup. My loop looks like:

drawTerrain()
drawTransitions()
drawBackgrounds() // stuff like rocks, etc.
drawForegrounds() // trees, and characters

I am not sure why, but there is a significant hesitation every once in a while. I am almost positive it is related to garbage collection, but I have no idea what garbage I am creating that would need to be collected!

Reference information: this is running in Java 1.3.1, on a JPanel that is 512x512. I maintain a primitive array of two BufferedImages that are used as offscreen buffers so that I can start drawing on the next one as the first one is drawn to the screen. I did this because at one point I was not sure that the drawing loop would complete in the time it takes to draw the buffer to the screen. The BufferedImages are optimized as per other threads on this board to minimize the impact of transparency on them; I believe they are RGB BufferedImages with no alpha. The terrains are RGB, the transitions and backgrounds and foregrounds and chars are all ARGB.

Any help is most graciously appreciated.

There’s a number of profiler tools which could help you in determinig the hot spots in your app.

You can start with the built-in heap profiler (java -Xrunhprof:help)
or use one of the known profiler tools on the market (like OptimizeIt). The idea is to get a baseline before a step in your app, make a step (like render one frame), and then check out the newly created objects.

It’s hard to say what’s wrong with your app, but 1.3.1 (1.4 to a lesser extent) is known to create intermediate image buffers for some opertations - for example, when you transform an image, or if there’s no optimized loop for the particular image format you’re using.

I don’t know of tools that answer the questions easily :frowning:

[quote]a) how to know if objects are being created
[/quote]
I don’t know quite how to do this live. JSwat may work. With it you can set a breakpoint for “when a class is prepared” which I think is when a new object is created. I dunno, try it.

[quote]b) how to know WHERE objects are being created
[/quote]
HPmeter has a “object created by method” view

Make sure you are not using Java2D features since at least shear, rotate and and translate(?) create huge amounts of garbage.

Thanks for that. Pretty sure I’m not using those features, but I don’t know if something may be occurring under the hood during the animation loop.