gpt4 book ai didi

java - 奇怪的 "java.lang.OutOfMemoryError: Java heap space"

转载 作者:行者123 更新时间:2023-12-01 04:18:31 25 4
gpt4 key购买 nike

今天我做了一个简单的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 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++;
}
}
}

我不专业,但我觉得这真的很奇怪。预先感谢您的帮助,帕特里克。

最佳答案

正如 GSP 指出的那样,您的小文件可以扩展到大量内存。事情是你很可能不应该将图像保留在内存中,几乎没有理由这样做。只需使用某种流媒体内容,具体取决于您想要做什么。

关于java - 奇怪的 "java.lang.OutOfMemoryError: Java heap space",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19209818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com