C# 异常 OutOfMemory

C# exception OutOfMemory

我需要一些帮助来处理这个异常:

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()

这就是我正在做的事情:

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);
}

此外:Count() 每个词典大约 300k。我没有 运行 物理内存不足,也没有 RAM 不足。我正在用这些词典计算很多不同的东西,所以我需要所有这些东西。

尝试将应用程序的体系结构设置为 64 位。那么您的应用程序应该能够分配更多内存!

需要考虑的几件事。

  • 您的目标架构(32 位/64 位)。 所说,尝试为 64 位平台编译如果这是一个选项。无论您的计算机有多少 RAM,32 位 .NET 进程都限制为 2 GB。

    您可以通过在 BuildConfiguration ManagerActive solution platform[ 中创建 64 位配置来完成此操作=43=] → 新建….

    这当然要求您不打算在 32 位计算机上开发或运行您的程序。

即使以上是一个选项,我认为您最好考虑一下是否可以更改您的应用程序设计(或者更准确地说,您保留数据的方式):

  • 您需要存储所有数据,还是需要存储一个子集?有什么方法可以减少必须保存的数据?例如,是否可以通过应用一些固定规则从其他数据派生一些数据,以便您只需要存储后一种数据?

  • 您能否将数据保存到 Web/App.config 以外的某些文件?(此外:设置存储机制当前使用的不应该被滥用于存储 任何 类型的数据。)如果你使用一些其他存储机制(例如每个字典一个文件)来持久化你的字典,你可能能够.. .

  • 一个接一个地序列化并保留一个字典,一次一个。这可能使您能够处理不再需要的字典(及其序列化的字典) representations),你一次只需要在内存中保存一个序列化输出。这可能会减轻内存压力。