如果仍然有足够的空闲内存'System.OutOfMemoryException的'被抛出 [英] 'System.OutOfMemoryException' was thrown when there is still plenty of memory free

查看:5531
本文介绍了如果仍然有足够的空闲内存'System.OutOfMemoryException的'被抛出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code:

  INT大小=亿;
双sizeInMegabytes =(大小* 8.0)/ 1024.0 / 1024.0; // 762 MB
双[] = randomNumbers新的双[尺寸]

例外:
类型的异常'System.OutOfMemoryException的'被抛出。

我有4GB内存的这台机器是2.5GB的免费当我开始这个运行,显然在PC上有足够的空间来处理亿随机数的762mb。我需要尽可能多的随机数存储尽可能给出可用内存。当我去生产会有12GB的盒子,我想使用它。

是否CLR限制我到默认最大内存下手?和我怎么要求更多?

更新

我以为这破成小块,并逐步添加到我的内存需求,如果该问题是由于内存碎片会有所帮助,但并不我不能让过去一个256MB的总规模的ArrayList不管我做什么调整块大小

 私有静态IRandomGenerator RND =新的梅森旋转算法();
私有静态IDistribution DIST =新DiscreteNormalDistribution(1048576);
私人静态列表<双> ndRandomNumbers =新的List<双>();私有静态无效AddNDRandomNumbers(INT numberOfRandomNumbers){
    的for(int i = 0; I< numberOfRandomNumbers;我++){
      ndRandomNumbers.Add(dist.ICDF(rnd.nextUniform()));
  }
}

从我的主要方法:

  INT块大小= 1000000;而(真){
  尝试
  {
    AddNDRandomNumbers(块大小);
  }
  赶上(前的System.OutOfMemoryException)
  {
    打破;
  }
}
双arrayTotalSizeInMegabytes =(ndRandomNumbers.Count * 8.0)/ 1024.0 / 1024.0;


解决方案

您可能需要阅读这一点:<一个href=\"http://blogs.msdn.com/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx\">Out内存由埃里克利珀并不是指物理内存。

在短,很简单,内存不足并不真正意味着可用内存量太小。最常见的原因是,目前的地址空间内,有一个存储器中没有连续部分,其足够大,以服务于所需分配。如果你有100块,每块4 MB的大,这是不会帮助你在您需要时5 MB区块。

要点:


  • 我们称之为进程的内存数据存储是我作为一个大量文件的最佳可视化在磁盘上的意见

  • RAM可被看作仅仅是一种性能优化

  • 您的程序占用的虚拟内存的总量是真的不其性能非常相关的

  • 运行的RAM很少导致一个内存不足的错误。相反,错误的,它会导致糟糕的性能,因为事实的全部成本的存储实际上是在磁盘上突然变得重要。

This is my code:

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

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

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.

Does the CLR constrain me to a default max memory to start with? and how do I request more?

Update

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()));                
  }
}

From my main method:

int blockSize = 1000000;

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

解决方案

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

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.

Key Points:

  • the data storage that we call "process memory" is in my opinion best visualized as a massive file on disk.
  • RAM can be seen as merely a performance optimization
  • Total amount of virtual memory your program consumes is really not hugely relevant to its performance
  • "running out of RAM" seldom results in an "out of memory" error. Instead of an error, it results in bad performance because the full cost of the fact that storage is actually on disk suddenly becomes relevant.

这篇关于如果仍然有足够的空闲内存'System.OutOfMemoryException的'被抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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