我有一個非常大的雙打數組,我使用基於磁盤的文件和MappedByteBuffers的分頁列表來處理,請參閱this question瞭解更多背景信息。我在使用Java 1.5的Windows XP上運行。爲什麼使用Java MappedByteBuffers獲得「沒有足夠的存儲空間來處理此命令」?
這裏是我的代碼,不會對文件緩衝區分配的重要組成部分......
try
{
// create a random access file and size it so it can hold all our data = the extent x the size of a double
f = new File(_base_filename);
_filename = f.getAbsolutePath();
_ioFile = new RandomAccessFile(f, "rw");
_ioFile.setLength(_extent * BLOCK_SIZE);
_ioChannel = _ioFile.getChannel();
// make enough MappedByteBuffers to handle the whole lot
_pagesize = bytes_extent;
long pages = 1;
long diff = 0;
while (_pagesize > MAX_PAGE_SIZE)
{
_pagesize /= PAGE_DIVISION;
pages *= PAGE_DIVISION;
// make sure we are at double boundaries. We cannot have a double spanning pages
diff = _pagesize % BLOCK_SIZE;
if (diff != 0) _pagesize -= diff;
}
// what is the difference between the total bytes associated with all the pages and the
// total overall bytes? There is a good chance we'll have a few left over because of the
// rounding down that happens when the page size is halved
diff = bytes_extent - (_pagesize * pages);
if (diff > 0)
{
// check whether adding on the remainder to the last page will tip it over the max size
// if not then we just need to allocate the remainder to the final page
if (_pagesize + diff > MAX_PAGE_SIZE)
{
// need one more page
pages++;
}
}
// make the byte buffers and put them on the list
int size = (int) _pagesize ; // safe cast because of the loop which drops maxsize below Integer.MAX_INT
int offset = 0;
for (int page = 0; page < pages; page++)
{
offset = (int) (page * _pagesize);
// the last page should be just big enough to accommodate any left over odd bytes
if ((bytes_extent - offset) < _pagesize)
{
size = (int) (bytes_extent - offset);
}
// map the buffer to the right place
MappedByteBuffer buf = _ioChannel.map(FileChannel.MapMode.READ_WRITE, offset, size);
// stick the buffer on the list
_bufs.add(buf);
}
Controller.g_Logger.info("Created memory map file :" + _filename);
Controller.g_Logger.info("Using " + _bufs.size() + " MappedByteBuffers");
_ioChannel.close();
_ioFile.close();
}
catch (Exception e)
{
Controller.g_Logger.error("Error opening memory map file: " + _base_filename);
Controller.g_Logger.error("Error creating memory map file: " + e.getMessage());
e.printStackTrace();
Clear();
if (_ioChannel != null) _ioChannel.close();
if (_ioFile != null) _ioFile.close();
if (f != null) f.delete();
throw e;
}
我得到的標題提到的錯誤後,我分配在第二或第三緩衝。
我認爲這是與可用的連續內存有關,所以嘗試使用不同的頁面大小和數量,但沒有獲得整體收益。
究竟是什麼「沒有足夠的存儲空間來處理這個命令」意思是什麼,如果有的話,我能做些什麼?
我認爲MappedByteBuffers的重點在於能夠處理比您可以放在堆上更大的結構,並將它們當作它們在內存中一樣對待。
任何線索?
編輯:
針對以下(@adsk)的答案,我改變了我的代碼,所以我從來沒有多單主動MappedByteBuffer更在任何一個時間。當我引用當前未映射的文件的某個區域時,我會丟棄現有的地圖並創建一個新的地圖。在大約3次地圖操作後,我仍然遇到同樣的錯誤。
用GC引用的錯誤並未收集MappedByteBuffers,但在JDK 1.5中似乎仍然存在問題。
之前,你的結論是錯誤的GC仍然存在,需要通過一個內存使用分析器來運行你的代碼,以確保其不會掛到字節緩衝區引用。 – 2009-12-18 11:03:08