2

I'm getting a java.lang.OutOfMemoryError: Java heap space. I'm not sure what I'm doing wrong. Here is my code:

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 is 44.2 mb and it is a json file. Here is my error message.

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 is 'addString += nextLine' above.

Thank you in advance for your help!

2
  • 1
    Not sure if this the cause of the error or not but you should really use a StringBuilder, or StringBuffer if synchronization is a concern, instead of a String for addString. Strings are immutable so every appending requires allocating a new object and copying. Commented Jan 25, 2018 at 2:38
  • Oh, that's a good point! Let me give that a shot, should I create one string buffer and then clear that string buffer each time? Commented Jan 25, 2018 at 2:48

1 Answer 1

1

Thank you to twain249! After changing my code to this, my program runs quickly and it's perfect!

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();
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.