我正在研究从文件读取大量数据的应用程序。基本上,我有一个巨大的文件(大约1.5 - 2演出)包含不同的对象(约5至10百万它们每个文件)。我需要阅读所有这些文件,并将它们放到应用程序中的不同地图上。问题是应用程序在某些时候读取对象时内存不足。只有当我将它设置为使用-Xmx4096m时,它才能处理文件。但是,如果文件会更大,它将无法再做到这一点。如何在读取Java中的大文件时避免OutOfMemory异常
下面的代码片段:
所有的String sampleFileName = "sample.file";
FileInputStream fileInputStream = null;
ObjectInputStream objectInputStream = null;
try{
fileInputStream = new FileInputStream(new File(sampleFileName));
int bufferSize = 16 * 1024;
objectInputStream = new ObjectInputStream(new BufferedInputStream(fileInputStream, bufferSize));
while (true){
try{
Object objectToRead = objectInputStream.readUnshared();
if (objectToRead == null){
break;
}
// doing something with the object
}catch (EOFException eofe){
eofe.printStackTrace();
break;
} catch (Exception e) {
e.printStackTrace();
continue;
}
}
} catch (Exception e){
e.printStackTrace();
}finally{
if (objectInputStream != null){
try{
objectInputStream.close();
}catch (Exception e2){
e2.printStackTrace();
}
}
if (fileInputStream != null){
try{
fileInputStream.close();
}catch (Exception e2){
e2.printStackTrace();
}
}
}
首先,我用的是objectInputStream.readObject()的代替objectInputStream.readUnshared(),所以它解决了部分问题。当我将内存从2048增加到4096时,它开始解析文件。 BufferedInputStream已被使用。从网上我发现只有例子如何读取行或字节,但没有关于对象,性能明智。
如何在不增加JVM内存的情况下读取文件并避免出现OutOfMemory异常?有没有办法从文件中读取对象,而不是在内存中保留其他任何东西?
这是简单的物理:更大的文件将需要更多的内存。那里没有魔法。您的文件不包含对象 - 它们包含映射到映射到对象的字符串的字节。 – duffymo
如果您可以在读取主文件时对数据进行排序,则可以使用BufferReader按行读取文件,然后使用PrintWriter将数据追加到已存在的文件或创建新文件。 – Jure
如果文件太大,别无选择,只能将它们存储在F.S.中。阅读:https://commons.apache.org/proper/commons-jcs/ –