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

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

问题描述

我的内存是 4G 物理内存,但是为什么即使我只创建了 1.5G 内存对象也会出现内存不足异常.任何想法为什么?(我同时看到,在任务管理器的性能选项卡中,内存未满,我也可以在这里打字——所以内存实际上并不低,所以我想我遇到了其他一些内存限制)?

使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Text;命名空间 TestBigMemoryv1{类 MemoryHolderFoo{静态随机种子 = 新随机();public Int32 holder1;public Int32 holder2;public Int64 holder3;公共 MemoryHolderFoo(){//防止优化掉holder1 = (Int32)seed.NextDouble();holder2 = (Int32)seed.NextDouble();holder3 = (Int64)seed.NextDouble();}}课程计划{静态 int MemoryThreshold = 1500;//M静态无效主(字符串 [] 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();如果(我 % 10000 == 0){Console.Write(".");}}返回;}}}

解决方案

在普通的 32 位 Windows 应用程序中,进程只有 2GB 的可寻址内存.这与可用的物理内存量无关.

所以 2GB 可用,但 1.5 是您可以分配的最大值.关键是您的代码不是流程中运行的唯一代码.其他 0.5 GB 可能是 CLR 加上进程中的碎片.

更新:在 64 位进程的 .Net 4.5 中,如果 gcAllowVeryLargeObjects 设置已启用:

<块引用>

在 64 位平台上,启用总大小超过 2 GB 的数组.数组中元素的最大数量为 UInt32.MaxValue.

<预><代码><配置><运行时><gcAllowVeryLargeObjects enabled="true"/></运行时></配置>

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# 应用程序会出现内存不足异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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