Java NIO MappedByteBuffer OutOfMemoryException [英] Java NIO MappedByteBuffer OutOfMemoryException

查看:213
本文介绍了Java NIO MappedByteBuffer OutOfMemoryException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很麻烦:我想用 FileChannel s和 MappedByteBuffer s来读取超过几GB的巨大文件 - 我发现的所有文档都意味着使用 FileChannel.map()方法映射文件相当简单。
当然,有2GB的限制,因为所有的Buffer方法都使用int来表示位置,限制和容量 - 但是系统默认的限制低于这个限制吗?

实际上,我发现很多关于 OutOfMemoryException s的问题!并没有任何文件,真正定义的限制!
所以 - 我怎样才能将一个符合int限制的文件安全地映射到一个或几个 MappedByteBuffer s中,而不会只是得到异常?



在尝试 FileChannel.map()之前,我可以问系统文件的哪个部分可以安全映射吗?怎么样?
为什么有这么少的文档关于这个功能?

解决方案

文件越大,所需的越少全部在内存中。设计一种方式来一次处理一个缓冲区的文件,一次一行,等等。

MappedByteBuffers特别有问题,因为映射没有定义的版本内存,所以一次使用多个基本上是必然的。


I am really in trouble: I want to read HUGE files over several GB using FileChannels and MappedByteBuffers - all the documentation I found implies it's rather simple to map a file using the FileChannel.map() method. Of course there is a limit at 2GB as all the Buffer methods use int for position, limit and capacity - but what about the system implied limits below that?

In reality, I get lots of problems regarding OutOfMemoryExceptions! And no documentation at all that really defines the limits! So - how can I map a file that fits into the int-limit safely into one or several MappedByteBuffers without just getting exceptions?

Can I ask the system which portion of a file I can safely map before I try FileChannel.map()? How? Why is there so little documentation about this feature??

解决方案

The bigger the file, the less you want it all in memory at once. Devise a way to process the file a buffer at a time, a line at a time, etc.

MappedByteBuffers are especially problematic, as there is no defined release of the mapped memory, so using more than one at a time is essentially bound to fail.

这篇关于Java NIO MappedByteBuffer OutOfMemoryException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆