嘿,我在我的spring引导应用程序中使用Java 8。我正在通过HTTP调用从apache服务器获取数据。数据大小有时超过100 MB (超过100 000行)。我需要把这些数据转换成字符串列表。对于下面的较小数据,代码是有效的,但是对于更大的数据,它提供了
java.lang.OutOfMemoryError: Java堆空间
这就是我如何将数据转换成字符串列表的方式。
List<String> lines = null;
try {
String data = con.sendGet();
if(data==null)
throw new UserAuthException("diff is not available at the location");
else {
diff.setLineAsString(data);
lines = IOUtils.readLines(new StringReader(data));
System.out.println("lines = IOUtils.readLines(new StringReader(data));");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} IOUtils在内存中运行吗?阅读它的有效方法是什么?
发布于 2020-07-02 06:51:44
让我们说出最大的细节。
内存中的读取()
读取文件行的标准方法是在内存中--无论是番石榴还是都提供了一种快速的方法来做到这一点:
Files.readLines(new File(path), Charsets.UTF_8);
FileUtils.readLines(new File(path));这种方法的问题是,所有文件行都保存在内存中--如果文件足够大,这将很快导致OutOfMemoryError。
例如,-读取一个~1Gb文件:
@Test
public void givenUsingGuava_whenIteratingAFile_thenWorks() throws IOException {
String path = ...
Files.readLines(new File(path), Charsets.UTF_8);
}这从消耗少量内存开始:(~0 Mb消耗)
主信息org.baeldung.java.CoreJavaIoUnitTest -总内存: 128 Mb 主信息org.baeldung.java.CoreJavaIoUnitTest -空闲内存: 116 Mb
然而,在完整的文件被处理之后,我们在结尾处有:(~2GB消耗)
主信息org.baeldung.java.CoreJavaIoUnitTest -总内存: 2666 Mb 主信息org.baeldung.java.CoreJavaIoUnitTest -空闲内存: 490 Mb
这意味着进程消耗了大约2.1GB的内存--原因很简单--文件行现在都存储在内存中。
这一点应该很明显,保存在内存中的文件内容将很快耗尽可用的内存--不管这实际上是多少。
更重要的是,我们通常不需要在内存中同时使用中的所有行-相反,我们只需要能够迭代每一行,做一些处理,并丢弃它。所以,这正是我们要做的--在没有将它们全部保存在内存中的情况下,迭代这些行。
流经文件
现在让我们来看看一个解决方案--我们将使用一个java.util.Scanner来遍历文件的内容并依次检索行:
FileInputStream inputStream = null;
Scanner sc = null;
try {
inputStream = new FileInputStream(path);
sc = new Scanner(inputStream, "UTF-8");
while (sc.hasNextLine()) {
String line = sc.nextLine();
// System.out.println(line);
}
// note that Scanner suppresses exceptions
if (sc.ioException() != null) {
throw sc.ioException();
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (sc != null) {
sc.close();
}
}此解决方案将遍历文件中的所有行-允许处理每一行-而不保留对它们的引用-最后,不将它们保存在内存中:(~150 Mb消耗)。
主信息org.baeldung.java.CoreJavaIoUnitTest -总内存: 763 Mb 主信息org.baeldung.java.CoreJavaIoUnitTest -空闲内存: 605 Mb
使用Apache 的流
通过使用库提供的custom LineIterator,也可以使用Commons库实现同样的目的:
LineIterator it = FileUtils.lineIterator(theFile, "UTF-8");
try {
while (it.hasNext()) {
String line = it.nextLine();
// do something with line
}
} finally {
LineIterator.closeQuietly(it);
}由于整个文件没有完全存储在内存中-这也会导致相当保守的内存消耗数字:(~150 Mb消耗)
主信息o.b.java.CoreJavaIoIntegrationTest -总内存: 752 Mb 主信息o.b.java.CoreJavaIoIntegrationTest -无内存: 564 Mb
代码片段可用的这里
发布于 2020-07-02 06:51:08
通常,在使用足够大的文件时,您将得到一个OutOfMemoryError。
如果您想将更大的文件转换为List<String>,有多种方法,
1. Loading a Binary File in Chunks
try(BufferedInputStream in = new BufferedInputStream(new FileInputStream(pathname)))
{
byte[] bbuf = new byte[4096];
int len;
while ((len = in.read(bbuf)) != -1) {
// process data here: bbuf[0] thru bbuf[len - 1]
}
}通过readLine
2. Reading a Text File Line By Line
try(BufferedReader in = new BufferedReader(new FileReader(pathname))) {
String line;
while ((line = in.readLine()) != null) {
// process line here.
}
}通过扫描仪,
3. Using a Scanner
try(Scanner scanner = new Scanner(new File(pathname))) {
while ( scanner.hasNextLine() ) {
String line = scanner.nextLine();
// process line here.
}
}穿过溪流,
4. With Java 8 Streams
List<String> alist = Files.lines(Paths.get(pathname))
.collect(Collectors.toList());您也可以引用这个链接
https://stackoverflow.com/questions/62690396
复制相似问题