2014-04-17 83 views
1

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

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

您确定此内存不被回收以后?请记住,GC根据需要运行。每当某个对象不再使用时,它就不会运行。 –

+1

当你完成它时,你是否关闭客户端? –

+0

@MikeStockdale不,这是问题的感谢 – user1438082

回答

2

您应该确保在使用它后关闭客户端。您可以按照此模式(取自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(); 
}