c# – OutOfMemoryException – WCF服务

我发布了一个WCF(C#)项目,我面临“OutOfMemoryException”问题. DLL使用Any CPU构建. AppPool内存设置设置为0(表示没有限制).

我每分钟有大约1500个请求,每分钟大约有100个请求.该项目使用EntityFramework.应用程序中有缓存(它是字典)

我做了一些故障排除试图找出问题,但目前尚不清楚.我试图计算使用的总内存(通过调用GC.GetTotalMemory(false))和缓存列表的大小.获取OOM异常时,缓存大小约为7 MB(2500个对象,每个30 KB),使用的总内存在600 MB到1.5 GB之间变化.

因此很明显内存未满并且GarbageCollector会定期清除资源(内存大小不会一直增加).因此,OOM异常不是因为内存已满.

主要是我在日志OOM异常中看到将对象序列化为JSON(我正在使用Newtonsoft)或对字符串应用一些操作(连接,替换,Regexreplace,…),这里有一些异常示例:

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at System.Text.StringBuilder.ExpandByABlock(Int32 minBlockCharCount)
at System.Text.StringBuilder.Append(Char* value, Int32 valueCount)
at System.Text.StringBuilder.Append(String value, Int32 startIndex, Int32 count)
at System.Text.RegularExpressions.RegexReplacement.Replace(Regex regex, String input, Int32 count, Int32 startat)
at System.Text.RegularExpressions.Regex.Replace(String input, String replacement, Int32 count, Int32 startat)
at System.Text.RegularExpressions.Regex.Replace(String input, String replacement)
at System.Text.RegularExpressions.Regex.Replace(String input, String pattern, String replacement)

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at System.String.ReplaceInternal(String oldValue, String newValue)
at System.String.Replace(String oldValue, String newValue)

关于如何重现问题的任何想法或建议?

最佳答案 您在32位上看到地址空间碎片.最大可用地址空间取决于操作系统和exe,冷却为2,3或4 GB.

以64位运行IIS工作进程,以便您的AnyCPU DLL可以利用它.

点赞