简体   繁体   English

C#异常OutOfMemory

[英]C# exception OutOfMemory

I would need some help how to handle this exception: 我需要一些帮助来处理该异常:

StackTrace :   at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
   at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.Configuration.GetSection(String sectionName)
   at System.Configuration.ClientSettingsStore.GetConfigSection(Configuration config, String sectionName, Boolean declare)
   at System.Configuration.ClientSettingsStore.WriteSettings(String sectionName, Boolean isRoaming, IDictionary newSettings)
   at System.Configuration.LocalFileSettingsProvider.SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection values)
   at System.Configuration.SettingsBase.SaveCore()
   at System.Configuration.SettingsBase.Save()
   at System.Configuration.ApplicationSettingsBase.Save()
   at BayesianSpamFilter.MainWindow.SaveSettings()

This is what I am doing: 这就是我在做什么:

string percentG = Properties.Settings.Default.PercentG;
string percentB = Properties.Settings.Default.PercentB;
string percentR = Properties.Settings.Default.PercentR;
string percentF = Properties.Settings.Default.PercentF;

string spamDbM = Properties.Settings.Default.SpamDbM;
string spamDbS = Properties.Settings.Default.SpamDbS;
string hamDbM = Properties.Settings.Default.HamDbM;
string hamDbS = Properties.Settings.Default.HamDbS;

private void SaveSettings()
{

    percentG = JsonConvert.SerializeObject(spamPercentageGraham);
    Properties.Settings.Default.PercentG = percentG;

    percentB = JsonConvert.SerializeObject(spamPercentageBurton);
    Properties.Settings.Default.PercentB = percentB;

    percentR = JsonConvert.SerializeObject(spamPercentageRobinson);
    Properties.Settings.Default.PercentR = percentR;

    percentF = JsonConvert.SerializeObject(spamPercentageFisher);
    Properties.Settings.Default.PercentF = percentF;

    spamDbM = JsonConvert.SerializeObject(spamDatabaseMulti);
    Properties.Settings.Default.SpamDbM = spamDbM;

    spamDbS = JsonConvert.SerializeObject(spamDatabaseSingles);
    Properties.Settings.Default.SpamDbS = spamDbS;

    hamDbM = JsonConvert.SerializeObject(hamDatabaseMulti);
    Properties.Settings.Default.HamDbM = hamDbM;

    hamDbS = JsonConvert.SerializeObject(hamDatabaseSingles);
    Properties.Settings.Default.HamDbS = hamDbS;


    Properties.Settings.Default.Save();
}

private void LoadSettings()
{
    spamPercentageGraham = JsonConvert.DeserializeObject<Dictionary<string, double>>(percentG);
    spamPercentageBurton = JsonConvert.DeserializeObject<Dictionary<string, double>>(percentB);
    spamPercentageRobinson = JsonConvert.DeserializeObject<Dictionary<string, double>>(percentR);
    spamPercentageFisher = JsonConvert.DeserializeObject<Dictionary<string, double>>(percentF);

    spamDatabaseMulti = JsonConvert.DeserializeObject<Dictionary<string, int>>(spamDbM);
    spamDatabaseSingles = JsonConvert.DeserializeObject<Dictionary<string, int>>(spamDbS);
    hamDatabaseMulti = JsonConvert.DeserializeObject<Dictionary<string, int>>(hamDbM);
    hamDatabaseSingles = JsonConvert.DeserializeObject<Dictionary<string, int>>(hamDbS);
}

Also: Count() for each of the dictionaries is around 300k. 另外:每个词典的Count()约为300k。 I am not running out of physical memory, nor RAM.. I am counting many diferent things with those dictionaries so I need all of them. 我没有用完物理内存,也没有用完RAM。.我正在用这些词典计算许多不同的事物,所以我需要所有这些。

Try setting the architecture of your application to 64bit. 尝试将应用程序的体系结构设置为64位。 Then your application should be able to allocate more memory! 然后,您的应用程序应该能够分配更多的内存!

A few things to consider. 需要考虑的几件事。

  • The architecture (32 bit / 64 bit) that you are targeting. 您要定位的体系结构(32位/ 64位)。 Like this answer said already, try compiling for a 64 bit platform if that is an option. 就像已经回答了这个答案一样,如果可以的话,请尝试为64位平台进行编译。 32-bit .NET processes are limited to 2 GB no matter how much RAM your machine's got. 无论您的计算机有多少RAM,32位.NET进程都限于2 GB。

    You can do this by creating a 64 bit configuration in BuildConfiguration ManagerActive solution platformNew… . 您可以通过在BuildConfiguration Manager活动解决方案平台New…中创建64位配置来完成此操作。

    This of course requires that you are not going to develop or run your program on a 32 bit machine. 当然,这要求您不要在32位计算机上开发或运行程序。

Even if the above is an option, I think you would do well to consider whether you can change your application design (or more precisely, the way how you persist that data): 即使上述选项是一种选择,我认为您还是可以考虑是否可以更改应用程序设计(或更准确地说,如何保存数据的方式):

  • Do you need to store all that data, or would a subset do? 您需要存储所有这些数据,还是需要一个子集存储? Is there any way you can reduce the data that has to be saved? 有什么方法可以减少必须保存的数据? For example, can some data be derived from other data by applying some fixed rule, so that you would only have to store the latter kind of data? 例如,是否可以通过应用某些固定规则从其他数据派生某些数据,这样您就只需要存储后一种数据即可?

  • Can you persist your data to some file(s) other than Web/App.config? 您能否将数据持久保存到Web / App.config以外的其他文件中? (Besides: the settings storage mechanism you are currently using should not be abused for storing any kind of data.) If you persisted your dictionaries using some other storage mechanism (such as one file per dictionary), you might be able to... (此外:不应滥用当前使用的设置存储机制来存储任何类型的数据。)如果使用其他存储机制(例如每个词典一个文件)来保存字典,则可以...

  • Serialize and persist one dictionary after the other, one at a time. 序列化并持久保存一个字典,一次又一次。 That might enable you to dispose no-longer-needed dictionaries (and their serialized representations) early, you'd only ever have to keep one serialization output in memory at a time. 这可能使您可以尽早处理不再需要的字典(及其序列化表示形式),而您一次只需要在内存中保留一个序列化输出即可。 This would likely relieve memory pressure. 这可能会减轻内存压力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM