简体   繁体   中英

Java - File Server Memory Leak

I have a server set up that such that when users open my application, it checks for updates from a file server, and downloads them if necessary.

However, I have noticed that over time, as people connect and download, the server's memory usage gradually goes up and up, and only goes down if I restart the Java program that opens the sockets to host the files.

The bulk of the program is included below:

@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();
}

And the variables are defined as follows:

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");

The main loop in a separate class that opens the socket in the first place is as follows:

@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();
        }
    }
}

Does anybody know why the memory usage just keeps going up and up? Any tips would be appreciated. Thanks!

That's way all closing/destroying operations should be performed in the finally clause. That one will be executed for sure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM