【问题标题】:Java - File Server Memory LeakJava - 文件服务器内存泄漏
【发布时间】:2018-06-02 16:35:17
【问题描述】:

我设置了一个服务器,当用户打开我的应用程序时,它会检查来自文件服务器的更新,并在必要时下载它们。

但是,我注意到随着时间的推移,随着人们连接和下载,服务器的内存使用量会逐渐上升,只有在我重新启动打开套接字以托管文件的 Java 程序时才会下降。

该程序的大部分内容如下:

@Override
public void run() {
    try {
        int archiveCount = in.readByte();
        while(archiveCount-- > 0) {
            int nameLength = in.readInt();
            byte[] name = new byte[nameLength];
            in.read(name);
            int size = in.readInt();
            clientLengths.put(new String(name).toLowerCase().trim(), size);
        }
        if(!clientLengths.isEmpty()) {
            int count = 0;
            for(String fileName : lengths.keySet()) {
                if(!clientLengths.containsKey(fileName) || 
                        (long)clientLengths.get(fileName) != (long)lengths.get(fileName)) {
                    missing.add(fileName);
                    count += lengths.get(fileName);
                }
            }
            if(count == 0) {
                out.writeByte(0);
            } else {
                out.writeByte(1);
                byte[] archive = readFile(archiveZip);
                out.writeInt(count);
                readFully(new ByteArrayInputStream(archive), out, archive.length);
            }
        } else { //hit if cache doesnt exist
            out.writeByte(2);
            byte[] cache = readFile(cacheZip);
            out.writeInt(cache.length);
            readFully(new ByteArrayInputStream(cache), out, cache.length);
        }
        destroy();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

private void destroy() throws IOException {
    System.out.println("Finished cache handling from: " + socket.getInetAddress());
    missing.clear();
    clientLengths.clear();
    socket.close();
}

并且变量定义如下:

private final Socket socket;
private final DataInputStream in;
private final DataOutputStream out;
private final List<String> missing = new ArrayList<>();
private final HashMap<String, Integer> clientLengths = new HashMap<>();
private final static HashMap<String, Integer> lengths = new HashMap<>();
private final static File dir = new File("./Data/Archive");
private final static File archiveZip = new File("./Data/Archive.zip");
private final static File cacheZip = new File("./Data/Cache.zip");

首先打开套接字的单独类中的主循环如下:

@Override
public void run() {
    while(true) {
        try {
            Socket socket = cacheSocket.accept();
            System.out.println("Accepted cache socket from: " + socket.getInetAddress());
            Thread acceptor = new Thread(new CacheHandler(socket));
            acceptor.start();
            Main.debug();
            System.gc();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

有人知道为什么内存使用量一直在上升吗?任何提示将不胜感激。谢谢!

【问题讨论】:

    标签: java sockets memory datainputstream


    【解决方案1】:

    这样所有关闭/销毁操作都应该在finally 子句中执行。那个肯定会被处决的。

    【讨论】:

    • 或者更好,使用 try-with-resources。
    猜你喜欢
    • 2011-07-21
    • 2016-06-09
    • 2011-02-18
    • 2016-01-09
    • 2015-02-12
    • 2013-08-27
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    相关资源
    最近更新 更多