GC problem in 2D platform game?

Hi all,

I’m writting a 2D platform game and I think I have a memory issue. Here is the log of the game:

D:\game_dev>set VM_OPTIONS=-Xms32m -Xmx64m -Dsun.java2d.accthreshold=0 -verbose:gc -XX:+PrintGCTimeStamps

D:\game_dev>java -cp bin;lib/dom4j.jar;lib/gage_timer-1_0.jar;lib/jutils.jar;lib/jinput.jar;lib/dxinput.jar -Djava.library.path=lib -Xms32m -Xmx64m -D
sun.java2d.accthreshold=0 -verbose:gc -XX:+PrintGCTimeStamps bubblegame.BubbleGame
0.000: [GC 2111K->222K(32576K), 0.0215583 secs]
Running at 85 hertz
New Acceleration Threshold: 0
[11-Dec-2003 12:10:46 PM] INFO - loading map…
[11-Dec-2003 12:10:46 PM] INFO - Found Chunk - MPHD, reading…
[11-Dec-2003 12:10:46 PM] INFO - loading map header.
[11-Dec-2003 12:10:46 PM] INFO - FMP version : 1.0
[11-Dec-2003 12:10:46 PM] INFO - MapWidth : 300
[11-Dec-2003 12:10:46 PM] INFO - MapHeight : 80
[11-Dec-2003 12:10:46 PM] INFO - BlockWidth : 32
[11-Dec-2003 12:10:46 PM] INFO - BlockHeight : 32
[11-Dec-2003 12:10:46 PM] INFO - ColourDepth : 32
[11-Dec-2003 12:10:46 PM] INFO - BlockByteSize : 32
[11-Dec-2003 12:10:46 PM] INFO - NoOfBlocks : 1283
[11-Dec-2003 12:10:46 PM] INFO - NoOfImages : 1266
[11-Dec-2003 12:10:46 PM] INFO - Found Chunk - EDHD, skipping…
[11-Dec-2003 12:10:46 PM] INFO - Found Chunk - CMAP, reading…
[11-Dec-2003 12:10:46 PM] INFO - loading 256 entries.
[11-Dec-2003 12:10:46 PM] INFO - Found Chunk - BKDT, reading…
[11-Dec-2003 12:10:46 PM] INFO - loading 1283 blocks.
[11-Dec-2003 12:10:46 PM] INFO - Found Chunk - BGFX, reading…
[11-Dec-2003 12:10:46 PM] INFO - loading 1266 images.
4.512: [GC 2333K->1392K(32576K), 0.0330226 secs]
4.703: [GC 3502K->1938K(32576K), 0.0141814 secs]
4.902: [GC 4050K->2503K(32576K), 0.0148150 secs]
5.131: [GC 4615K->3724K(32576K), 0.0205685 secs]
5.382: [GC 5834K->4205K(32576K), 0.0115557 secs]
5.579: [GC 6315K->5444K(32576K), 0.0200933 secs]
5.769: [GC 7556K->5922K(32576K), 0.0114517 secs]
5.923: [GC 8034K->6419K(32576K), 0.0124315 secs]
6.127: [GC 8529K->7360K(32576K), 0.0159188 secs]
[11-Dec-2003 12:10:48 PM] INFO - Found Chunk - BODY, reading…
[11-Dec-2003 12:10:48 PM] INFO - Found Chunk - LYR1, reading…
[11-Dec-2003 12:10:48 PM] INFO - Map loaded succesfully.
7.127: [GC 9472K->8017K(32576K), 0.0169907 secs]
7.822: [GC 10118K->9432K(32576K), 0.0185303 secs]
8.126: [GC 11500K->10386K(32576K), 0.0125689 secs]
Number of buffers used: 3

Available accelerated memory (in bytes): 25898752
8.662: [GC 12498K->11158K(32576K), 0.0132754 secs]
Windows 2000
Ticks/Second: 1193182
Ticks/Frame: 14037
27.735: [GC 13270K->11214K(32576K), 0.0090187 secs]
72.364: [GC 13326K->11223K(32576K), 0.0037304 secs]
BufferStrategy was page flipping.

fps : 85.0
Game Duration: 112 seconds
110.193: [GC 13335K->12053K(32576K), 0.0188094 secs]

You can see in the 2 last bold lines that the GC frees about 2 megs of memory. This behavior will continue until the program terminates.

The strange thing about this is that my code doesn’t create that much objects to fill 2 megs of memory regularly. The only object I create is String to display the fps BUT only when the fps value changes and I’ve commented out these lines of code and there is still a lot of garbage. So is someone could help me to explain this undesired behavior?

The game runs with the resolution of 640x480x16 at 85 hertzs with 3 buffers to do page flipping.

You can see the VM options I pass to the VM. I use JDK 1.4.2_02, it runs on Win 2000 with a Pentium 3 866 and a GeForce TNT 2 32 megs.

Any idea?

I really don’t see what happens here.

Thanks

For some rendering operations Java2D has to create intermediate buffers. This happens in most cases of non-simple transforms (like rotation), some image format conversions.

You might want to use some profiling tool to find out what objects are being created. For example, you can use the built-in profiler (Xrunhprof:help). Make you app quit after a few frames and see what’s left on the heap.

Also, check out what primitives your app is using, with tracing: -Dsun.java2d.trace=help and see if there are some ‘generic’ loops that are being used, or mask blit operations.