1

今日、私は単純なJavaアプリケーションを作成しました.pngファイルをメモリにロードする必要があります(これらのファイルの合計スペースは〜7mbです)が、次の奇妙なエラーが発生します。

(私の小さなコマンド: 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 読み込んだ画像(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 読み込んだ画像(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 読み込んだ画像(39) images/image_01110101010001111001011000000011 スレッド「メイン」での例外 java.lang.OutOfMemoryError: java.awt.image.DataBufferByte の Java ヒープ スペース。 java:415) で java.awt.image.Raster.createWritableRaster(Raster.java:941) で javax.imageio.ImageTypeSpecifier.createBufferedImage(ImageTypeSpecifier.java:1073) で javax.imageio.ImageReader.getDestination(ImageReader.java:2896) で) com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1280) で com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1577) で javax.imageio.ImageIO. read(ImageIO.java:1448) at javax.imageio.ImageIO.read(ImageIO.java:1308) at pngimageloader.Images.loadImage(Images.java:22) pngimageloader.Images.getImage(Images.java:33) で pngimageloader.Images.getAllImages(Images.java:43) で 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++;
        }
    }
}

私は専門家ではありませんが、それは本当に奇妙だと思います。助けてくれてありがとう、パトリック。

4

1 に答える 1

0

GSP が指摘したように、小さなファイルは大量のメモリに拡張できます。おそらく、画像をメモリに保持しないでください。そうする理由はほとんどありません。やりたいことに応じて、ある種のストリーミングのものを使用してください。

于 2013-10-06T14:46:40.907 に答える