0

I am building recursive function that go through a million line or so, I get stackOverFlow from this function during execution.

protected String[] getConnectedCities(String line) {
    return line.trim().toLowerCase().replace(DELIMITER + " ", DELIMITER)
            .split(DELIMITER);
}

This is the full code:

    protected final Map<String, City> processLine(
        final Map<String, City> dataMap) {
    try {
        String line = "";
        if ((line = bReader.readLine()) == null) {
            return dataMap;
        }
        // Check if direct relation can be found
        String connectedCities[] = parseLine(line);
        line = null;
        saveConnection(dataMap, connectedCities);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return processLine(dataMap);
}

I am not sure what am I doing wrong, I think it is related to the String line but am not quite sure what is it.

Thanks.

4
  • if this is recursing a million levels down then not un-expected to get stack overflow eventually, would need very large stack memory area to handle. Do you really mean to use recursion rather than just read the file line at a time in a loop and process each one? Commented Jun 26, 2014 at 14:13
  • Thanks dethorpe for your respond, well I was thinking to reduce the processing time to log n, otherwise it takes long time to process the whole file. Commented Jun 26, 2014 at 15:25
  • I doubt recursing like this would be faster than simply looping through the file. Maybe could try reading larger chunks of the file in then looping through them, could possible use multiple threads to process the chunks in parrallel. Commented Jun 26, 2014 at 16:08
  • Actually the problem is not reading the file, I use MappedBufferByte to load the file into memory. My main problem is how to process it in more efficient way. File is around 200 MB of line after line with two words per line separated by ',' Commented Jun 26, 2014 at 16:12

1 Answer 1

1

The last thing you do, you're calling processLine agan. This will have as many levels of recusion as you have lines in file, until return statement above bails out. This is technically tail recursion, but Java may not understand that.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer Arkediy, well my main concern here is to reduce processing time of the file to the minimum. I think I need to refactor the code more.

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.