【发布时间】:2015-01-30 11:24:51
【问题描述】:
我正在尝试使用映射字节缓冲区读取文件,并在 buffer.position(position); 行收到 OutOfMemoryError:JavaHeapSpace。我不知道'不明白代码有什么问题。错误的原因可能是什么?
private void readFile()
{
int lastPos = buffer.getInt();
int dirLen = buffer.getInt();
byte[] dirBytes = new byte[dirLen];
buffer.get(dirBytes);
this.dir = new String(dirBytes);
int filePatternLen = buffer.getInt();
byte[] filePatternBytes = new byte[filePatternLen];
buffer.get(filePatternBytes);
this.filePattern = new String(filePatternBytes);
int numFileMetaDatas = 0;
while (buffer.position() < lastPos)
{
numFileMetaDatas++;
int position = buffer.position();
//Size is needed as we are reserving some extra bytes
int size = buffer.getInt();
buffer.position(position + ByteUtil.SIZE_OF_INT + ByteUtil.SIZE_OF_BYTE + ByteUtil.SIZE_OF_LONG + ByteUtil.SIZE_OF_LONG + ByteUtil.SIZE_OF_LONG);
int fileNameLen = buffer.getInt();
byte[] fileNameBytes = new byte[fileNameLen];
buffer.get(fileNameBytes);
FileMetaData fileMetaData = new FileMetaData(buffer, size, position);
UniqueID uniqID = fileMetaData.getUniqueID();
uniqIdVsFileMetaData.put(uniqID, fileMetaData);
position = position + size;
buffer.position(position);
}
nextMetaStartPos = lastPos;
}
【问题讨论】:
-
转到eclipse配置设置文件,即ini文件更改perm大小
-
因为代码不包含所有信息。这里只是猜测
int size = buffer.getInt();可能会返回一个巨大的整数。 -
你看过这个链接吗 mkyong.com/eclipse/…> 这个链接会有所帮助。我认为仅仅增加堆大小就可以解决问题。
-
@Rishikesh.. 增加堆大小不是解决方案,因为代码可以正常工作几个月。但我最近遇到了这个错误。
标签: java out-of-memory mappedbytebuffer