如何增加分配的内存?{由于内存不足异常,功能评估被禁用.}

Bil*_*ill 5 c# memory asp.net-mvc memory-management generic-collections

我的目标是使用C#ASP.NET MVC从XML文件加载148万个项目到内存通用集合字典,并能够将过滤器列表呈现给视图.现在它在VS调试模式下作为单个用户在我的字典中使用大约50万个项目.我得到了更多的内存错误 - 现在我得到:myDictionary.Count导致System.TypeInitializationException - '由于内存不足异常,函数评估已被禁用.

在过去,从XML到Dict的加载过程中也出现了mscorlib.dll,它抱怨了内存不足.

我还有足够的系统内存,如何为这个MVC Web应用程序提供更多内容?顺便说一句,我尝试使用XML到Perl词典,它可以很好地处理150万个对象 - 没问题.我不想在Perl中编写我的应用程序.如果Perl可以做到这一点,C#必须能够做到 - 我还没有找到一个搜索网络的解决方案.

示例代码:

Dictionary<string, msg> masterDictionary = new Dictionary<string, mgs>();

foreach (string file in filePath)
{
    XDocument xdoc = XDocument.Load(file);
    Dictionary<string, msg> fileDictionary = xdoc
        .Descendants("msg")
        .ToDictionary(m => m.Element("msgId").Value,
                      m => new msg
                           {
                               msgId = m.Element("msgId").Value,
                               msgType = m.Element("msgType").Value,
                               name = m.Element("name").Value
                           });

    //now insert your new values into the master
    foreach(var newValue in fileDictionary)
        masterDictionary.Add(newValue.Key, newValue.Value);
}
Run Code Online (Sandbox Code Playgroud)

Bil*_*ill 7

这个DID修复了这个问题.在调试模式下,我仍然限制在VS 2013中的2 GB内存.但是,在部署到IIS7.5,2008 R2 Server和App时.点网4.0x的池,我可以为我的网站使用更多的内存.它在我的web.config中进行了以下设置.我现在需要将物理性能从8 GB提升到16 GB; 但是,这是一个不同的故事.解:

<gcAllowVeryLargeObjects enabled="true" />
Run Code Online (Sandbox Code Playgroud)

https://msdn.microsoft.com/en-us/library/hh285054(v=vs.110).aspx