如何修复这个java.lang.OutOfMemoryError:java堆空间?

如何修复这个java.lang.OutOfMemoryError:java堆空间?,java,bufferedreader,stringbuffer,Java,Bufferedreader,Stringbuffer,我得到一个java.lang.OutOfMemoryError:java堆空间。我不确定我做错了什么。这是我的密码: StringBuffer finalString = new StringBuffer(); try { BufferedReader br = new BufferedReader(new FileReader("collegeData 2.txt")); StringBuffer returnFile = new StringBuff

我得到一个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();
    }
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();
    }
StringBuffer finalString=new StringBuffer();
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(“collegeData 2.txt”);
StringBuffer returnFile=新的StringBuffer();
returnFile.append(br.readLine()+“\n”+br.readLine()+”\n”);
布尔completedFirstSection=false;
整数计数=2;
StringBuffer addString=新的StringBuffer();
字符串下一行;
而((nextLine=br.readLine())!=null){
如果(计数<990){
addString.append(nextLine+“\n”);
计数++;
}否则{
String sub=nextLine.substring(16);
sub=sub.substring(0,sub.length());
//addString=sub+“:{“+nextLine+”,“+addString.substring(0,addString.length()-2)+br.readLine();
append(sub+“:{+nextLine+”,“+”\n”);
returnFile.append(addString.substring(0,addString.length()-2)+“\n”);
returnFile.append(br.readLine()+“\n”);
addString.setLength(0);
计数=2;
br.readLine();
}
}
returnFile.append(“}}]”;
finalString=returnFile;
br.close();
}捕获(IOE异常){
e、 printStackTrace();
}

不确定这是否是错误的原因,但如果需要同步,您应该使用
StringBuilder
,或者
StringBuffer
,而不是
addString
String
<代码>字符串s是不可变的,因此每次追加都需要分配一个新对象并进行复制。哦,这是一个很好的观点!让我试一试,我是否应该创建一个字符串缓冲区,然后每次清除该字符串缓冲区?
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();
    }