当仍有大量可用内存时抛出“System.OutOfMemoryException" [英] 'System.OutOfMemoryException' was thrown when there is still plenty of memory free

查看:38
本文介绍了当仍有大量可用内存时抛出“System.OutOfMemoryException"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

int size = 100000000;
double sizeInMegabytes = (size * 8.0) / 1024.0 / 1024.0; //762 mb
double[] randomNumbers = new double[size];

异常:抛出了System.OutOfMemoryException"类型的异常.

Exception: Exception of type 'System.OutOfMemoryException' was thrown.

我在这台机器上有 4GB 内存 2.5GB 空闲 当我开始运行时,PC 上显然有足够的空间来处理 762MB 的 100000000 个随机数.在给定可用内存的情况下,我需要存储尽可能多的随机数.当我开始生产时,盒子上会有 12GB,我想使用它.

I have 4GB memory on this machine 2.5GB is free when I start this running, there is clearly enough space on the PC to handle the 762mb of 100000000 random numbers. I need to store as many random numbers as possible given available memory. When I go to production there will be 12GB on the box and I want to make use of it.

CLR 是否将我限制为默认的最大内存?以及如何请求更多?

更新

我认为如果问题是由于内存碎片引起的,我认为将其分成更小的块并逐渐增加我的内存需求会有所帮助,但它不会我无法克服无论我如何调整 blockSize,总 ArrayList 大小为 256mb.

I thought breaking this into smaller chunks and incrementally adding to my memory requirements would help if the issue is due to memory fragmentation, but it doesn't I can't get past a total ArrayList size of 256mb regardless of what I do tweaking blockSize.

private static IRandomGenerator rnd = new MersenneTwister();
private static IDistribution dist = new DiscreteNormalDistribution(1048576);
private static List<double> ndRandomNumbers = new List<double>();

private static void AddNDRandomNumbers(int numberOfRandomNumbers) {
    for (int i = 0; i < numberOfRandomNumbers; i++) {
      ndRandomNumbers.Add(dist.ICDF(rnd.nextUniform()));                
  }
}

来自我的主要方法:

int blockSize = 1000000;

while (true) {
  try
  {
    AddNDRandomNumbers(blockSize);                    
  }
  catch (System.OutOfMemoryException ex)
  {
    break;
  }
}            
double arrayTotalSizeInMegabytes = (ndRandomNumbers.Count * 8.0) / 1024.0 / 1024.0;

推荐答案

您可能想阅读以下内容:"内存不足"不指物理内存",作者 Eric Lippert.

You may want to read this: ""Out Of Memory" Does Not Refer to Physical Memory" by Eric Lippert.

简而言之,非常简化,内存不足"并不意味着可用内存量太小.最常见的原因是在当前地址空间内,没有足够大的连续内存部分来提供所需的分配.如果您有 100 个块,每个 4 MB 大,那么当您需要一个 5 MB 块时,这对您没有帮助.

In short, and very simplified, "Out of memory" does not really mean that the amount of available memory is too small. The most common reason is that within the current address space, there is no contiguous portion of memory that is large enough to serve the wanted allocation. If you have 100 blocks, each 4 MB large, that is not going to help you when you need one 5 MB block.

要点:

  • 在我看来,我们称之为进程内存"的数据存储最好可视化为磁盘上的海量文件.
  • RAM 可以被视为仅仅是一种性能优化
  • 您的程序消耗的虚拟内存总量实际上与其性能没有太大关系
  • 内存不足"很少会导致内存不足"错误.它不是错误,而是导致性能不佳,因为存储实际上在磁盘上这一事实的全部成本突然变得相关.

这篇关于当仍有大量可用内存时抛出“System.OutOfMemoryException"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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