在应用程序关闭之前未释放 WCF 代理的未释放资源?
我有一个 .NET 应用程序,我在其中实例化一个 WCF 代理,该代理保存在内存中直到应用程序关闭。考虑引用代理的单例服务代理,即代理被重复用于连续的服务调用。没有对 proxy.Dispose() 或 using 块的显式调用。
问题是,我是否泄漏了任何资源?据我所知,.NET 垃圾收集将防止内存泄漏(代理是受管理的,不是吗?)。还有其他吗 - 网络连接、端口等?
编辑:该项目未保存在应用程序之外的另一个进程中。
I have a .NET application where I instantiate a WCF proxy which is held in memory till the application shuts down. Think of a singleton serviceagent that references the proxy, i.e. the proxy is reused for consecutive service calls. There are no explicit calls to proxy.Dispose() or using blocks.
The question is, am I leaking any resources? As far as I can tell, .NET garbage collection will prevent this from leaking memory (proxies are managed are they not?). Anything else - Network connections, ports etc?
edit: the item is not being held in another process outside of the application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不会。一旦您的应用程序终止,除非您分配了共享内存等全局资源,否则 Windows 将关闭所有套接字并释放分配给您的程序的所有内存。如果您认为存在类似于泄漏的情况,可能是因为您的程序未完全终止(检查任务管理器)。
No. Once your application terminates, unless you've allocated global resources like shared memory/etc, Windows will close all your sockets and release all memory that was allocated to your program. If you think there's something akin to a leak, it may be because your program is not fully terminating (check Task Manager).
一旦应用程序关闭,任何正在处理的内容都将被回收。该项目是否保存在您的应用程序之外的另一个流程中?
As soon as the application shuts down, anything in process will be reclaimed. Is this item being held in another process outside of your application?
代理受到管理,但它们可能会保留连接。这就是代理实现 IDisposable 的原因。
一般情况是,如果一个对象实现了 IDisposable,则应该处置它。
当您的应用程序终止时,代理也会随之终止,但它所占用的任何资源(例如打开的连接)都不会终止,直到代理实际上被垃圾收集器清理为止。
长话短说:始终对一次性对象调用
Dispose
。Proxies are managed, but they may hold on to connections. That is why proxies implement
IDisposable
.The general story is that if an object implements IDisposable, you should dispose it.
When your app terminates, the proxy is terminated along but any resources that it holds onto (such as open connections) are not terminated until the proxy is actually cleaned up by the garbage collector.
Long story short: Always call
Dispose
on disposable objects.推荐的最佳实践是在客户端完成时始终关闭代理
使用它。关闭代理会释放与服务保持的连接,即
在交通会议期间这样做尤其重要。它还有助于确保最大连接数的阈值
无法到达客户端计算机上。
对于单例服务,关闭客户端代理只会终止传输会话,而不终止单例上下文和内部实例。如果是单例
服务支持没有会话的合约,这些合约不会是每次调用:它们
也将连接到同一个实例。就其本质而言,单例是共享的,
每个客户端应该简单地创建自己的代理或代理。关闭代理并没有终止单例。
It is a recommended best practice to always close the proxy when the client is done
using it. Closing the proxy releases the connection held toward the service, which is
particularly important to do in the presence of a transport session. It also helps ensure the threshold for the maximum number of connections
on the client’s machine is not reached.
For the singleton service closing the client proxy will terminate only the transport session, not the singleton context and the instance inside. If the singleton
service supports contracts without a session, those contracts will not be per-call: they
too will be connected to the same instance. By its very nature, the singleton is shared,
and each client should simply create its own proxy or proxies to it. closing the proxies did not terminate the singleton.