JavaFX OutOfMemoryError with animated GIFs

I was creating a JavaFX 2.0 application with an image browser that would have to display animated GIFs when I came across some exceptions OutOfMemoryErrorafter viewing several GIFs. I managed to highlight the appropriate code in the "GifCrasher" application:

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;

import javafx.scene.image.Image;


public class GifCrash {

    // Settings:
    private static  long            waitTime    =   100;    // msec
    private static  ArrayList<File> imageFiles  =   new ArrayList<File>() {{
                                                    add(new File("thrillercat.gif"));
                                                }};

    // Other:
    private static  long    totalSize   =   0;
    private static  long    gifsLoaded  =   0;

    public static void main(String[] args) throws Exception {

        while(!Thread.currentThread().isInterrupted()) {
            // Read gif file:
            File        imageFile   =   GifCrash.imageFiles.get((int) (GifCrash.gifsLoaded % GifCrash.imageFiles.size()));
            InputStream iStream     =   new FileInputStream(imageFile);
            Image       image       =   new Image(iStream);
            iStream.close();

            // Display info:
            GifCrash.gifsLoaded++;
            GifCrash.totalSize += imageFile.length();
            System.out.println("Loaded " + imageFile + " (" + imageFile.length() + " bytes)");
            System.out.println("GifCount\t=\t" + GifCrash.gifsLoaded);
            System.out.println("TotalSize\t=\t" + Math.round((double) GifCrash.totalSize / (1024 * 1024)) + " MBytes (" + GifCrash.totalSize + " bytes)");
            System.out.println();

            // Wait?
            if (GifCrash.waitTime > 0) {
                Thread.sleep(GifCrash.waitTime);
            }
        }
    }
}

javafx Image, , , , . GIF, GIF , - ( > 250 GIF ). waitTime, , . , GIF imageFiles - OutOfMemoryError ( GIF 250 ). PNG , , GIF - .

, : thrillercat.gif catdestroyer.png. () , thrillercat.gif:

Loaded thrillercat.gif (1203120 bytes)
GifCount    =   1
TotalSize   =   1 MBytes (1203120 bytes)

Loaded thrillercat.gif (1203120 bytes)
GifCount    =   2
TotalSize   =   2 MBytes (2406240 bytes)

...

Loaded thrillercat.gif (1203120 bytes)
GifCount    =   225
TotalSize   =   258 MBytes (270702000 bytes)

Loaded thrillercat.gif (1203120 bytes)
GifCount    =   226
TotalSize   =   259 MBytes (271905120 bytes)

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at com.sun.javafx.iio.gif.GIFImageLoader2.decodePalette(GIFImageLoader2.java:288)
    at com.sun.javafx.iio.gif.GIFImageLoader2.load(GIFImageLoader2.java:191)
    at com.sun.javafx.iio.ImageStorage.loadAll(ImageStorage.java:294)
    at com.sun.javafx.iio.ImageStorage.loadAll(ImageStorage.java:244)
    at com.sun.javafx.tk.quantum.PrismImageLoader2.loadAll(PrismImageLoader2.java:107)
    at com.sun.javafx.tk.quantum.PrismImageLoader2.<init>(PrismImageLoader2.java:41)
    at com.sun.javafx.tk.quantum.QuantumToolkit.loadImage(QuantumToolkit.java:607)
    at javafx.scene.image.Image.loadImage(Image.java:942)
    at javafx.scene.image.Image.initialize(Image.java:722)
    at javafx.scene.image.Image.<init>(Image.java:625)
    at GifCrash.main(GifCrash.java:27)

, , ​​ , , ?

, ? GIF JavaFX ( GIF ).

!

+4
1

Java 8, Java 8.

Java 7u45 (OS X 10.8) (MacBook Air 2012, 4gb ram), 71 .

Java 8 5000 :

Loaded /Users/lilyshard/dev/playfx/src/thriller-cat-o.gif (1203120 bytes)
GifCount    =   10000
TotalSize   =   11474 MBytes (12031200000 bytes)

Java 7u45, , , Java 8, JavaFX 2.2. JavaFX, .

, JavaFX 2.2 gif, Java 8.

+6

All Articles