[OpenJDK 2D-Dev] Why are java images type not serializable ?

Jim Graham james.graham at oracle.com
Tue Mar 10 18:37:44 UTC 2015

Serializing images by content is generally wasteful in terms of the size 
of the data stream.

For images loaded from a URL or file, presumably those can be reloaded 
by the application if it has a way to retrigger loading its resources (a 
common technique is to lazily load some images or have a central method 
for loading media - both of which are easy to trigger in the process of 

For images that are generated on the fly, often those can be regenerated 
by the code, but it may not be as automatic as lazily reloading an image 
from its file/URL.

One issue is that images may be loaded from a cache and multiple 
references stored throughout the program (particularly for images used 
as common UI elements).  I suppose that a well-written serialization 
mechanism may notice the repeated references and try to share the 
serialized forms, but I'm not entirely sure how that works or how that 
would impact serializing the images.

There is also the issue that BufferedImages can be fully custom, which 
means that we have no idea how the pixels are stored or where they are 
stored.  The fact that ColorModel, SampleModel, and Raster are openly 
subclassable allows for such completely custom implementations.  We 
could not guarantee serializability of custom subclasses of any of 
those, unfortunately...


On 3/10/15 10:20 AM, Jean-François Savard wrote:
> Hi,
> I've realized java.awt.image.BufferedImage is not serializable.
> After a couple of research, I've realized that each java image types
> (correct me if I'm wrong) are not serializable at all.
> I've then decided to do research without specifying java as language and
> realized serialization of image is not present in most of the languages.
> I've come to the conclusion that is simply because serializing an image
> is illogical. When you load an image, it is because you already have the
> resource stored somewhere (file or in-memory), so there is no point in
> serializing it as the main utility of serialization is to store data
> independent of the JVM, which is already the case for an image file. For
> the case where the image is in-memory, it mean you already loaded the
> ressource and have the byte array so it is better to simply serialize
> that byte array.
> Would you please confirm that my understanding is correct ?
> Best regards.

More information about the 2d-dev mailing list