WCF 序列化和缓存

发布于 2024-11-14 23:15:25 字数 259 浏览 6 评论 0原文

我有一个托管在控制台应用程序中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迟到的我 2024-11-21 23:15:25

听起来更像是垃圾收集器尚未运行,因此内存尚未释放。此外,当在控制台应用程序中托管 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.

各自安好 2024-11-21 23:15:25

内存可能会一直增加,直到 GC 运行为止,这与数据返回到客户端的时间不对应。

您是否尝试过在服务方法中添加断点或某种类型的日志记录,以确保每个请求都调用该方法?我认为 WCF 本身不会进行任何缓存;至少,我从未让它在使用它的应用程序中执行任何操作。


编辑:

内存将继续使用,直到垃圾收集器运行。如果您的进程的堆中仍然有足够的可用空间,那么 GC 确实没有理由运行。

根据 MSDN:http://msdn.microsoft.com/en-我们/library/ee787088.aspx#conditions_for_a_garbage_collection

当以下情况之一发生时,就会发生垃圾收集:
满足以下条件:

  • 系统物理内存不足。

  • 托管堆上分配的对象使用的内存
    超过可接受的阈值。
    这意味着阈值
    可接受的内存使用量已
    超出了托管堆。这
    阈值不断调整为
    进程运行。

  • 调用 GC.Collect 方法。几乎在所有情况下,您都没有
    调用这个方法,因为
    垃圾收集器持续运行。
    该方法主要用于
    独特的情况和测试。

您可能正在堆中分配对象,但它们没有被 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

Garbage collection occurs when one of
the following conditions is true:

  • The system has low physical memory.

  • The memory that is used by allocated objects on the managed heap
    surpasses an acceptable threshold.
    This means that a threshold of
    acceptable memory usage has been
    exceeded on the managed heap. This
    threshold is continuously adjusted as
    the process runs.

  • The GC.Collect method is called. In almost all cases, you do not have
    to call this method, because the
    garbage collector runs continuously.
    This method is primarily used for
    unique situations and testing.

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

断桥再见 2024-11-21 23:15:25

使用这样的工具来查看正在创建/收集哪些对象:
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文