I’m working with an embedded image and running into a java heap space problem when doing the ImageIO.read(new ByteArrayInputStream(bytes)); to set the image’s size. I’m wondering if there is a size of file that is suggested not to go over when using this method or if there is a way that this memory can be freed up after the image is sized. Any suggestions would be appreciated.
If you really need to process huge images consider using ImageIO.read(InputStream) method with FileInputStream to not load huge images to memory twice (in byte array and as image object). If you need to obtain InputStream from FileStorage you can use FileDataProvider class:
FileDescriptor fd = ...; BufferedImage bufferedImage = ImageIO.read(new FileDataProvider(fd).provide());
If you need to show a huge image in a web browser, you can pass that FileDataProvider to ExportDisplay or create and use your own implementation of ExportDataProvider based on input stream processing instead of byte arrays.
I would not recommend processing in memory files larger than 10 MB, but it depends on your application specific.