【发布时间】:2019-11-04 03:47:43
【问题描述】:
无法读取超过 500 万行的 csv 文件
环境:Java 8、opencsv -version 4.6、Lunix、MySQL
我的代码:
public static <T> List<T> parseCsvToBeanPosition(Class<T> clazz, Reader readerInput) {
ColumnPositionMappingStrategy ms = new ColumnPositionMappingStrategy();
ms.setType(clazz);
CsvToBean<T> csvToBean =
new CsvToBeanBuilder(readerInput)
.withType(clazz)
.withMappingStrategy(ms)
.build();
return csvToBean.parse();
}
2019-06-21 16:38:13 [http-nio-8085-exec-2] INFO SynchronizeBusiness - Start parse: (SynchronizeBusiness.java:223)
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000abc80000, 284688384, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 284688384 bytes for committing reserved memory.
# An error report file with more information is saved as:
【问题讨论】:
-
那么,您是否增加了 JVM 的最大堆大小?如果是这样,它是否超过了操作系统的总内存(物理 + 虚拟)?
-
你可以用
-Xmx1024m修复那些,它设置为1GB。 -
您必须将所有内容都保存在内存中吗?因为它是流式阅读的更好设计。例如使用 IterableCSVToBean 而不是 CsvToBean
-
你服务器的内存是多少?您的 CSV 文件大小是多少?