解析大文件时,我收到以下错误Caught: java.lang.OutOfMemoryError: Java heap space
如何在不超出堆大小的情况下解析Groovy中的大文件?
大文件失败的示例代码......
import java.io.File
def inputFile = new File("c:/dev/test.txt")
inputFile.getText().eachLine{ it, i ->
... do something with each line
}
答案 0 :(得分:17)
确保您以不会将整个文件加载到内存中的方式迭代文件...
groovy -Xmx1024M myscript.groovy
扩展到1GB请参阅answer here 请参阅groovy邮件列表中的page以获取更多信息和进一步讨论
没有堆空间错误的代码......
def inputFile = new File("c:/dev/test.txt")
inputFile.eachLine{ it, i ->
... do something with each line
}