堆内存问题
有一个 WCF 自托管服务必须在 99% 的时间内正常工作。有时我们会遇到这样的内存问题:
但在出现问题后服务正常工作。我们该如何处理这个问题?任何关于提供能够在不同情况下生存的强大服务的提示和要点都非常受欢迎。
There's a WCF self hosted service that must work 99% of time. Sometimes we got some memory troubles like this:
But service is working as usual after that issues. How can we manage this? Any tips and points to make robust services that will survive in different except situations are very very welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不太确定问题出在哪里,但内存泄漏可能是一个原因。
您假设所有代码都是托管的,但可能存在非托管部分。但是,所有一次性对象在使用后必须调用 Dispose 方法,不要以为它们一旦超出范围就被正确处置。最佳实践是,不要让 Disposable 对象超出范围而不调用其 Dispose 方法。如果您将“using”语句用作局部变量,则可以使用它们。
DbConnection 是一次性对象的一个很好的示例,请确保处置所有连接(一次性对象)。
I am not too sure where the problem resides but memory leaking can be a reason.
You assume all code is managed, but there can be unmanaged parts. However, you must call the Dispose method for all the disposable objects after using them, don't think they are properly dispose once they go out of scope. The best practice is, not to let Disposable objects to go out of scope without calling their Dispose method. You may be able to use 'using' statements if you are using them as local variables.
DbConnection is a good example for disposable objects, make sure you dispose all the connections (disposable objects).
如果是 WCF 问题(我不知道如何处理您的转储),我建议您激活 WCF 服务器端跟踪,并查看是否有异常(并编辑您的问题,以便我们进一步提供帮助)你)。
以下链接解释了如何执行此操作:
如何启用 WCF 跟踪
If it's a WCF problem (I'm not sure what to do with your dump), I suggest you activate WCF server-side tracing, and have a look at the exceptions if there are any (and edit your question so we can further help you).
Here is a link that explain how to do this:
How to enable WCF tracing
您的服务行为是什么,特别是 ConcurrencyMode 和 InstanceContextMode。
如果您将 Multiple 作为 ConcurrencyMode 并将 InstanceContext 设置为(PerCall 或 PerSession(默认)),那么如果您有大量 DataStructures 或未处置的资源,那么您肯定会遇到问题。
如果您使用多个并发,请尝试 InstanceContextMode Single [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
What are your Service Behaviors particularly ConcurrencyMode and InstanceContextMode.
if you have Multiple as ConcurrencyMode and InstanceContext set to (PerCall, or PerSession(default)) you can definetely run into issues if you have large DataStructures or undisposed resources.
if you are using multiple Concurrency try InstanceContextMode Single [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
您的依赖项是否 100% 没有非托管代码?我见过与此非常相似的事情发生,发生这种情况是因为我们正在释放内存,而另一个进程稍后也会尝试释放内存。
Are you 100% none of you dependencies have unmanaged code? I've seen something very similar to this happen, and it was happening because we were deallocating memory that another process would also try to deallocate later.