Search code examples
javafor-looplinked-listout-of-memory

Java / JavaFX - java.lang.OutOfMemoryError in a short and relatively simple programm


I am currently working on a JavaFX application and am trying to implement a function that converts a string of any length, which contains numbers separated by empty spaces, into integers. I am pretty sure the function I wrote should work, although for some reason, I am getting "java.lang.OutOfMemoryError" errors.

I am using Java and JavaFX version 17!

My Function:

// Create an empty LinkedList to store the converted integers.
LinkedList<Integer> inputList = new LinkedList<>();


public void StringtoInteger(String strInput) {
    int emptySpace = 0; // This variable keeps track of the index of the empty space.
    int endString = strInput.length(); // The size of the input string.
    String str; // Temporary string variable to hold substrings.

    // Iterate over the input string to extract substrings.
    for(int x=0; x<endString; x = emptySpace + 1) {
        emptySpace = strInput.indexOf(" ", x); // Find the index of the next empty space.

        if (emptySpace != -1) {
            str = strInput.substring(x, emptySpace); // Extract the substring from the start to the empty space.
        } else {
            str = strInput.substring(x); // If no empty space is found, extract the remaining substring.
        }

        inputList.add(Integer.parseInt(str.trim())); // Convert the substring to an integer and add it to the LinkedList.
    }
}

My Main is as follows:

public static void main(String[] args) {
    String testHeap = "5 6 1 7 12 21 58";
    StringtoInteger(testHeap);
    System.out.println(inputList);
}

The full Error message is as follows

ERROR!
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.base/java.util.Arrays.copyOfRange(Arrays.java:4030)
at java.base/java.lang.StringLatin1.newString(StringLatin1.java:715)
    at java.base/java.lang.String.substring(String.java:1879)
    at ExampleClass.stringToInteger(ExampleClass.java:15)
    at ExampleClass.main(ExampleClass.java:26)

I tried reallocating the memory my program uses, but it just keeps crashing or showing me the error. If I run the program in IntelliJ, it crashes. If I run the function explicitly in a separate Java environment, it shows the error message.

Thanks for any help beforehand.


Solution

  • The problem arises because you don't terminate the loop when there are no more spaces in the string.

    When this happens, as your code acknowledges with the if (emptySpace != -1) clause, emptySpace will be -1. The loop modification clause x = emptySpace + 1 then sets x to 0, and the process starts from the beginning of the string. Consequently, you just add elements to the list indefinitely.

    This is a good example of trying to reinvent potentially tricky algorithms that are already implemented by the API. You should always use API methods where possible. String.split() already implements splitting a string into parts at a delimiter:

    public void stringtoInteger(String strInput) {
        for (String str : strInput.split(" ")) {
            inputList.add(Integer.parseInt(str.trim())); // Convert the substring to an integer and add it to the LinkedList.
        }
    }