Java - утечка памяти файлового сервера - PullRequest
0 голосов
/ 02 июня 2018

У меня сервер настроен так, что когда пользователи открывают мое приложение, оно проверяет наличие обновлений с файлового сервера и загружает их при необходимости.

Однако я заметил, что со временем, как людипри подключении и загрузке использование памяти сервером постепенно увеличивается и увеличивается, и уменьшается только в том случае, если я перезапускаю программу 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();
        }
    }
}

Кто-нибудь знает, почему использование памяти продолжает расти?Любые советы будут оценены.Спасибо!

1 Ответ

0 голосов
/ 02 июня 2018

Таким образом, все операции закрытия / уничтожения должны выполняться в предложении finally.Это будет выполнено наверняка.

...