Java循环读取文件导致内存溢出!
在主函数的循环中,创建一个ReadNodesEdges类对象并处理,当进入下次循环后前一项创建的对象不是应该会被自动GC么。打开GC显示后,老年代内存随着每次循环不断加大,到固定位置后报错——“java.lang.OutOfMemoryError: GC overhead limit exceeded”。
求解应该怎么处理问题??
public class Main_test {
public static void main(String[] args) {
File[] FileList = new File("D:/Samples/B_func_binary/").listFiles();// 获取目录下所有子目录对象
for (int i = 0; i < FileList.length; i++) {
if (FileList[i] != null) {
System.out.println(FileList[i].getAbsolutePath());// getAbsolutePath()绝对地址
ReadNodesEdges readNodesEdges = new ReadNodesEdges();
//问题出现在这个类中
readNodesEdges.script("D:/Samples/B_gexf/"+ FileList[i].getName().replaceAll("txt", "gexf"));// getName()文件名
}
}
}
}
class ReadNodesEdges {
public void script(String path) {
//Init a project - and therefore a workspace
ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
pc.newProject();
Workspace workspace = pc.getCurrentWorkspace();
//Get controllers and models
ImportController importController = Lookup.getDefault().lookup(ImportController.class);
//Import file
Container container;
try {
File file = new File(path);
container = importController.importFile(file);
container.getLoader().setEdgeDefault(EdgeDefault.DIRECTED); //Force DIRECTED
container.setAllowAutoNode(false); //Don't create missing nodes
} catch (Exception ex) {
ex.printStackTrace();
return;
}
//这行是关键所在,去掉这行则没有溢出,但是也无法读取数据
importController.process(container, new DefaultProcessor(), workspace);
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();
importController = null;
}
}