Garbage Collector

M

Martijn Mulder

I have code like this:

java.awt.image.BufferedImage bufferedimage=
java.awt.GraphicsEnvironment.
getLocalGraphicsEnvironment().
getDefaultScreenDevice().
getDefaultConfiguration().
createCompatibleImage(100,100);

//use bufferedimage,
//then use the same code
//to create another BufferedImage

bufferedimage=
java.awt.GraphicsEnvironment.
getLocalGraphicsEnvironment().
getDefaultScreenDevice().
getDefaultConfiguration().
createCompatibleImage(200,200);


Will the original BufferedImage (size 100x100)
be garbage collected now? I never used new().
 
T

Thomas Fritsch

Martijn said:
I have code like this:

java.awt.image.BufferedImage bufferedimage=
java.awt.GraphicsEnvironment.
getLocalGraphicsEnvironment().
getDefaultScreenDevice().
getDefaultConfiguration().
createCompatibleImage(100,100);

//use bufferedimage,
//then use the same code
//to create another BufferedImage

bufferedimage=
java.awt.GraphicsEnvironment.
getLocalGraphicsEnvironment().
getDefaultScreenDevice().
getDefaultConfiguration().
createCompatibleImage(200,200);


Will the original BufferedImage (size 100x100)
be garbage collected now?
The GC is allowed to do so now, and will probably
do at some indefinite time in the future.
I never used new().
The createCompatibleImage() method *did* use new().
(How else could it create a new image.)
 
M

Martijn Mulder

Thomas said:
The GC is allowed to do so now, and will probably
do at some indefinite time in the future.

The createCompatibleImage() method *did* use new().
(How else could it create a new image.)


Is that also true for Graphics-objects that you create on the fly? Some texts
insist that I should call Graphics.dispose() after I am done with the Graphics
object, but when I use code like this:

bufferedimage.createGraphics().drawImage(image,0,0,this);

then I don't even get a change to call dispose();
 
T

Thomas Fritsch

Martijn said:
Is that also true for Graphics-objects that you create on the fly? Some texts
insist that I should call Graphics.dispose() after I am done with the Graphics
object, ...
Well, OK. They recommend this, so that the Graphics-object will
*immediately* release its resources, instead of waiting until it is
garbage-collected (which may be much later). This will be especially
important if you create hundreds of short-lived Graphics-objects per
seconds.
... but when I use code like this:

bufferedimage.createGraphics().drawImage(image,0,0,this);

then I don't even get a change to call dispose();
You get a chance by splitting this code into more than 1 line:
Graphics g = bufferedimage.createGraphics();
g.drawImage(image,0,0,this);
g.dispose();
 
S

Stefan Fleiter

Thomas Fritsch said:
Well, OK. They recommend this, so that the Graphics-object will
*immediately* release its resources, instead of waiting until it is
garbage-collected (which may be much later). This will be especially
important if you create hundreds of short-lived Graphics-objects per
seconds.

You get a chance by splitting this code into more than 1 line:
Graphics g = bufferedimage.createGraphics();
g.drawImage(image,0,0,this);
g.dispose();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top