【发布时间】:2023-10-08 18:01:01
【问题描述】:
我试图解决这个创建目录的方法为什么要使用这么多内存(大约 530MB PS 伊甸园空间)的谜团。方法执行后,GC 会清理一些内存,但之后仍有一些内存。但是分配的内存总是保持不变(大约 700MB 分配的所有池)。看来我做取消引用对象失败了:(
如果有人能给我一些如何处理它的建议,那就太好了。
public void writeDir(File root, ArrayList<String> hardwareList, ArrayList<String> detectionListFormated, ArrayList<String> siteName, int depth) {
if (depth == 1) {
return;
}
if (depth == 2) {
for (int i = 0; i < listToDestroy.size(); i++) {
String toBeFormated = listToDestroy.get(i);
String toBeTrimmed = toBeFormated.replace("ä", "ae").replace("ß", "ss").replace("ü", "ue").replace("ö", "oe").trim();
String s = trimLastChar(toBeTrimmed);
int index = s.indexOf("_");
if (s.charAt(index + 1) == this.stationNumber) {
if (s.contains("Manuelle Gruppe")) {
File subdir = new File(root, s);
File samePath = new File(root, "");
subdir.mkdir();
detectionListFormated.remove(i);
listToDestroy.remove(i);
writeDir(samePath, hardwareList, detectionListFormated, siteName, depth);
} else if (s.contains("Automatische Gruppe")) {
File subdir = new File(root, s);
File samePath = new File(root, "");
subdir.mkdir();
detectionListFormated.remove(i);
listToDestroy.remove(i);
writeDir(samePath, hardwareList, detectionListFormated, siteName, depth);
} else if (s.contains("Abschnitt")) {
writeDir(root.getParentFile(), hardwareList, detectionListFormated, siteName, depth + 1);
} else if (s.contains("Stations-Objekt")) {
writeDir(root.getParentFile().getParentFile(), hardwareList, detectionListFormated, siteName, depth + 2);
}
}
}
}
if (depth == 3) {
for (int i = 0; i < listToDestroy.size(); i++) {
String toBeFormated = listToDestroy.get(i);
String toBeTrimmed = toBeFormated.replace("ä", "ae").replace("ß", "ss").replace("ü", "ue").replace("ö", "oe").trim();
String s = trimLastChar(toBeTrimmed);
int index = s.indexOf("_");
if (s.charAt(index + 1) == stationNumber) {
if (s.contains("Abschnitt")) {
File subdir = new File(root, s);
subdir.mkdir();
detectionListFormated.remove(s);
listToDestroy.remove(i);
writeDir(subdir, hardwareList, detectionListFormated, siteName, depth - 1);
} else if (s.contains("Detektions-Objekt")) {
writeDir(root.getParentFile(), hardwareList, detectionListFormated, siteName, depth + 1);
} else if (s.contains("Stations-Objekt")) {
writeDir(root.getParentFile(), hardwareList, detectionListFormated, siteName, depth + 1);
}
}
}
}
if (depth == 4) {
for (int i = 0; i < listToDestroy.size(); i++) {
String toBeFormated = listToDestroy.get(i);
String toBeTrimmed = toBeFormated.replace("ä", "ae").replace("ß", "ss").replace("ü", "ue").replace("ö", "oe").trim();
String s = trimLastChar(toBeTrimmed);
int index = s.indexOf("_");
if (s.charAt(index + 1) == stationNumber) {
if (s.contains("Stations-Objekt")) {
File subdir = new File(root, s);
subdir.mkdir();
listToDestroy.remove(i);
detectionListFormated.remove(i);
// if added it uses literaly no memory at allSystem.gc();
writeDir(root, hardwareList, detectionListFormated, siteName, depth - 3);
} else if (s.contains("Detektions-Objekt")) {
File subdir = new File(root, s);
subdir.mkdir();
listToDestroy.remove(i);
detectionListFormated.remove(i);
// if added it uses literaly no memory at allSystem.gc();
writeDir(subdir, hardwareList, detectionListFormated, siteName, depth - 1);
}
}
}
}
if (depth == 5) {
for (String s : hardwareList) {
String toBeFormated = s;
String toBeTrimmed = toBeFormated.replace("ä", "ae").replace("ß", "ss").replace("ü", "ue").replace("ö", "oe").trim();
String a = trimLastChar(toBeTrimmed);
File subdir = new File(root, a);
subdir.mkdir();
this.stationNumber = a.charAt(0);
// if added it uses literaly no memory at allSystem.gc();
writeDir(subdir, hardwareList, detectionListFormated, siteName, depth - 1);
}
}
if (depth == 6) {
for (String s : siteName) {
String toBeFormated = s;
String toBeTrimmed = toBeFormated.replace("ä", "ae").replace("ß", "ss").replace("ü", "ue").replace("ö", "oe").trim();
String a = trimLastChar(toBeTrimmed);
File subdir = new File(root, a);
subdir.mkdirs();
listToDestroy = new CopyOnWriteArrayList<>(detectionListFormated);
ArrayList<String> test = new ArrayList<>(listToDestroy);
// if added it uses literaly no memory at allSystem.gc();
writeDir(subdir, hardwareList, test, siteName, depth - 1);
}
}
}
}
谢谢:)
【问题讨论】:
-
可能文件太多?
-
它创建了 220 个文件夹。我不认为这应该是问题。我希望它创建 n 个文件
-
您知道使用
CopyOnWriteArrayList意味着每当您remove(或对其进行任何其他写入操作)时都会有一个新的列表副本?
标签: java file methods arraylist mkdir