Skip to main content

Questions about releasing off-screen image

4 replies [Last post]
sherikekele
Offline
Joined: 2010-05-06
Points: 0

Hi all,

Recently, I encounter a memory leak issue, it's caused by off-screen image. My test case is liking the following one:

Graphics canvas = getGraphics();
Image doublebuffer = createImage(960,540);
g = doublebuffer.getGraphics();
...//drawing operation
canvas.drawImage(doublebuffer, 0, 0, this);
g.dispose();
canvas.dispose();

As I know, I can call the dispose() method to release the Graphics resources allocated in the native code. But I cannot find a good way to release the Image related resources. So I refered to Graphics.java, and add finalize() and dispose() method to java.awt.Image, thus application can call Image.dispose() method just as Graphics.dispose() to release the image resources.

I'm not sure whether this is a proper method to fix the memory leak issue. Can anyone help me about this issue? Thanks in advance.

Best Wishes,
Sheri

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Hinkmond Wong

phonemeadvanced@mobileandembedded.org wrote:
> Hi Hinkmond,
>
> I didn't set doublebuffer as null after I finished image drawing. If I didn't do this, does that mean GC will not collect the doublebuffer and not be freed forever?
>

Yes, as long as you have an object pointer (variable) that holds a
reference to that image and it remains in scope or otherwise remains
existing as a variable, that image will not be garbage collected.

Hinkmond

---------------------------------------------------------------------
To unsubscribe, e-mail: advanced-unsubscribe@phoneme.dev.java.net
For additional commands, e-mail: advanced-help@phoneme.dev.java.net

Hinkmond Wong

phonemeadvanced@mobileandembedded.org wrote:
> Hi all,
>
> Recently, I encounter a memory leak issue, it's caused by off-screen image. My test case is liking the following one:
>
> Graphics canvas = getGraphics();
> Image doublebuffer = createImage(960,540);
> g = doublebuffer.getGraphics();
> ...//drawing operation
> canvas.drawImage(doublebuffer, 0, 0, this);
> g.dispose();
> canvas.dispose();
>
> As I know, I can call the dispose() method to release the Graphics resources allocated in the native code. But I cannot find a good way to release the Image related resources. So I refered to Graphics.java, and add finalize() and dispose() method to java.awt.Image, thus application can call Image.dispose() method just as Graphics.dispose() to release the image resources.
>
> I'm not sure whether this is a proper method to fix the memory leak issue. Can anyone help me about this issue? Thanks in advance.
>

Are you making sure to set doublebuffer = null (or to a new value)
whenever you are finished with the Image?

Hinkmond

---------------------------------------------------------------------
To unsubscribe, e-mail: advanced-unsubscribe@phoneme.dev.java.net
For additional commands, e-mail: advanced-help@phoneme.dev.java.net

sherikekele
Offline
Joined: 2010-05-06
Points: 0

Hi Hinkmond,

I didn't set doublebuffer as null after I finished image drawing. If I didn't do this, does that mean GC will not collect the doublebuffer and not be freed forever?

Back to my original question about freeing off-screen image. Off-screen image maybe very large, if I don't free it in the application, then the only way is to wait GC to free it. Then the framebuffer will be occupied, and maybe other application or the same application can not allocate another off-screen image. What I want is to find out a way that can free the off-screen image in application, like Graphics.dispose().

Best Wishes,
Sheri

bonito_qq
Offline
Joined: 2010-07-28
Points: 0

You dont have to set doubleBuffer to null if the scope of the function finishes there.
I think this is a long standing bug of the JVM implementaion( Cant get the link right now). The memory will be freed only if the doubleBuffer object is GCed. The only way for an application to trigger the GC is by System.gc(). Try this, though it isnt a very clean solution.2 continous System.gc() might be a good idea, since the finalize() of Image, causes the GC to need to cycles to free the object and its resources