【发布时间】:2025-05-18 18:05:02
【问题描述】:
我正在尝试调试此方法,但似乎没有任何效果...
我怀疑在条件语句中 pathName.add() 会引起麻烦。执行此方法后,它会占用 50 MB,再执行一次它会占用 150 MB,直到达到 800 MB。但一切都是分配的空间。为什么 gc 不清理这个烂摊子???
P.s 这个方法根据给定的路径创建目录,这些路径是用条件语句构造的
P.s P.s 从 actionListener 中调用 writeDir(...) 方法(当点击 gui ic 上的按钮时)。按钮可以经常点击
P.s P.s P.s 我已经尝试了 Andreas 的建议,它部分工作。调用 pathName.clean() 后,eden 空间下降,但分配的空间仍在增长,已达到最大值。
会对您的意见感兴趣 :) 谢谢
调用 writeDir(...)
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
progress(0);
XMLSaxLogic stax = new XMLSaxLogic(xmlPath);
DetectionFilter detectionFilter = new DetectionFilter(stax.getObjets());
try {
WriteFile writeFile = new WriteFile(detectionFilter.getDetectionList());
writeFile.writeDir(savedDirPath, detectionFilter.getHardwareList(), stax.getSiteName());
progress(100);
} catch (Exception a) {
System.out.println(a.getLocalizedMessage());
}
GetFileCount getFileCount = new GetFileCount(savedDirPath, detectionFilter.getDetectionList(), combo.getSelectedIndex());
getFileCount.getFile(savedDirPath.getAbsoluteFile().toString());
} catch (Exception a) {
System.out.println(a.getLocalizedMessage());
}
}
}
writeDir(...)
private ArrayList<String> detectList;
private String detection = null;
private String build = null;
private Set<String> pathName = new LinkedHashSet<>();
public WriteFile(ArrayList<String> detectList) {
this.detectList = detectList;
}
public void writeDir(File root, ArrayList<String> sevenElementList, ArrayList<String> oneElementList) {
for (String site : oneElementList) {
for (String s : sevenElementList) {
int indexx = s.indexOf("_");
int id = Character.getNumericValue(s.charAt(indexHardware - 1));
for (String detectionList : detectList) {
int index = detectionList.indexOf("_");
int sId = Character.getNumericValue(detectionList.charAt(index + 1));
if (detectionList.contains("Apple") && sId == id) {
detection = site.trim() + "/" + s + "/" + detectionList.trim();
pathName.add(format(detection));
} else if (detectionList.contains("Banana") && sId == id) {
build = detection.trim() + "/" + detectionList.trim();
pathName.add(format(build.trim()));
} else if (detectionList.contains("nananana") && sId == id) {
pathName.add(format(build.trim() + "/" + detectionList.trim()));
} else if (detectionList.contains("Watermelone") && sId == id) {
pathName.add(format(build.trim() + "/" + detectionList));
} else if (detectionList.contains("Orange") && sId == id) {
pathName.add(format(site.trim() + "/" + s.trim() + "/" + detectionList.trim()));
}
}
}
}
createDirTest(pathName, root);
}
private void createDirTest(Set<String> pathArray, File root) {
for (String s : pathArray) {
File subdir = new File(root, s);
subdir.mkdirs();
}
}
private String format(String toBeFormated) {
String toBeTrimmed = trimLastChar(toBeFormated.replace("ä", "ae").replace("ß", "ss").replace("ü", "ue").replace("ö", "oe").trim());
return toBeTrimmed;
}
【问题讨论】:
-
您介意发布调用 writeDir() 的代码吗?我怀疑您在循环过程中以某种方式修改了这些 for-each 列表中的一个(或多个)。但如果不查看调用该方法的位置/方式,我无法验证这种怀疑。
-
是的,我可以做到:)秒
-
还可以考虑替换 {site.trim() + "/" + s + "/" + detectionList.trim(); } 构造到
StringBuilder使用。在每次迭代调用stringBuilderInstance.setLength(0)后清除 StringBuilder。它将显着减少运行时分配的对象数量。 -
感谢您的建议,我一定会这样做的。感谢您提醒我存在 stringbuilder :)
标签: java file loops directory-structure mkdirs