Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

[Reposting this question from stackoverflow, as it was pointed out that it fits better here.]

I'm currently porting my 2D game engine to Java. I looked at some of the game libraries pointed at here on stackoverflow. However, the ones I looked at were rather simplistic and didn't even state if they supported things like alpha transparency, so I decided to port my C++ renderer for which I had already written out the logic.

This renderer is a pure software renderer which uses tiling to avoid needless re-rendering. I optimized its scrolling performance by creating an "offscreen buffer" a little larger than my output pane, and blitting this offscreen buffer onto my output on every frame. This way, I could avoid redrawing tiles needlessly just because I scrolled a pixel on the map.

I used Java's AWT to implement it, using a large BufferedImage for the offscreen buffer. The CPU usage is okay(around twice of what I had in C++), but there's an odd problem with continuous scrolling, where every second or so, the renderer will lag out for around 0.2 seconds.

Since there's nothing in my own code that would occur in these periods, and since the spikes disappear if I don't draw my offscreen buffer onto the main view, I can only conclude that Java is doing some internal optimization of its own. However, I'm not sure what it does, nor do I know which of my own optimizations I would have to remove to get rid of the spikes. Also, it might be that java AWT wasn't made with continuous, high FPS scrolling in mind, and that's entirely unusable for this purpose.

Is there some way for me to get rid of these spikes?

share|improve this question
3  
Could it be the garbage collector hitting you? –  bummzack Aug 30 '11 at 19:20
    
@bummzack: Possibly. In the profiler it looks like this: i.imgur.com/EMxkA.png However, I'm not sure how I would reduce this effect, especially if it's caused by my calls to graphics.drawImage –  cib Aug 30 '11 at 20:25
add comment

1 Answer

While I cannot be sure without looking at your code, it sounds like your problem is the garbage collector. In Java, you have major and minor garbage collections taking place every now and then. The minor use up some of your cpu but wont bug you too much. The major collections can be a real problem for real time apps such as games since they will actually pause everything while they are running.

There are two options to solve this. First you can tweak the JVM to make sure less major collections take place. Secondly (and recommended), you can make sure you do not leave too much garbage. Simply check where in your app you create a lot of objects(in my games those are usually the vector3 classes) and make sure you reuse them as much as possible (especially in inner loops etc).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.