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

时间:2013-10-30 20:26:36

标签: c# asp.net-mvc memory 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);
}

2 个答案:

答案 0 :(得分:7)

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

<gcAllowVeryLargeObjects enabled="true" />

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

答案 1 :(得分:0)

在BeginUpdate()和EndUpdate()之间,垃圾收集为我解决了32位和64位的问题。 所以GC.Collect(); GC.WaitForPendingFinalizers();