I have two large CSV files which contain data that is required for users of a web application to validate some info. I defined an ArrayList< String[] > and intended to keep the contents of both files in memory so I wouldn't have to read them each time a user logged in and used the application.

I'm getting a java.lang.OutOfMemoryError: Java heap space, though, when initializing the application and trying to read the second file. (It finishes reading the first file just fine but hangs when reading the second file and after a while I get that exception)

The code for reading the files is pretty straight forward:

```
ArrayList<String[]> tokenizedLines = new ArrayList<String[]>();

public void parseTokensFile() throws Exception {
    BufferedReader bRead = null;
    FileReader fRead = null;

    try {
        fRead = new FileReader(this.tokensFile);
        bRead = new BufferedReader(fRead);
        String line;
        while ((line = bRead.readLine()) != null) {
            tokenizedLines.add(StringUtils.split(line, fieldSeparator));
        }
    } catch (Exception e) {
        throw new Exception("Error parsing file.");
    } finally {
        bRead.close();
        fRead.close();
    }
}

```

I read Java's split function could use up a lot of memory when reading large amounts of data since the substring function makes a reference to the original string, so a substring of some String will use up the same amount of memory as the original, even though we only want a few chars, so I made a simple split function to try avoiding this:

```
public String[] split(String inputString, String separator) {
    ArrayList<String> storage = new ArrayList<String>();
    String remainder = new String(inputString);
    int separatorLength = separator.length();
    while (remainder.length() > 0) {
        int nextOccurance = remainder.indexOf(separator);
        if (nextOccurance != -1) {
            storage.add(new String(remainder.substring(0, nextOccurance)));
            remainder = new String(remainder.substring(nextOccurance +  separatorLength));
        } else {
            break;
        }
    }

    storage.add(remainder);
    String[] tokenizedFields = storage.toArray(new String[storage.size()]);
    storage = null;

    return tokenizedFields;

}

```

This gives me the same error though, so I'm wondering if it's not a memory leak but simply that I can't have structures with so many objects in memory. One file is about 600'000 lines long, with 5 fields per line, and the other is around 900'000 lines long with about the same amount of fields per line.

The full stacktrace is:

```
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at xxx.xxx.xxx.StringUtils.split(StringUtils.java:16)
    at xxx.xxx.xxx.GFTokensFile.parseTokensFile(GFTokensFile.java:36)

```

So, after the long post (sorry :P), is this a restriction of the amount of memory assigned to my JVM or am I missing something obvious and wasting resources somewhere?