2014-04-17 80 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(); 
}