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 OutOfMemoryError
after 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 {
private static long waitTime = 100;
private static ArrayList<File> imageFiles = new ArrayList<File>() {{
add(new File("thrillercat.gif"));
}};
private static long totalSize = 0;
private static long gifsLoaded = 0;
public static void main(String[] args) throws Exception {
while(!Thread.currentThread().isInterrupted()) {
File imageFile = GifCrash.imageFiles.get((int) (GifCrash.gifsLoaded % GifCrash.imageFiles.size()));
InputStream iStream = new FileInputStream(imageFile);
Image image = new Image(iStream);
iStream.close();
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();
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 ).
!