我需要改进一个开源工具 (Releng)(符合 JDK 1.5 合规性),用于更新源文件中的版权 header 。 (例如版权 2000、2011)。
它读取文件并插入较新的修订日期(例如 2014 年)。
目前它消耗了太多内存,导致性能下降得非常缓慢。 我需要重新编写文件解析器,以便它使用更少的内存/运行得更快。
我编写了一个基本文件解析器(如下),它读取目录(项目/文件)中的所有文件。然后它递增在文件中找到的前四位数字并打印运行时信息。
[编辑] 在小范围内,当前结果执行 25 次垃圾收集,垃圾收集需要 12 毫秒。在大规模上,我获得了太多的内存开销,以至于 GC 影响了性能。
Runs Time(ms) avrg(ms) GC_count GC_time
200 4096 20 25 12
200 4158 20 25 12
200 4072 20 25 12
200 4169 20 25 13
是否可以重用 File 或 String 对象(以及其他对象??)来减少垃圾收集计数?
优化指南建议重新使用对象。 我考虑过使用 Stringbuilder 而不是 Strings。但据我所知,它只有在进行大量串联时才有用。在这种情况下哪一个没有做? 我也不知道如何在下面的代码中重用任何其他对象(例如文件?)?
在这种情况下我该如何重用对象(或优化下面的代码)?
欢迎任何想法/建议。
import java.io.File;
import java.io.IOException;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
public class Test {
//Use Bash script to create 2000 files, each having a 4 digit number.
/*
#!/bin/sh
rm files/test*
for i in {1..2000}
do
echo "2000" > files/test$i
done
*/
/*
* Example output:
* runs: 200
* Run time: 4822 average: 24
* Gc runs: Total Garbage Collections: 28
* Total Garbage Collection Time (ms): 17
*/
private static String filesPath = System.getProperty("user.dir") + "/src/files";
public static void main(String args[]) {
final File folder = new File(filesPath);
ArrayList<String> paths = listFilesForFolder(folder);
if (paths == null) {
System.out.println("no files found");
return;
}
long start = System.currentTimeMillis();
// ..
// your code
int runs = 200;
System.out.println("Run: ");
for (int i = 1; i <= runs; i++) {
System.out.print(" " + i);
updateFiles(paths);
}
System.out.println("");
// ..
long end = System.currentTimeMillis();
long runtime = end - start;
System.out.println("Runs Time avrg GC_count GC_time");
System.out.println(runs + " " + Long.toString(runtime) + " " + (runtime / runs) + " " + printGCStats());
}
private static ArrayList<String> listFilesForFolder(final File folder) {
ArrayList<String> paths = new ArrayList<>();
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
paths.add(filesPath + "/" + fileEntry.getName());
}
}
if (paths.size() == 0) {
return null;
} else {
return paths;
}
}
private static void updateFiles(final ArrayList<String> paths) {
for (String path : paths) {
try {
String content = readFile(path, StandardCharsets.UTF_8);
int year = Integer.parseInt(content.substring(0, 4));
year++;
Files.write(Paths.get(path), Integer.toString(year).getBytes(),
StandardOpenOption.CREATE);
} catch (IOException e) {
System.out.println("Failed to read: " + path);
}
}
}
static String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path)); // closes file.
return new String(encoded, encoding);
}
//PROFILING HELPER
public static String printGCStats() {
long totalGarbageCollections = 0;
long garbageCollectionTime = 0;
for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
long count = gc.getCollectionCount();
if (count >= 0) {
totalGarbageCollections += count;
}
long time = gc.getCollectionTime();
if (time >= 0) {
garbageCollectionTime += time;
}
}
return " " + totalGarbageCollections + " " + garbageCollectionTime;
}
}
最佳答案
最后,上面的代码实际上运行得很好。
我发现在生产代码中,代码没有关闭文件缓冲区,这导致内存泄漏,从而导致大量文件出现性能问题。
修复后,它的扩展性良好。
关于Java:如何优化读取/更新/写入许多小文件的内存占用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25146626/