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

时间:2014-05-12 04:35:31

标签: java memory-leaks paint

正如我在Java中制作游戏时通常所做的那样,我创建了一个处理图形的对象。但是,我从来没有在" main"内部的任何地方发生内存泄漏。涂料方法。它从未发生过,所以我不知道它可能是什么。 我的猜测 它与图像/检索图像有关,因为如果我每次擦除缓存并重新加载它们,它就不会#39 ; t似乎泄漏内存。有什么想法吗?

问题:以下内容可能是导致内存泄漏的原因:

[编辑]事实证明它实际上是字体。感谢。

绘画方法供参考:

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;
}

提前致谢。

1 个答案:

答案 0 :(得分:3)

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

因此,每次调用该方法时都不应该读取Font文件。