内存不足使用扫描仪将大文件读入内存 [英] Out of memory using Scanner to read large file into memory

查看:33
本文介绍了内存不足使用扫描仪将大文件读入内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将一个大文件传递给扫描仪时,以下代码块会引发 java.lang.OutOfMemoryError 异常.解决此问题的最佳方法是什么?是arraylist的问题还是scanner的问题?

The following block of code is throwing a java.lang.OutOfMemoryError exception when I pass a large file into the scanner. What is the best way to fix this? Is the problem in the arraylist or the scanner?

ArrayList rawData = new ArrayList();
Scanner scan = new Scanner(file);

while (scan.hasNext()) {
    String next = scan.next();
        rawData.add(next);
}

推荐答案

增加java堆大小,例如

Increase the java heap size, e.g.

java -Xmx6g myprogram

将堆大小设置为 6 GB.当然总会有限制的....

will set the heap size to 6 gigabytes. Of course there will always be a limit....

这篇关于内存不足使用扫描仪将大文件读入内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆