Results 1 to 3 of 3
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    CA, USA
    Rep Power

    Default ImageIO Memory Issue for large images

    I developed a tool for a number of people. Most people don't seem to have a problem with it, but a number of people said they are having issues when saving large images. Essentially, the this part of the tool takes 70 modest sized images and combines them into one large image. After some research, I discovered they were running out of memory. Some people were able to rectify the issue by running the jar with a larger heap size, but not all of the users know how to do that - besides, it would be better to lower the memory footprint anyway. I am looking for a way to conserve memory when saving these large images... my current method is simply using javax.imageio.ImageIO (detailed below). Any ideas are hugely appreciated! Thanks.

    Java Code:
    public void save()
    	// File file... (location it is saved)
    	// String format... (JPG or PNG)
    	try(final FileImageOutputStream out = new FileImageOutputStream(file))
    		final BufferedImage image = createImage(deck, size, scaleHint);
    		ImageIO.write(image, format, out);
    private static BufferedImage createImage(final Deck deck, final Dimension size, final int scaleHint)
    	final Dimension cardSize = deck.getCardDimension();
    	final Dimension deckSize = deck.getDeckDimension();
    	final BufferedImage image = new BufferedImage(deckSize.width, deckSize.height, BufferedImage.TYPE_INT_RGB);
    	final Graphics g = image.getGraphics();
    	g.fillRect(0, 0, image.getWidth(), image.getHeight());
    	for(int x = 0; x < App.DECK_COLUMNS; x++)
    		for(int y = 0; y < App.DECK_ROWS; y++)
    			final ImageFile card = deck.getCard(x, y);
    			if(card != null)
    				final Image draw = card.getImage().getScaledInstance(cardSize.width, cardSize.height, scaleHint);
    				final int drawX = cardSize.width * x;
    				final int drawY = cardSize.height * y;
    				g.drawImage(draw, drawX, drawY, null);
    	final Image scaled = image.getScaledInstance(size.width, size.height, scaleHint);
    	final BufferedImage bufferedImage = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    	bufferedImage.getGraphics().drawImage(scaled, 0, 0, null);
    	return bufferedImage;

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: ImageIO Memory Issue for large images

    Please include sufficient information on how to use the "tool." Also, include any missing classes (e.g. Deck). An example of how it is used would also be helpful so it can be compiled. I do not expect you to include the images).

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Rep Power

    Default Re: ImageIO Memory Issue for large images

    Images take up a set amount of memory because in the end they're just buffers of raw byte data, you're not going to change anything there. If you want to make the application use less memory you'll just have to make sure that less image data is in memory at the same time. That will probably mean you stop using the comfortable Java imaging classes and start working with streaming uncompressed image data as raw bytes to disc, to compress it to the format of your choice in a secondary processing step.

    Or you could:

    a1) provide shell scripts to boot the application using an installed runtime with the proper heap parameters set
    a2) provide a bundled runtime with a shell script to boot the application with the proper heap parameters set

    (you can probably work out the benefits and disadvantages of either of those approaches yourself)

    b) use a jar wrapper tool such as launch4j to generate an executable which will automatically set the proper parameters
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 0
    Last Post: 10-17-2013, 09:56 AM
  2. Replies: 0
    Last Post: 10-14-2013, 11:37 AM
  3. Replies: 0
    Last Post: 10-31-2011, 11:26 AM
  4. Parsing Large PDF :Out of Memory Exception
    By murthybhat in forum Advanced Java
    Replies: 5
    Last Post: 11-03-2009, 05:08 AM
  5. Displaying large images
    By pir8ped in forum AWT / Swing
    Replies: 2
    Last Post: 01-21-2009, 09:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts