When parsing a large file I get the following error Caught: java.lang.OutOfMemoryError: Java heap space
How do you parse large files in Groovy without exceeding heap size?
example code that fails with large files...
import java.io.File
def inputFile = new File("c:/dev/test.txt")
inputFile.getText().eachLine{ it, i ->
... do something with each line
}
Ensure that the you're iterating over the file in a way that doesn't load the whole file into memory...
groovy -Xmx1024M myscript.groovy
See also answer here
See this page on the groovy mailing list for more info and further discussion
Code that works without a heap space error...
def inputFile = new File("c:/dev/test.txt")
inputFile.eachLine{ it, i ->
... do something with each line
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With