Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何增加分配的内存?{由于内存不足异常,函数计算被禁用。}_C#_Asp.net Mvc_Memory_Memory Management_Generic Collections - Fatal编程技术网

C# 如何增加分配的内存?{由于内存不足异常,函数计算被禁用。}

C# 如何增加分配的内存?{由于内存不足异常,函数计算被禁用。},c#,asp.net-mvc,memory,memory-management,generic-collections,C#,Asp.net Mvc,Memory,Memory Management,Generic Collections,我的目标是使用C#ASP.NET MVC将148万个XML文件项加载到内存中的通用集合词典中,并将过滤器列表呈现给视图。现在,它在VS调试模式下以单个用户的身份处理字典中的大约120万项。如果出现任何内存不足错误-现在我得到:myDictionary.Count results in System.TypeInitializationException-“由于内存不足异常,函数求值被禁用。” 过去,在从XML加载到Dict的过程中,也有mscorlib.dll抱怨内存不足 我还有大量的系统内存,

我的目标是使用C#ASP.NET MVC将148万个XML文件项加载到内存中的通用集合词典中,并将过滤器列表呈现给视图。现在,它在VS调试模式下以单个用户的身份处理字典中的大约120万项。如果出现任何内存不足错误-现在我得到:myDictionary.Count results in System.TypeInitializationException-“由于内存不足异常,函数求值被禁用。”

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

我还有大量的系统内存,如何才能为这个MVC Web应用程序提供更多内存?顺便说一句,我尝试将XML转换成Perl字典,它可以很好地处理150万个对象——没问题。我不想用Perl编写我的应用程序。如果Perl能做到这一点,C#必须能够做到——我只是还没有找到搜索web的解决方案

示例代码:

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);
}
Dictionary masterDictionary=newdictionary();
foreach(文件路径中的字符串文件)
{
XDocument xdoc=XDocument.Load(文件);
Dictionary fileDictionary=xdoc
.后代(“msg”)
.ToDictionary(m=>m.Element(“msgId”).Value,
m=>新消息
{
msgId=m.Element(“msgId”).值,
msgType=m.Element(“msgType”).Value,
名称=m.Element(“名称”).值
});
//现在,将新值插入主控形状
foreach(fileDictionary中的var newValue)
masterDictionary.Add(newValue.Key,newValue.Value);
}

确实解决了问题。在调试模式下,VS 2013中的内存仍然限制为2GB。但是,当部署到IIS7.5、2008 R2服务器和应用程序时。如果是4.0x的dotnetpool,我可以为我的网站使用更多的内存。我的web.config中采用了以下设置。我现在需要将我的体能从8 GBs提高到16 GBs;但是,这是一个不同的故事。解决方案:

<gcAllowVeryLargeObjects enabled="true" />

在BeginUpdate()和EndUpdate()之间,垃圾收集用32位和64位修复了我的问题。 GC.Collect();
GC.WaitForPendingFinalizers()

您不会显示任何代码,让其他人知道您正试图加载多少内存,因此.Net运行时可以使用大量内存—在某些情况下可能需要几百MB。我知道你以前从来没有获得过超过2GB的CLR,即使在使用64位windows时,也不知道在最新的CLR核心版本中是否是这样。运行performance Monditory时,我似乎有大约10 GB的空闲空间。@GaryWalker-不清楚你的评论“从未获得超过2GB”是什么意思,但x86/x64都不允许单个分配大于2GB(除此之外)。在32位上,您应该能够使用最多4GB的内存(假设您的操作系统是x64,但进程是x86),在x64上,您不应该因为分配的内存量而出现OOM异常,而只是因为大小。我相信asp.net堆栈在2GB进程中运行,并且堆栈本身可以使用几百兆内存。