{"id":870,"date":"2015-01-25T21:58:03","date_gmt":"2015-01-25T11:58:03","guid":{"rendered":"http:\/\/www.moneystock.net\/wp_e\/?p=870"},"modified":"2016-08-06T00:21:04","modified_gmt":"2016-08-05T14:21:04","slug":"memory-management-in-wcf-applications-not-to-leak-memory","status":"publish","type":"post","link":"https:\/\/moneystock.net\/wp_e\/2015\/01\/25\/memory-management-in-wcf-applications-not-to-leak-memory\/","title":{"rendered":"How to prevent memory leak in WCF"},"content":{"rendered":"<p>I have one WCF site that I&#8217;m managing and it started leaking memory, which ended up consuming\u00a0up all server memory and stopped working.<\/p>\n<p>The change I made between the incident was that I\u00a0changed from using service reference to using a class called WebServcieClinet\u00a0that dynamically instantiating the web service connection.<\/p>\n<p>The reason why I introduced WebServiceClient is that I wanted to dynamically change the web service endpoint.<\/p>\n<p>What I have missed from the class that creates the web service connection was failing to\u00a0release the unmanaged\u00a0resource.<\/p>\n<p>Unmanaged resource in C# is such as open file, open connection, etc., which garbage collector does not know about. For this unmanaged\u00a0resource, we need to release the resource explicitly by calling Dispose () method.<\/p>\n<p>Ref:\u00a0<a title=\"What exactly are unmanaged resources\" href=\"http:\/\/stackoverflow.com\/questions\/3433197\/what-exactly-are-unmanaged-resources\">http:\/\/stackoverflow.com\/questions\/3433197\/what-exactly-are-unmanaged-resources<\/a><\/p>\n<p>1. Calling GC.Collect after finishing using the unmanaged resource.<\/p>\n<p>At first, I tried to go use GC.Collect, which means that I explicitly called garbage collection method to release any resource that&#8217;s not being used anymore.<\/p>\n<pre>WebServicesClient dm = new WebServicesClient(requestData.token, Settings.Default.EndPointUrl);\r\n\r\n\/\/ do something\r\n\r\ndm = null;\r\nGC.Collect();<\/pre>\n<p>This seems sort or work, but not perfect. And also I could find that calling GC.Collect is very costly in terms of performance.<\/p>\n<p>2. Use IDisposible pattern<\/p>\n<p>All web service connections (XXXSoapClient) made by the WebServiceClient object are unmanaged resources and those are the object inherited from IDispoable for this reason.<\/p>\n<p>So, I applied IDisposable interface in WebServiceClient.<\/p>\n<pre>public class WebServicesClient : IDisposable\r\n{\r\n bool disposed;\r\n\r\npublic void Dispose()\r\n {\r\n Dispose(true);\r\n GC.SuppressFinalize(this);\r\n }\r\n\r\nprotected virtual void Dispose(bool disposing)\r\n {\r\n if (disposed)\r\n return;\r\n\r\nif (disposing)\r\n {\r\n \/\/ Free any other manage object here.\r\n }\r\n\r\n\/\/ Free any unmanaged object here.\r\n if (soapClientA != null)\r\n {\r\n soapClientA.Close();\r\n ((IDisposable) soapClientA).Dispose();\r\n soapClientA = null;\r\n }\r\n\r\nif (soapClientB != null)\r\n {\r\n soapClientB.Close();\r\n ((IDisposable) soapClientB).Dispose();\r\n soapClientBA = null;\r\n }\r\n\r\ndisposed = true;\r\n }\r\n\r\n\/\/ This is needed only when this object directly uses managed resources\r\n ~WebServiceClient()\r\n {\r\n Dispose(false);\r\n }\r\n}<\/pre>\n<p>When using the WebServiceClient, we can just call Dispose() instead of calling GC.Collect.<\/p>\n<pre>WebServicesClient dm = new WebServicesClient(requestData.token, Settings.Default.EndPointUrl);\r\n\r\n\/\/ do something\r\n\r\ndm.Dispose();<\/pre>\n<p>As the Dispose() method releases the exact resource that needs to be released, which is much efficient in terms of performance compared to calling GC.Collect, which investigate all resources that are still being used or not.<\/p>\n<p>After applying IDisposable pattern, there wasn&#8217;t memory leak and performance should be ok.<\/p>\n<p>3. Further clean up<\/p>\n<p>I haven&#8217;t tested below, but what Dispose() does in web service base class is &#8216;Close&#8217; the web service connection. In this regard, the line below may be redundant from the code of WebServiceClient.<\/p>\n<pre>soapClientA.Close();<\/pre>\n<p>Ref:\u00a0<a href=\"http:\/\/stackoverflow.com\/questions\/538060\/proper-use-of-the-idisposable-interface\/538238#538238\">http:\/\/stackoverflow.com\/questions\/538060\/proper-use-of-the-idisposable-interface\/538238#538238<\/a><\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/538060\/proper-use-of-the-idisposable-interface\">http:\/\/stackoverflow.com\/questions\/538060\/proper-use-of-the-idisposable-interface<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have one WCF site that I&#8217;m managing and it started leaking memory, which ended up consuming\u00a0up all server memory and stopped working. The change I made between the incident was that I\u00a0changed from using service reference to using a class called WebServcieClinet\u00a0that dynamically instantiating the web service connection. The reason why I introduced WebServiceClient&hellip; <a class=\"more-link\" href=\"https:\/\/moneystock.net\/wp_e\/2015\/01\/25\/memory-management-in-wcf-applications-not-to-leak-memory\/\">Continue reading <span class=\"screen-reader-text\">How to prevent memory leak in WCF<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[193,180],"tags":[197,198,196,194,195,181],"class_list":["post-870","post","type-post","status-publish","format-standard","hentry","category-performance","category-wcf","tag-dispose","tag-garbage-collection","tag-idisposable","tag-memory-leak","tag-performance","tag-wcf-2","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/comments?post=870"}],"version-history":[{"count":9,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/870\/revisions"}],"predecessor-version":[{"id":976,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/870\/revisions\/976"}],"wp:attachment":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/media?parent=870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/categories?post=870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/tags?post=870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}