我编写的一些代码似乎在内存方面会爆炸。
我不明白为什么,因为大多数对象都是在补充方法中创建的,并且我希望方法结束后空间会被释放。?!?(或者不是?)
我对内存消耗主题相当陌生。我不知道如何改进它。
通过添加标志 -Xmx8192 来配置 JVM 没有帮助。它只让我又处理了 3 个包裹。 (最初使用 -Xmx 标志处理了 27 个包,我达到了 30)
我可以以某种方式延迟它,以便给 GC 时间来释放空间吗? 或者这没有帮助?
这是我到目前为止编写的代码:
public class mainToTest{
public static void main(String [] args)throws IOException{ String str;
String home = "C:/Users/Eirini/Desktop/OP/";
String s = "13092017-1800";
File Descriptor;
final Charset ENCODING = StandardCharsets.UTF_8;
Path path = Paths.get(home+"output.xml");
List <String> LOP= new ArrayList();
LOP.clear();
List<String> lines;
int i,j;
File [] a =(new File(home+s)).listFiles();
System.out.println("Packages found...");
for (i=0; i<a.length; i++){
System.out.print("For package " + i);
Descriptor=findDescriptor(a[i]);
XSL.transformation(Descriptor,new File
(home+"extractprocedureid.xslt"));
lines = Files.readAllLines(path, ENCODING);
str=lines.get(1);
if (LOP.isEmpty()){
LOP.add(str);}
for(j=0; j<LOP.size(); j++){
if(!(str.equals(LOP.get(j)))){
LOP.add(str);}
}
}
System.out.println("");
System.out.println("Finished Procedures found:");
for (i=0; i<LOP.size();i++){
System.out.println(LOP.get(i)); }
}
public static File findDescriptor(File pckg){
String s;
int i,k;
int j=0;
File[] ind=pckg.listFiles();
System.out.println(" all Items and descriptor listed");
k=ind.length;
for (i=0;i<k;i++){
System.out.println("File " +i);
if (ind[i].getName().endsWith("_immc.xml")){
j=i;
i=200;
System.out.println("Descriptor found !!!!"); }
else{
System.out.println(" not a descriptor. Moving to the next");} }
return ind[j];
}
}
XSL.transformation 看起来像这样
public static void transformation (File immc,File xslt){
Source xmlInput = new StreamSource(immc);
Source xsl = new StreamSource(xslt);
Result xmlOutput = new StreamResult(new File("C:/Users/Eirini/Desktop/OP/output.xml"));
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer(xsl);
transformer.transform(xmlInput, xmlOutput);
}
catch (TransformerException e) {
System.out.println("Exception caught");
}
System.out.println("XSLT transformation finnished...The result can be found in file C:/Users/Eirini/Desktop/OP/output.xml");
}
错误通常发生在 XSL.transformation(第二段代码)之后
谢谢
最佳答案
看起来问题出在以下几行:
for(j=0; j<LOP.size(); j++){
if(!(str.equals(LOP.get(j)))){
LOP.add(str);}
}
每当你有一个新的 str 值时,这个片段就会使列表 LOP 的大小加倍。所以你的内存使用量呈指数级增长。
关于java.lang.OutOfMemory错误: Java heap space which objects are getting all the memory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46636697/