gpt4 book ai didi

java - 我该如何解决这个 java.lang.OutOfMemoryError : Java heap space?

转载 作者:行者123 更新时间:2023-11-30 10:19:49 27 4
gpt4 key购买 nike

我收到 java.lang.OutOfMemoryError:Java 堆空间。我不确定我做错了什么。这是我的代码:

StringBuffer finalString = new StringBuffer();

try {
BufferedReader br = new BufferedReader(new FileReader("collegeData 2.txt"));
StringBuffer returnFile = new StringBuffer();
returnFile.append(br.readLine() + br.readLine());
ArrayList<String> allData = new ArrayList<String>();

boolean completedFirstSection = false;
int count = 2;
String addString = "";

String nextLine;

while ((nextLine=br.readLine())!=null) {
if(count < 990) {
addString += nextLine;
count++;
} else {
String sub = nextLine.substring(16);
sub = sub.substring(0, sub.length());

addString = sub + ":{" + nextLine + "," + addString.substring(0, addString.length()-2) + br.readLine();
br.readLine();
allData.add(addString);
count = 2;
}
}
allData.add("}}]");
System.out.println("here");
System.out.println(returnFile.toString());
finalString = returnFile;
br.close();
} catch (IOException e) {
e.printStackTrace();
}

collegeData 2.txt 是 44.2 mb,它是一个 json 文件。这是我的错误消息。

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.base/jdk.internal.misc.Unsafe.allocateUninitializedArray(Unsafe.java:1250)
at java.base/java.lang.invoke.StringConcatFactory$MethodHandleInlineCopyStrategy.newArray(StringConcatFactory.java:1605)
at java.base/java.lang.invoke.DirectMethodHandle$Holder.invokeStatic(DirectMethodHandle$Holder)
at java.base/java.lang.invoke.LambdaForm$BMH/754666084.reinvoke(LambdaForm$BMH)
at java.base/java.lang.invoke.LambdaForm$MH/801197928.linkToTargetMethod(LambdaForm$MH)
at RunnerCombined.main(RunnerCombined.java:31)

RunnerCombined.java:31 是上面的 'addString += nextLine'。

预先感谢您的帮助!

最佳答案

感谢 twain249!将我的代码改成这样后,我的程序运行很快,非常完美!

StringBuffer finalString = new StringBuffer();

try {
BufferedReader br = new BufferedReader(new FileReader("collegeData 2.txt"));
StringBuffer returnFile = new StringBuffer();
returnFile.append(br.readLine() + "\n" + br.readLine() + "\n");

boolean completedFirstSection = false;
int count = 2;
StringBuffer addString = new StringBuffer();

String nextLine;

while ((nextLine=br.readLine())!=null) {
if(count < 990) {
addString.append(nextLine + "\n");
count++;
} else {
String sub = nextLine.substring(16);
sub = sub.substring(0, sub.length());

// addString = sub + ":{" + nextLine + "," + addString.substring(0, addString.length()-2) + br.readLine();
returnFile.append(sub + ":{" + nextLine + "," + "\n");
returnFile.append(addString.substring(0, addString.length()-2) + "\n");
returnFile.append(br.readLine() + "\n");
addString.setLength(0);
count = 2;
br.readLine();
}
}
returnFile.append("}}]");
finalString = returnFile;
br.close();
} catch (IOException e) {
e.printStackTrace();
}

关于java - 我该如何解决这个 java.lang.OutOfMemoryError : Java heap space?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48434750/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com