Java Graphics.drawImage seems to leak memory..?

Started by
4 comments, last by ChristerSwahn 11 years, 10 months ago
I am creating a simple isometric game. I am noticing Graphics.drawImage is increasing my memory use. Here is how I'm painting my graphics to a JPanel (which is within a JScrollPane):

[source lang="java"]public class PanelCanvas extends JPanel{

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

paintScene(g);
}

private void paintScene(Graphics g){
// Only screen area with buffer is drawn

g.drawImage(tile, plotX, plotY, FRAME_WIDTH, FRAME_HEIGHT, null);
}
}[/source]

I was thinking that my JPanel was not being cleared before the next paint, however super.paintComponent(g) should be taking care of that. When I comment out g.drawImage my memory use is in an expected range and doesn't increase, but when I uncomment the line, memory increases. Scrolling around the JPanel makes the memory go up faster, however, when not scrolling, my memory still increases, just not as much.

My game uses about 70,000K in memory, but with g.drawImage my usage can go up to 140,000K within a minute..

Any suggestions?


Thank you
Advertisement
I read this regarding a C# application:

[color=#4D528C][font=verdana, geneva, lucida,][background=rgb(228, 230, 245)]The problem is that to do interpolation with scaling GDI+ needs to create a[/background][/font]
[color=#4D528C][font=verdana, geneva, lucida,]copy of the bitmap. This is why I recommend effectively turning these[/font]
[color=#4D528C][font=verdana, geneva, lucida,]features off with the settings above so that hopefully the code in the GDI+[/font]

[color=#4D528C][font=verdana, geneva, lucida,][background=rgb(228, 230, 245)]library bypasses these routines and calls a bitblt for you.[/background][/font][/quote]

http://www.hightechtalks.com/dotnet-framework-drawing/graphics-drawimage-consuming-more-memory-182393.html


Now to figure out how this is done in Java..
Hmm, i set the following, but memory still increases sitting idle.

[source lang="java"]g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);[/source]
Erggg, I set this as well:

[source lang="java"] g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED);
g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);[/source]

But still memory increases, though much worse when scrolling..
Does memory continually increase while running or does it just increase over the first few seconds/minutes then settle down? If the latter you don't have a leak - you just have normal expected behaviour.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Java2D Graphics does some caching of the transformed and rendered images, e.g. if you transform/scale them, to increase the UI update speed. Perhaps it is this caching that you notice. Is the increased memory a problem for you? If you only want a "raw" rendering of an image, pixel-for-pixel, perhaps you should not use Java 2D (Graphics2, and JComponent decendents such as JPanel), and instead use e.g. Canvas ("heavy-weight" UI component in the java.awt package).

This topic is closed to new replies.

Advertisement