OutOfMemoryException - WCF 服务

OutOfMemoryException - WCF services

我发布了一个 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异常不是因为内存已满。

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

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)

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

看来问题出在长字符串上。可能 StringBuilder 在添加新内容时需要扩展内存。我不确定,但是这个手术什么时候进行; StringBuilder 试试双倍尺寸。我相信 CLR 不能分配大小大于 2GB 的对象,这就是它抛出 OutOfMemoryException

的原因

您在 32 位上看到地址 space 碎片。最大可用地址 space 取决于 OS 和 exe,冷为 2,3 或 4 GB。

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