2013-10-06 141 views
1

今天我做了簡單的Java應用程序,應該只是加載一些.png文件到內存(這些文件的總空間是~7mb),但它使這個奇怪的錯誤:奇怪的「java.lang.OutOfMemoryError:Java堆空間」

(我的小命令:java -Xmx1024m -jar DIST/PNGImageLoader.jar)

Loaded image (0) images/image_01000111100000011000000110100000 Loaded image (1) images/image_00000101010101101101000111111101 Loaded image (2) images/image_01001110110011110011111001000000 Loaded image (3) images/image_01001111000111010110101101001111 Loaded image (4) images/image_00011100000011100011001011011100 Loaded image (5) images/image_01010010001010001010110110110110 Loaded image (6) images/image_01101100010001000001000110101111 Loaded image (7) images/image_01010111011011101010101110100010 Loaded image (8) images/image_01101101110100001011001011101100 Loaded image (9) images/image_01100010111110011011100001000011 Loaded image (10) images/image_00011111011000111101101111101111 Loaded image (11) images/image_00000100111001010110101001110001 Loaded image (12) images/image_01000001100111011101101011011011 Loaded image (13) images/image_01100111000010000001011101111001 Loaded image (14) images/image_01101000011011000001111010001100 Loaded image (15) images/image_00010010110100001011000111111000 Loaded image (16) images/image_00111100111011101101101001000101 Loaded image (17) images/image_01111110001010111100011000000110 Loaded image (18) images/image_00010111101011100011101101101000 Loaded image (19) images/image_00101000001111110110000110101101 Loaded image (20) images/image_01110101110000010000000000001001 Loaded image (21) images/image_01001000101010101010000010100011 Loaded image (22) images/image_00110111100001110011010011011001 Loaded image (23) images/image_00001010001001000001101001001011 Loaded image (24) images/image_00101111000110011000110011111000 Loaded image (25) images/image_00101100010101000011001101010111 Loaded image (26) images/image_01101111101000010111011000011010 Loaded image (27) images/image_00111011111100000111100000000011 Loaded image (28) images/image_01100100000101111111001000111101 Loaded image (29) images/image_01001101110111111011000101000011 Loaded image (30) images/image_00110010111010000111111011100101 Loaded image (31) images/image_00111001110100011100101111111011 Loaded image (32) images/image_01100011101111010111110110001111 Loaded image (33) images/image_01000000110111000000010100010011 Loaded image (34) images/image_00110101000001001101100010101001 Loaded image (35) images/image_01001101011011010110100110111010 Loaded image (36) images/image_00010111010100110100011111110101 Loaded image (37) images/image_01011111011001111010000000111000 Loaded image (38) images/image_00110001100110011111000010110001 Loaded image (39) images/image_01110101010001111001011000000011 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.awt.image.DataBufferByte.(DataBufferByte.java:92) at java.awt.image.ComponentSampleModel.createDataBuffer(ComponentSampleModel.java:415) at java.awt.image.Raster.createWritableRaster(Raster.java:941) at javax.imageio.ImageTypeSpecifier.createBufferedImage(ImageTypeSpecifier.java:1073) at javax.imageio.ImageReader.getDestination(ImageReader.java:2896) at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1280) at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1577) at javax.imageio.ImageIO.read(ImageIO.java:1448) at javax.imageio.ImageIO.read(ImageIO.java:1308) at pngimageloader.Images.loadImage(Images.java:22) at pngimageloader.Images.getImage(Images.java:33) at pngimageloader.Images.getAllImages(Images.java:43) at pngimageloader.PNGImageLoader.main(PNGImageLoader.java:15)

這裏是我PNGImageLoader.java類,

package pngimageloader; 

public class PNGImageLoader { 

    public static void main(String[] args) { 
     Images images = new Images(); 
     images.getAllImages("images/"); 
    } 
} 

這裏是Images.java類..

package pngimageloader; 

import java.awt.image.BufferedImage; 
import java.io.File; 
import java.util.HashMap; 
import javax.imageio.ImageIO; 

public class Images { 

    HashMap images; 

    Images() { 
     images = new HashMap(); 
    } 

    BufferedImage loadImage(String path) { 
     try { 
      return ImageIO.read(new File(path)); 
     } catch (Exception exception) { 
      System.out.println(exception.getMessage()); 
      System.exit(1); 
      return null; 
     } 
    } 

    BufferedImage getImage(String path) { 
     BufferedImage image = (BufferedImage) images.get(path); 
     if (image == null) { 
      image = loadImage(path); 
      images.put(path, image); 
     } 
     return image; 
    } 

    void getAllImages(String path) { 
     File files = new File(path); 
     int i = 0; 
     for (File file : files.listFiles()) { 
      getImage(file.getPath()); 
      System.out.println("Loaded image (" + i + ") " + file.getPath()); 
      i++; 
     } 
    } 
} 

我不專業,但我認爲這真的很奇怪。 在此先感謝您的幫助,Patrick。

+4

它是如何奇怪?您正在從內存中加載大量磁盤映像。內存是有限的。所以它會拋出一個OutOfMemoryError,因爲它沒有足夠的內存來加載所有的圖像。 –

+0

@JBNizet顯然有些奇怪的是,這些圖片的總大小僅爲〜7mb,而[默認的最大堆大小大得多](http://docs.oracle.com/javase/7/docs/technotes/guides/ VM/GC-ergonomics.html)。當然,除非使用默認值以外的值,否則... – yair

+0

對我來說這很奇怪,因爲這些文件的總空間大約是7mb,而且我有免費的2GB內存。 我的小命令:java -Xmx128m -jar dist/PNGImageLoader.jar –

回答

0

由於GSP指出您的小文件可以擴展到大量內存。 事情是你最有可能不應該保存在記憶中的圖像,幾乎沒有理由這樣做。 只需使用某種流媒體內容,依賴於您想要做的事情。

相關問題