Jump to content

  • Log In with Google      Sign In   
  • Create Account


Java Graphics.drawImage seems to leak memory..?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Rich76   Members   -  Reputation: 178

Like
0Likes
Like

Posted 27 June 2012 - 04:54 PM

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

Edited by Rich76, 27 June 2012 - 04:58 PM.


Sponsor:

#2 Rich76   Members   -  Reputation: 178

Like
0Likes
Like

Posted 27 June 2012 - 05:31 PM

I read this regarding a C# application:

The problem is that to do interpolation with scaling GDI+ needs to create a
copy of the bitmap. This is why I recommend effectively turning these
features off with the settings above so that hopefully the code in the GDI+

library bypasses these routines and calls a bitblt for you.


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


Now to figure out how this is done in Java..

#3 Rich76   Members   -  Reputation: 178

Like
0Likes
Like

Posted 27 June 2012 - 05:45 PM

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]

#4 Rich76   Members   -  Reputation: 178

Like
0Likes
Like

Posted 27 June 2012 - 05:55 PM

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..

#5 mhagain   Crossbones+   -  Reputation: 7468

Like
1Likes
Like

Posted 28 June 2012 - 03:28 AM

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.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#6 ChristerSwahn   Members   -  Reputation: 198

Like
1Likes
Like

Posted 28 June 2012 - 11:49 AM

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).

Edited by ChristerSwahn, 28 June 2012 - 11:49 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS