I have a couple more items that may affect OOM when using the Toolkit
image code...

Try using the Toolkit.createImage(...) variants instead of the
Toolkit.getImage(...) variants.  The latter getImage calls all keep a
copy of the image that was returned in an internal hashmap in case they
get a future request for an image loaded from the same location.  I
think you said you were pre-loading the data into byte arrays before
making a Toolkit image out of it in which case you are probably already
using the createImage(byte array) call and there will be no implicit
caching for that call.

If you drop your reference to the original Image (and to the
PixelGrabber that you construct with it), then call the Image.flush()
method on the object before you drop your reference to it.  This will
cause the system to immediately flush all of the pixel data for the
Toolkit version of the image - which won't affect your PixelGrabber copy
at all - and the memory can be reclaimed more aggressively.

It might be a good idea to call System.gc() after you flush the image
and drop your reference(s) to it to be more proactive about keeping
memory cleaned up.

Finally, the code:

       int w = image.getWidth(canvas);
       int h = image.getHeight(canvas);
       int p[] = new int[w * h];
       PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, p);
       // ... pg.grabPixels...

is only reliable if you force the loading of the image before you call
this code.  If you do not preload the image then the calls to getWidth
and getHeight might return -1 meaning "the data isn't loaded yet".  If
you load the image from a local byte array of image data then the data
might be decoded fast enough to be ready by the time you call getWidth
and getHeight, but there is still potential for a race condition there.
 Finally, those calls to getWidth/Height(canvas) both cause the Toolkit
image to populate itself with pixel data that you will likely never use
since you only care about the copy in the PixelGrabber.

The following code:

       PixelGrabber pg = new PixelGrabber(image, 0, 0, -1, -1, true);
       // ...pg.grabPixels...
       int w = pg.getWidth();
       int h = pg.getHeight();
       int p[] = (int[]) pg.getPixels();

will do the same thing, but never trigger the Toolkit image to load its
internal copy of the data.  The PG copy of the data will be the only one
created.  The width and height will be obtained by the PixelGrabber from
the ImageConsumer stream and it will create an int[] array if you tell
it to force the RGB colormodel with the last parameter "true".

Hopefully with one or more of these suggestions you can reduce your
memory consumption and avoid OOM conditions for much longer...

                       ...jim

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to