java - 为什么使用 Java MappedByteBuffers 会得到 "Not enough storage is available to process this command"?

标签 java memory-mapped-files

我有一个非常大的 double 组,我正在使用基于磁盘的文件和 MappedByteBuffers 的分页列表来处理,请参阅 this question了解更多背景。我正在使用 Java 1.5 在 Windows XP 上运行。

这是我的代码的关键部分,它针对文件分配缓冲区...

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),我更改了代码,因此我在任何时候都不会拥有超过一个 Activity 的 MappedByteBuffer。当我引用当前未映射的文件区域时,我会废弃现有映射并创建一个新映射。大约 3 次 map 操作后我仍然遇到同样的错误。

GC 引用的错误不收集 MappedByteBuffers 似乎仍然是 JDK 1.5 中的一个问题。

最佳答案

I thought the point of MappedByteBuffers was the ability to be able to handle structures larger than you could fit on the heap, and treat them as if they were in memory.

没有。这个想法是允许您寻址超过 2**31 个 double ...假设您有足够的内存并且使用 64 位 JVM。

(我假设这是 this question 的后续问题。)

编辑:显然,需要更多解释。

存在许多限制。

  1. Java 有一个基本限制:length数组的属性,数组索引的类型为 int 。这与 int 的事实相结合有符号并且数组不能有负大小意味着最大可能的数组可以有 2**31元素。此限制适用于 32 位和 64 位 JVM。它是 Java 语言的基本组成部分...就像 char值来自 065535 .

  2. 使用 32 位 JVM 的(理论)上限为 2**32 JVM 可寻址的字节数。这包括整个堆、您的代码和您使用的库类、JVM 的 native 代码核心、用于映射缓冲区的内存……一切。 (事实上​​,根据您的平台,操作系统可能为您提供的地址空间远少于 2**32 字节。)

  3. 您在 java 命令行上提供的参数决定了 JVM 将允许您的应用程序使用多少堆内存。内存映射到使用 MappedByteBuffer对象不计入此。

  4. 操作系统提供的内存量取决于(在 Linux/UNIX 上)配置的交换空间总量、“进程”限制等。类似的限制可能也适用于 Windows。当然,如果主机操作系统支持 64 位,并且您使用的是支持 64 位的硬件,则只能运行 64 位 JVM。 (如果你有奔腾,那你就运气不好了。)

  5. 最后,系统中的物理内存量也会发挥作用。理论上,您可以要求 JVM 使用比您的机器物理内存大很多倍的堆等。实际上,这是一个坏主意。如果您过度分配虚拟内存,您的系统将会崩溃,应用程序性能也会下降。

要点是:

  • 如果您使用 32 位 JVM,您可能会被限制在 2**31 之间的某个位置。和2**32可寻址内存字节。对于 2**29 之间的最大值来说,这个空间足够了。和2**30 double ,无论您使用数组还是映射缓冲区。

  • 如果您使用 64 位 JVM,则可以表示 2**31 的单个数组。 double 。映射缓冲区的理论限制为 2**63字节或 2**61双倍,但实际限制大约是您机器拥有的物理内存量。

关于java - 为什么使用 Java MappedByteBuffers 会得到 "Not enough storage is available to process this command"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1927123/

相关文章:

java - 如何使用正则表达式将最后一个字母替换为java中的另一个字母

java - 如何从注释处理器中的注释值捕获枚举

c# - 具有单个编写器的 MemoryMappedFile 是否需要同步

c# - 1 字节对齐会导致内存损坏吗?

java映射FileChannel实现

java - AP CS 实践 - OOP

java - 如何将硬编码枚举转换为不同的语言?

java.lang.NullPointerException 浓度游戏

c - 如何在C(Windows)中访问父进程创建的内存映射文件

C 内存映射文件与常规文件