【问题标题】:WCF memory Usage increasingWCF 内存使用量增加
【发布时间】:2014-04-17 21:20:05
【问题描述】:

我有一个 WCF 客户端和一个 WCF 服务。为了进行测试,当我持续同步调用 WCF 方法时,内存使用量每次会增加约 1 mb。我怎样才能阻止这种情况发生?

MyService.JobsClient Client = new MyService.JobsClient();
Client.Endpoint.Address = new System.ServiceModel.EndpointAddress(ConfigurationManager.AppSettings["ServicePath"]);
Client.WCFGetSystemState(System.Environment.MachineName); 

【问题讨论】:

  • 你确定这块内存以后不会被回收吗?请记住,GC 根据需要运行。它不会在某些对象不再使用时运行。
  • 完成后是否关闭客户端?
  • @MikeStockdale 不,这就是问题所在

标签: c# performance wcf wcf-binding


【解决方案1】:

您应该确保在使用客户端后关闭它。你可以遵循这个模式(取自MSDN):

MyService.JobsClient Client = new MyService.JobsClient();
Client.Endpoint.Address = new System.ServiceModel.EndpointAddress(ConfigurationManager.AppSettings["ServicePath"]);
try
{
    Client.WCFGetSystemState(System.Environment.MachineName); 
    Client.Close();
}
catch (TimeoutException timeout)
{
    // Handle the timeout exception.
    Client.Abort();
}
catch (CommunicationException commException)
{
    // Handle the communication exception.
    Client.Abort();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-20
    • 2012-01-24
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 2011-06-14
    • 2021-12-08
    • 2019-05-07
    相关资源
    最近更新 更多