gpt4 book ai didi

java - 什么可能导致我的绘画方法内存泄漏?

转载 作者:行者123 更新时间:2023-11-29 05:23:41 25 4
gpt4 key购买 nike

当我用 Java 制作游戏时,我通常会创建一个对象来处理图形。但是,我以前从未在“主要”绘制方法中的某处发生过内存泄漏。它以前从未发生过,所以我不知道它会是什么。 我的猜测 是它与图像/检索图像有关,因为如果我每次清除缓存并重新加载它们,它不会似乎泄漏内存。有什么想法吗?

问题:以下内存泄漏的原因可能是什么:

[编辑] 原来是字体。谢谢。

引用绘画方法:

public void drawFrame(Graphics2D g) {
// Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int width = CLIENT.GAMEFRAME.getBounds().width;
int height = CLIENT.GAMEFRAME.getBounds().height;
g.setColor(Color.BLACK);
Font font1 = null;
Font font2 = null;
try {
font1 = Font.createFont(Font.TRUETYPE_FONT, new File(
"./cache/fonts/Minecraftia.ttf"));
font2 = Font.createFont(Font.TRUETYPE_FONT, new File(
"./cache/fonts/SF Automaton Extended.ttf"));
g.setFont(font1.deriveFont(15f).deriveFont(Font.BOLD));
} catch (FontFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (CLIENT.user.loggedIn) {
g.drawImage(images.getPermaImage(3), width - 520, height - 94, null);
g.drawImage(images.getPermaImage(2), width - 518, height - 92, null);
// Skills
g.drawImage(images.getPermaImage(0), width - 516, height - 90, null);
g.drawImage(images.getPermaImage(0), width - 466, height - 90, null);
g.drawImage(images.getPermaImage(0), width - 416, height - 90, null);
g.drawImage(images.getPermaImage(0), width - 366, height - 90, null);
g.drawImage(images.getPermaImage(0), width - 316, height - 90, null);
g.drawImage(images.getPermaImage(0), width - 266, height - 90, null);
g.drawImage(images.getPermaImage(0), width - 216, height - 90, null);
g.drawImage(images.getPermaImage(0), width - 166, height - 90, null);
g.drawImage(images.getPermaImage(0), width - 116, height - 90, null);
g.drawImage(images.getPermaImage(0), width - 66, height - 90, null);
g.drawImage(images.getPermaImage(4), 0, height - 290, null);
g.drawString(CLIENT.user.name + ": " + CLIENT.user.message, 8,
height - 48);
} else {
g.drawImage(
images.getPermaImage(5),
(this.getWidth() - images.getPermaImage(5).getWidth(null)) / 2,
(this.getHeight() - images.getPermaImage(5).getHeight(null)) / 2,
null);
g.drawImage(images.getPermaImage(7), ((this.getWidth() - images
.getPermaImage(7).getWidth(null)) / 2) + 136,
((this.getHeight() - images.getPermaImage(7)
.getHeight(null)) / 2) - 106, null);
if (!CLIENT.box1Active) {
g.drawImage(
images.getPermaImage(6),
((this.getWidth() - images.getPermaImage(6).getWidth(
null)) / 2) - 80,
((this.getHeight() - images.getPermaImage(6).getHeight(
null)) / 2) - 106, null);
} else {
g.drawImage(
images.getPermaImage(9),
((this.getWidth() - images.getPermaImage(6).getWidth(
null)) / 2) - 80,
((this.getHeight() - images.getPermaImage(6).getHeight(
null)) / 2) - 106, null);
}
if (!CLIENT.box2Active) {
g.drawImage(
images.getPermaImage(6),
((this.getWidth() - images.getPermaImage(6).getWidth(
null)) / 2) - 80,
((this.getHeight() - images.getPermaImage(6).getHeight(
null)) / 2) - 50, null);
} else {
g.drawImage(
images.getPermaImage(9),
((this.getWidth() - images.getPermaImage(6).getWidth(
null)) / 2) - 80,
((this.getHeight() - images.getPermaImage(6).getHeight(
null)) / 2) - 50, null);
}

g.drawImage(
images.getPermaImage(11),
((this.getWidth() - images.getPermaImage(10).getWidth(null)) / 2) + 180,
((this.getHeight() - images.getPermaImage(10).getHeight(
null)) / 2) - 50, null);
g.setColor(Color.GREEN);
g.setFont(font2.deriveFont(18f));
g.drawString("LOG-IN", ((this.getWidth() - images.getPermaImage(10)
.getWidth(null)) / 2) + 205, ((this.getHeight() - images
.getPermaImage(10).getHeight(null)) / 2) - 18);
g.setColor(Color.BLACK);
g.setFont(font1.deriveFont(30f).deriveFont(Font.BOLD));
g.drawString(
CLIENT.user.usernameBox,
((this.getWidth() - (CLIENT.user.usernameBox.length() * 1)) / 2) - 225,
((this.getHeight()) / 2) - 95);
g.drawString(
CLIENT.user.passwordBox,
((this.getWidth() - (CLIENT.user.usernameBox.length() * 1)) / 2) - 225,
((this.getHeight()) / 2) - 39);
if (CLIENT.user.rememberMe) {
g.drawImage(
images.getPermaImage(8),
((this.getWidth() - images.getPermaImage(8).getWidth(
null)) / 2) + 140,
((this.getHeight() - images.getPermaImage(8).getHeight(
null)) / 2) - 110, null);
}
}
}

这是 ImageCache 对象:

public final class ImageCache {

public ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
public ArrayList<Integer> ids = new ArrayList<Integer>();
public ArrayList<BufferedImage> permanentImages = new ArrayList<BufferedImage>();
public ArrayList<Integer> pids = new ArrayList<Integer>();

public void loadImage(String path, int id) {
try {
images.add(ImageIO.read(new File(path)));
} catch (IOException e) {
e.printStackTrace();
return;
}
ids.add(id);
}

public void writeCache() {
String path = "./cache/sprites/gameframe/";
File f = new File(path);
byte[] b;
String zipFile = "./Cache.dat";
FileOutputStream fout = null;
try {
fout = new FileOutputStream(zipFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(
fout));

File[] s = f.listFiles();
for (int i = 0; i < s.length; i++) {
b = new byte[2028];
FileInputStream fin = null;
try {
fin = new FileInputStream(s[i]);
zout.putNextEntry(new ZipEntry(s[i].getName()));
int length;
while ((length = fin.read(b, 0, 1024)) > 0) {
zout.write(b, 0, length);
}
zout.closeEntry();
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
zout.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public void clearCache() {
images.clear();
ids.clear();
}

public void loadPermanentImage(String path, int id) {
try {
permanentImages.add(ImageIO.read(new File(path)));
} catch (IOException e) {
e.printStackTrace();
return;
}
pids.add(id);
}

public BufferedImage getImage(int id) {
for (int i = 0; i < ids.size(); i++) {
if (ids.get(i) == id) {
return images.get(i);
}
}
return null;
}

public BufferedImage getPermaImage(int id) {
for (int i = 0; i < pids.size(); i++) {
if (pids.get(i) == id) {
return permanentImages.get(i);
}
}
return null;
}

提前致谢。

最佳答案

不知道它是否会导致内存泄漏,但您不应该在任何绘画方法中进行任何文件 I/O。

所以您不应该在每次调用该方法时都读取字体文件。

关于java - 什么可能导致我的绘画方法内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23601114/

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