简体   繁体   English

在Java中使用扫描仪时如何减少内存泄漏?

[英]How to reduce memory leak when using Scanner in Java?

I using Scanner for reading many logs file (~100 files), each logs file is ~120mb and have over 1,000,000 lines. 我使用扫描仪读取许多日志文件(〜100个文件),每个日志文件约为120mb,并具有超过1,000,000行。 The memory using for reading there logs file is continuous increase and cause memory overload. 用于读取那里的日志文件的内存持续增加,并导致内存过载。 How to prevents this happen? 如何防止这种情况发生? Here's my code: 这是我的代码:

File file = processing.poll(); // processing is a queue.
Scanner sc = new Scanner(file);
String line;
int lineCount = 0;
while (sc.hasNextLine()) {
    line = sc.nextLine();
}
sc.close();

Thanks you!! 谢谢!!

P/S: The memory increase slow, ~1mb for each file processed but I will push this code to the server and there're countless files in the future. P / S:内存增加缓慢,每个处理的文件约1mb,但我会将这段代码推送到服务器,以后会有无数文件。

I suggest you use a try-with-resources to close the Scanner and release the file handle. 我建议您使用try-with-resources close Scanner并释放文件句柄。 Also, you could restrict the scope of the line and I think you meant to increment the lineCount . 此外,您可以限制的范围line ,我想你的意思是递增lineCount Something like, 就像是,

int lineCount = 0;
try (Scanner sc = new Scanner(file)) {
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        lineCount++;
    }
}

Alternatively, you might use a finally block like 或者,你可以使用一个finally块像

int lineCount = 0;
Scanner sc = new Scanner(file);
try {
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        lineCount++;
    }
} finally {
    sc.close();
}

The problem is that you are loading all of these huge ~100 log files into memory. 问题是您正在将所有这些约100个巨大的日志文件加载到内存中。 Think about it. 想一想。 ~100 log files, that are ~120mb each. 约100个日志文件,每个约120mb。 That's a lot of memory being taken! 占用了大量内存! You are going to have to read in a single log file, process it, then release the handle on the file and remove it from memory, and then repeat until all the logs have been processed how you want them to. 您将必须读入单个日志文件,进行处理,然后释放文件上的句柄并将其从内存中删除,然后重复进行直到所有日志都按照您希望的方式处理为止。 You need an algorithm that will process them separately and release the handle/memory as needed to avoid the overload. 您需要一种算法来单独处理它们,并根据需要释放句柄/内存,以避免过载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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