{"id":3794,"date":"2014-03-30T05:38:26","date_gmt":"2014-03-30T05:38:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/windows-workflow-runtime-leaks-a-ton-of-memory-collection-of-common-programming-errors\/"},"modified":"2014-03-30T05:38:26","modified_gmt":"2014-03-30T05:38:26","slug":"windows-workflow-runtime-leaks-a-ton-of-memory-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/windows-workflow-runtime-leaks-a-ton-of-memory-collection-of-common-programming-errors\/","title":{"rendered":"Windows Workflow Runtime leaks a ton of memory-Collection of common programming errors"},"content":{"rendered":"<p>Here&#8217;s a overview of my workflow implementation:<\/p>\n<ul>\n<li>GUI thread starts worker thread<\/li>\n<li>worker thread analyzes some data<\/li>\n<li>worker thread starts several other worker threads to work on subsets of data<\/li>\n<li>each of these last worker threads creates a workflow runtime and executes a sequential workflow<\/li>\n<\/ul>\n<p>Up until now, I&#8217;ve been creating a new WorkflowRuntime object in each thread like this:<\/p>\n<pre><code>  using( WorkflowRuntime workflow_runtime = new WorkflowRuntime()) {\n      AutoResetEvent waitHandle = new AutoResetEvent(false);\n      workflow_runtime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) {waitHandle.Set();};\n      workflow_runtime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)\n      {\n          Console.WriteLine(e.Exception.Message);\n          waitHandle.Set();\n      };\n\n      WorkflowInstance instance = workflow_runtime.CreateWorkflow(typeof(MyWorkflow), parameters);\n      instance.Start();\n      waitHandle.WaitOne();\n}\n<\/code><\/pre>\n<p>The reason for doing it this way is that I need to know when a specific workflow instance has been terminated or errored. The problem is that it causes a huge memory leak in my application, as mentioned here on SO.<\/p>\n<p>If I use the using keyword, or even if I call Dispose and set the workflow_runtime reference to null, I get a massive memory leak. However, if I implement the workflow runtime as a Singleton, as described in this post, memory usage is very low and consistent. I can see when workflows are launched and completed by blips in the graph.<\/p>\n<p>The problem is, if I use the Singleton pattern for the WF runtime, how will I know when a <em>specific<\/em> workflow has an error? If I just register the event handlers, won&#8217;t <em>all<\/em> of them get called when <em>any<\/em> of the workflows get terminated or completed?<\/p>\n<p>EDIT: should I just use another handle on the stack for errors, and then wait for either to be set, and then check which one was set? I should have considered that before.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a overview of my workflow implementation: GUI thread starts worker thread worker thread analyzes some data worker thread starts several other worker threads to work on subsets of data each of these last worker threads creates a workflow runtime and executes a sequential workflow Up until now, I&#8217;ve been creating a new WorkflowRuntime object [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3794","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3794"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3794\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}