Advertisement

[java] Double Buffering Performance

Started by November 13, 2000 05:50 PM
3 comments, last by letsch 24 years, 1 month ago
I have some problems with double buffering. It is all about performance. I have an Image as buffer. All I do is draw that Image onto the Graphics of the Panel I am using. The Image doesn''t change. So all in all it is a standard double buffer. There is nothing behind all that at the moment, so I really do NOTHING MORE than drawing that Image. public void paint(Graphics g) { int x = (this.getWidth()-bufferwidth)/2; int y = (this.getHeight()-bufferheight)/2; this.getGraphics().drawImage(image,x+offsetX,y+offsetY,canvas); } public void update(Graphics g) { paint(g); } The problem now: It isn''t fast if the Panel (and the Image) is 640x480. But... As soon as I make it 1600x1200 I have really big problems. I am not dumb, I know the difference between 0.3M and 1.92M pixels. The really strange thing is the memory usage. I usually have a memory usage of about 100MB. As soon as I start my application (I mean after it runs for some seconds...) the memory usage goes up to 300MB, the computer begins to swap and the gc has enough time to bring the memory usage down to 120MB again. (btw, I even tried to call the gc each time I update and the thread that calls repaint has low priority) So the question: Is there anything I can do against that performance problem with drawImage()? I would be very happy if anyone could help. Yours, Leonhard
I can''t really help you on the double buffering problem (not yet..) but you wrote that you try to call the gc each time you update.

You cannot force gc. Here''s what the "Java 2 Certification Studay Guide" says about this.

"Calling this method (.gc()) SUGGESTS that the Java Virtual Machine expends effort toward recycling unused objects."

You can get memory leaks if you allow live, accessible references to unneeded objects to persist in the program. This items can''t be gc''d. Therefore it may be a good idea to explicity assign null into a variable when you have finished with it.

Here two examples
WRONG
public Object pop() {
return storage[index--];
}

RIGHT
public Object pop () {
Object returnValue = storage[index]:
storage[index--] = null;
return returnValue;
}
Advertisement
Thank you for that answer. I know that I cannot force the gc. The problem is, that the only thing I do is drawing that Image. There is nothing I could set to null. My problem is that I don''t know how to make one single code line faster.

this.getGraphics().drawImage(image,x+offsetX,y+offsetY,canvas);

I am still hoping that I have a big mistake in my code or in the concept.

Thank you,
Leonhard
Hi again,

I took a look at your code again and the problem might be

int x = (this.getWidth()-bufferwidth)/2;
int y = (this.getHeight()-bufferheight)/2;

this.getWidth() and this.getHeight() take a little time to return their values. Normally no problem, but inside the paint method this can cause problems. paint(Graphics g) is updated very often.

I am not sure, but I think the paint method is waiting for getWidth() to return a value.

Maybe try
public class xxx
private int x,y,width,height;

public constructor () {
width = this.getWidth();
height = this.getHeight();
}

public void paint(Graphics g) {
x = (width -bufferwidth)/2;
y = (height-bufferheight)/2;
this.getGraphics().drawImage(image,x+offsetX,y+offsetY,canvas);
}
public void update(Graphics g) {
paint(g);
}

I also read something about bitwise shifting which could replace /2. I dunno know if it makes the calculation faster, though.
Do the same thing with the graphics, store them in a member variable too.


public class xxx
private Graphics gra;
private int x,y,width,height;

public constructor () {
width = this.getWidth();
height = this.getHeight();
gra = getGraphics();
}


...

1600x1200 is a little ambitious for a java game in my opinion.

This topic is closed to new replies.

Advertisement