c# - OutOfMemoryException - WCF 服务

标签 c# .net string wcf out-of-memory

我发布了一个 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)

关于如何重现问题有什么想法或建议吗?

最佳答案

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

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

关于c# - OutOfMemoryException - WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31673966/

相关文章:

python - 查找字符串中重复字符的最长子串

c# - Entity Framework : Unknown column in 'where clause

c++ - 如何从文件中识别数据类型

c# - SignalR 拒绝工作

c# - WebRequest 相当于 `curl -X DELETE ...`

c# - 将 gRPC 服务绑定(bind)到 aspnetcore 中的特定端口

c# - 为ASP.NET运行Memcached

c++ - 为什么 strncpy 不安全?

C#如何从列表中删除特定节点

c# - 来自 .wav 文件的峰值频率