为什么会出现在我的C#应用​​程序内存异常的out? [英] Why am I getting an Out Of Memory Exception in my C# application?

查看:141
本文介绍了为什么会出现在我的C#应用​​程序内存异常的out?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的内存是4G的身体,但为什么我离开了内存异常,即使我只创建1.5G的内存对象。任何想法,为什么? (我同时看到,在任务管理器的性能选项卡内存不占用饱满,而且我也可以在这里输入 - 所以内存实际上并不低,所以我认为我击出了一些其他内存限制)?

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;命名空间TestBigMemoryv1
{
    类MemoryHolderFoo
    {
        静态随机种子=新的随机();
        公众的Int32 holder1;
        公众的Int32 holder2;
        公众的Int64 holder3;        公共MemoryHolderFoo()
        {
            从优化掉了// prevent
            holder1 =(Int32)已seed.NextDouble();
            holder2 =(Int32)已seed.NextDouble();
            holder3 =(Int64的)seed.NextDouble();
        }
    }    类节目
    {
        静态INT MemoryThreshold = 1500; //中号
        静态无效的主要(字串[] args)
        {
            INT persize = 16;
            INT数= MemoryThreshold * 1000 * 1000 / persize;
            MemoryHolderFoo [] =池新MemoryHolderFoo [数字];
            的for(int i = 0; I<数;我++)
            {
                池[i] =新MemoryHolderFoo();
                如果(I%10000 == 0)
                {
                    Console.Write(。);
                }
            }            返回;
        }
    }
}


解决方案

在一个正常的32位Windows应用程序,这个过程只可寻址内存为2GB。这是无关可用的物理内存量。

所以,2GB可用,但1.5是可以分配最大。关键是,你的code不是在这个过程中只有code运行。其他0.5 GB的可能是在这个过程中的CLR加碎片。

更新:在.NET 4.5中64位的过程中,你可以有较大的阵列,如果<一个href=\"http://msdn.microsoft.com/en-us/library/hh285054%28v=vs.110%29.aspx\">gcAllowVeryLargeObjects设置已启用:


  

在64位平台上,实现了在总规模超过2千兆字节(GB)阵列。
  元件的阵列中的最大数量是UInt32.MaxValue


 &LT;结构&gt;
  &LT;&运行GT;
    &LT; gcAllowVeryLargeObjects启用=真/&GT;
  &LT; /运行&GT;
&LT; /结构&gt;

My memory is 4G physical, but why I got out of memory exception even if I create just 1.5G memory object. Any ideas why? (I saw at the same time, in the performance tab of task manager the memory is not full occupied, and I could also type here -- so memory is not actually low, so I think I hit some other memory limitations)?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestBigMemoryv1
{
    class MemoryHolderFoo
    {
        static Random seed = new Random();
        public Int32 holder1;
        public Int32 holder2;
        public Int64 holder3;

        public MemoryHolderFoo()
        {
            // prevent from optimized out
            holder1 = (Int32)seed.NextDouble();
            holder2 = (Int32)seed.NextDouble();
            holder3 = (Int64)seed.NextDouble();
        }
    }

    class Program
    {
        static int MemoryThreshold = 1500; //M
        static void Main(string[] args)
        {
            int persize = 16;
            int number = MemoryThreshold * 1000 * 1000/ persize;
            MemoryHolderFoo[] pool = new MemoryHolderFoo[number];
            for (int i = 0; i < number; i++)
            {
                pool[i] = new MemoryHolderFoo();
                if (i % 10000 == 0)
                {
                    Console.Write(".");
                }
            }

            return;
        }
    }
}

解决方案

In a normal 32 bit windows app, the process only has 2GB of addressable memory. This is irrelevant to the amount of physical memory that is available.

So 2GB available but 1.5 is the max you can allocate. The key is that your code is not the only code running in the process. The other .5 GB is probably the CLR plus fragmentation in the process.

Update: in .Net 4.5 in 64 bit process you can have large arrays if gcAllowVeryLargeObjects setting is enabled:

On 64-bit platforms, enables arrays that are greater than 2 gigabytes (GB) in total size. The maximum number of elements in an array is UInt32.MaxValue.

<configuration>
  <runtime>
    <gcAllowVeryLargeObjects enabled="true" />
  </runtime>
</configuration>

这篇关于为什么会出现在我的C#应用​​程序内存异常的out?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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