Skip to main content

Memory problem

4 replies [Last post]
ajmmm
Offline
Joined: 2008-07-04

Hello!

Can I ask your help, please?

I'm using the method below to get a picture from a page in an application. Method drawPage() draws page contents over the background, if it exists.

For some reason, each time this method is called, used RAM grows about 3MB. When this value reaches nearly 100MB, the application just stops, without any exception thrown.

<br />
public BufferedImage getPageImage(int finalw,int finalh)<br />
  {<br />
  int w=1024;<br />
  int h=768;<br />
  try {<br />
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();<br />
    GraphicsDevice gs = ge.getDefaultScreenDevice();<br />
    GraphicsConfiguration gc = gs.getDefaultConfiguration();<br />
    BufferedImage image = gc.createCompatibleImage(w,h, Transparency.OPAQUE);<br />
    Graphics2D g2d = (Graphics2D)image.createGraphics();<br />
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);<br />
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);<br />
    g2d.setColor(getBgColor());<br />
    g2d.fillRect(0,0,w,h);</p>
<p>    if(getBgImageFName()!=null)<br />
      {<br />
      try{<br />
        BufferedImage bgimage=ImageIO.read(new File(bgImageFName));<br />
        g2d.drawImage(bgimage,0,0,w,h,null);<br />
        }catch(Exception e){e.printStackTrace();}<br />
      }<br />
    drawPage(g2d);<br />
    g2d.dispose();<br />
    return getScaledInstance(image,finalw,finalh,RenderingHints.VALUE_INTERPOLATION_BILINEAR);<br />
    }<br />
    catch(Exception de)<br />
      {<br />
      de.printStackTrace(CLogger.pw);<br />
      }<br />
   return null;<br />
   }</p>
<p>

What can i be doing wrong?

Thank you.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mgoe
Offline
Joined: 2007-10-02

With image.createGraphics() you are creating a Graphics which has to be disposed in order to release any system resources that it is using. For exception safety this should be done in a try-finally block.

demonduck
Offline
Joined: 2008-03-14

You are making three separate BufferedImages and throwing away the reference to two of them. So most likely, those two zombie instances of BufferedImages that are created each time you call getPageImage() are gradually eating memory. You can't depend on the default garbage collection to clean up memory.

So I recommend setting image and bgimage to null and then explicitly calling System.gc() IN getPageImage() before you exit.

Try that and see what happens. If it does nothing, maybe the memory leak is somewhere else.

walterln
Offline
Joined: 2007-04-17

Try loading the image only once.

ajmmm
Offline
Joined: 2008-07-04

Thank you for your help, WalterIn.

I partially solved the problem, invoking image.flush() in every code that calls this function. I mean partially, because the effect is much reduced but not 100% supressed.
Moreover, after reading many texts about BufferedImage.flush(), I could not be sure if it is really needed, and in which cases it is...

In summary, I'm not convinced at all...