WCF 序列化和缓存
我有一个托管在控制台应用程序中的 WCF 服务。我有一个 ChannelFactory 来调用 WCF 的操作合同。
问题:每当我调用返回值的操作时,返回的值似乎在序列化时被服务缓存在某处。
我正在通过Windows 7下的任务管理器检查服务内存使用情况。当我调用一个不返回任何内容的操作时,内存不会增加,但是当我调用一个返回数据的操作时,内存会增加,并且即使在数据返回给客户端。
我的猜测是这是一个序列化缓存问题?!?
I have a WCF service hosted in a console application. and I have a ChannelFactory
to call WCF's operation Contracts.
Problem: whenever I call an operation that returns values, it seems that the value returned is cached somewhere by the service when it is serialized.
I am checking the service memory usage through the task manager under windows 7. When I call an operation that returns nothing, the memory is not increased, but when I call an operation that returns data, memory is increased and stays this way even after the data is returned to the client.
My guess is that this is a serialization caching issue?!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来更像是垃圾收集器尚未运行,因此内存尚未释放。此外,当在控制台应用程序中托管 WCF 服务时,GC 以工作站模式运行,在这种情况下效率可能较低。
It sounds more like garbage collector hasn't run yet and because of that the memory wasn't released. Moreover when hosting WCF service in console application the
GC
runs in Workstation mode which can be less effective in such case.内存可能会一直增加,直到 GC 运行为止,这与数据返回到客户端的时间不对应。
您是否尝试过在服务方法中添加断点或某种类型的日志记录,以确保每个请求都调用该方法?我认为 WCF 本身不会进行任何缓存;至少,我从未让它在使用它的应用程序中执行任何操作。
编辑:
内存将继续使用,直到垃圾收集器运行。如果您的进程的堆中仍然有足够的可用空间,那么 GC 确实没有理由运行。
根据 MSDN:http://msdn.microsoft.com/en-我们/library/ee787088.aspx#conditions_for_a_garbage_collection
您可能正在堆中分配对象,但它们没有被 GC,因为 GC 认为没有理由运行(堆生成中仍然有开放空间,没有理由花时间清除它)。
但是,如果您可以一遍又一遍地重复 WCF 调用并最终得到内存不足异常,那么这将表明您确实在某处保存引用时遇到了问题。在这种情况下,我将使用内存分析器来确定保留了什么以及由什么保留。
编辑#2:
另请参阅此线程:C# 线程未释放内存
Memory will probably stay increased until the GC runs, which won't correspond to when data is returned to the client.
Have you tried adding a breakpoint or logging of some sort to your service method to make sure that the method is being called on each request? I don't think WCF does any caching on its own; at least, I've never had it do any in my applications that use it.
Edit:
Memory will remain in use until the garbage collector runs. If your process still has plenty of free space in its heap, then there really isn't a reason for the GC to run.
According to MSDN: http://msdn.microsoft.com/en-us/library/ee787088.aspx#conditions_for_a_garbage_collection
It is likely that you are allocating objects in the heap, but they are not being GC'd because the GC sees no reason to run (there is still open space in the heap generation, and no reason to spend time clearing it out).
However, if you can repeat your WCF call over and over and eventually get an Out of Memory exception, then that would indicate that you do indeed have a problem with references being held somewhere. In that case, I would use a memory profiler to determine what is being held onto, and by what.
Edit #2:
See also this thread: C# Thread not releasing memory
使用这样的工具来查看正在创建/收集哪些对象:
http://memprofiler.com/
如果每次调用后没有运行 GC,我不会太担心 -无论如何,这不会非常有效 - 垃圾收集器将在 .NET 框架确定对象已经足够旧而可以收集时运行。如果内存开始不足,这种情况就会更频繁地发生。
Use a tool like this to see what objects are being created/collected:
http://memprofiler.com/
I wouldn't worry too much if GC isn't being run after each call - this wouldn't be very efficient anyway - the garbage collector will run at a point when the .NET framework determines that objects are old enough to be collected. If memory starts to run low then this will happen more frequently.