<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="RSS_xslt_style.asp" version="1.0" ?>
<rss version="2.0" xmlns:WebWizForums="http://syndication.webwiz.co.uk/rss_namespace/">
 <channel>
  <title>DevForce Community Forum : ExecuteAsync leaking memory?</title>
  <link>http://www.ideablade.com/forum/</link>
  <description>This is an XML content feed of; DevForce Community Forum : DevForce 2010 : ExecuteAsync leaking memory?</description>
  <pubDate>Sat, 11 Apr 2026 17:39:28 -700</pubDate>
  <lastBuildDate>Mon, 04 Oct 2010 06:07:32 -700</lastBuildDate>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Web Wiz Forums 9.69</generator>
  <ttl>360</ttl>
  <WebWizForums:feedURL>www.ideablade.com/forum/RSS_post_feed.asp?TID=2204</WebWizForums:feedURL>
  <image>
   <title>DevForce Community Forum</title>
   <url>http://www.ideablade.com/forum/forum_images/IdeaBlade_logo_tm.png</url>
   <link>http://www.ideablade.com/forum/</link>
  </image>
  <item>
   <title>ExecuteAsync leaking memory? : I have been able to solve this...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=2204&amp;PID=8694#8694</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=857" rel="nofollow">sarmaad</a><br /><strong>Subject:</strong> 2204<br /><strong>Posted:</strong> 04-Oct-2010 at 6:07am<br /><br />I have been able to solve this problem...<div><br></div><div>After a long testing and trial and error, I have found this interesting&nbsp;behaviour...</div><div><br></div><div>maybe someone at Ideablade can shed some light on it...</div><div><br></div><div>In my app, I have a global Entity Manager that holds all entities for the application.</div><div>This global manager is exposed as a property in which the class is exported using MEF.</div><div><br></div><div><table width="99%"><tr><td><pre class="BBcode"></div><div><div>&#091;Export(typeof(IEntityRepository))&#093;</div><div>class EntityRepository : IEntityRepository</div><div>{</div><div><span ="Apple-tab-span" style="white-space: pre; ">	</span>public EntityModelManager GlobalManager { get; private set; }</div><div><div><span ="Apple-tab-span" style="white-space: pre; ">	</span>public EntityModelManager CreateManager()</div><div><span ="Apple-tab-span" style="white-space: pre; ">	</span>{</div><div><span ="Apple-tab-span" style="white-space:pre">		</span>return new EntityModelManager(GlobalManager, true, DataSourceExtension, GlobalManager.EntityServiceOption);</div><div><span ="Apple-tab-span" style="white-space:pre">	</span>}</div></div><div><br></div><div>&nbsp;&#091;.... other properties/methods emitted ....&#093;</div><div><br></div><div>}</div></div><div></pre></td></tr></table></div><div><br></div><div>when I open a record to perform edit operations, I use a localized manager.&nbsp;</div><div>the local manager will pull down from global manager any entities it needs to perform its operations.</div><div>this is happening via injecting the&nbsp;IEntityRepository at the constructor level of the consuming class.</div><div><br></div><div><table width="99%"><tr><td><pre class="BBcode"></div><div><div>&#091;ExportViewModel("CRM.EditContact")&#093;</div><div>&#091;PartCreationPolicy(CreationPolicy.NonShared)&#093;</div></div><div><div>class CustomerEditViewModel</div><div>{</div></div><div><div><span ="Apple-tab-span" style="white-space:pre">	</span>IEntityRepository _repository;</div><div><span ="Apple-tab-span" style="white-space:pre">	</span>EntityModelManager _manager;</div><div><br></div><div><span ="Apple-tab-span" style="white-space:pre">	</span>&#091;ImportingConstructor&#093;</div><div><span ="Apple-tab-span" style="white-space:pre">	</span>public ContactEditViewModel(IEntityRepository repository)</div><div><span ="Apple-tab-span" style="white-space:pre">	</span>{</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_repository = repository;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//local manager</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_manager = _repository.CreateManager();</div><div><span ="Apple-tab-span" style="white-space:pre">	</span>}</div></div><div></pre></td></tr></table></div><div><br></div><div>when the form is shown, I had another method that preloaded the cache using AsyncParallelTask and loaded the entity by ID.</div><div><br></div><div>this is how I&nbsp;retrieved&nbsp;the entity from global manager to local manager</div><div><br></div><div><table width="99%"><tr><td><pre class="BBcode"></div><div><div><span ="Apple-tab-span" style="white-space:pre">	</span>var query = new EntityQuery&lt;Contact&gt;()</div><div><span ="Apple-tab-span" style="white-space:pre">		</span>.AddIncludePaths("ContactAddresses")</div><div><span ="Apple-tab-span" style="white-space:pre">		</span>.Where(col =&gt; col.ContactID == contactId)</div><div><span ="Apple-tab-span" style="white-space:pre">		</span>.With(QueryStrategy.DataSourceThenCache);</div><div><br></div><div><span ="Apple-tab-span" style="white-space:pre">	</span></div><div><span ="Apple-tab-span" style="white-space:pre">	</span>_repository.GlobalManager</div><div><span ="Apple-tab-span" style="white-space:pre">		</span>.ExecuteQueryAsync(</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;query,</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;opt =&gt;</div><div><span ="Apple-tab-span" style="white-space:pre">			</span>{&nbsp;</div><div><span ="Apple-tab-span" style="white-space:pre">				</span>&#091;code emitted&#093;&nbsp;</div><div><br></div><div><span ="Apple-tab-span" style="white-space:pre">				</span>var entityGraph = _repository.GlobalManager.FindEntityGraph(roots, spans, EntityState.Unchanged);</div><div><span ="Apple-tab-span" style="white-space:pre">				</span>_manager.ImportEntities(entityGraph, MergeStrategy.OverwriteChanges);</div><div><br></div><div><span ="Apple-tab-span" style="white-space:pre">				</span>&#091;code emitted&#093;&nbsp;</div><div><span ="Apple-tab-span" style="white-space:pre">			</span>}</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tag);</div></div><div></pre></td></tr></table></div><div><br></div><div>as you can see, I have executed the query&nbsp;against&nbsp;the global manager and imported the resulted entity graph to the local manager.</div><div><br></div><div>when the form is closed and all variables, fields, properties cleaned up, I still have a&nbsp;reference&nbsp;linking to the ViewModel and not allowing the GC from cleaning up the ViewModel, causing the memory leak.</div><div><br></div><div>now, How I solved the issue is by changing the last method to execute the query&nbsp;against&nbsp;the local manager and not the global.</div><div><br></div><div><table width="99%"><tr><td><pre class="BBcode"></div><div><div><span ="Apple-tab-span" style="white-space: pre; ">	</span>_manager</div><div><span ="Apple-tab-span" style="white-space: pre; ">		</span>.ExecuteQueryAsync(</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;query,</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;opt =&gt;</div><div><span ="Apple-tab-span" style="white-space: pre; ">			</span>{&nbsp;</div><div><span ="Apple-tab-span" style="white-space: pre; ">				</span>&#091;code emitted&#093;&nbsp;</div><div><br></div><div><span ="Apple-tab-span" style="white-space:pre">				</span>//no need to get the graph or import any entities</div><div><span ="Apple-tab-span" style="white-space: pre; ">				</span>//var entityGraph = _repository.GlobalManager.FindEntityGraph(roots, spans, EntityState.Unchanged);</div><div><span ="Apple-tab-span" style="white-space: pre; ">				</span>//_manager.ImportEntities(entityGraph, MergeStrategy.OverwriteChanges);</div><div><br></div><div><span ="Apple-tab-span" style="white-space: pre; ">				</span>&#091;code emitted&#093;&nbsp;</div><div><span ="Apple-tab-span" style="white-space: pre; ">			</span>}</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tag);</div></div><div></pre></td></tr></table></div><div><br></div><div>what really interesting, why my previous code, the EntityQuery&nbsp;maintained&nbsp;a link to the query but the preload cache method with utilize&nbsp;AsyncParallelTask worked perfectly without maintainig any link?</div><div><br></div><div>here is the preload cache method:</div><div><table width="99%"><tr><td><pre class="BBcode"></div><div><div><span ="Apple-tab-span" style="white-space:pre">	</span>int i = 0;</div><div><span ="Apple-tab-span" style="white-space:pre">	</span>var task = AsyncParallelTask.Create()</div><div><span ="Apple-tab-span" style="white-space:pre">		</span>.AddAsyncQuery(++i, x =&gt; _repository.GlobalManager.AddressTypes,</div><div><span ="Apple-tab-span" style="white-space:pre">			</span>opt =&gt; _manager.ImportEntities(opt.Results, MergeStrategy.OverwriteChanges));</div><div><span ="Apple-tab-span" style="white-space:pre">	</span>task.Execute(opt =&gt;</div><div><span ="Apple-tab-span" style="white-space:pre">		</span>{</div><div><span ="Apple-tab-span" style="white-space:pre">		</span>&#091;... error checks emitted ...&#093;</div><div><br></div><div><span ="Apple-tab-span" style="white-space:pre">		</span>_manager.AddressTypes.ForEach(AddressTypes.Add);</div><div><br></div><div><span ="Apple-tab-span" style="white-space:pre">		</span>&#091;code emitted&#093;</div><div><span ="Apple-tab-span" style="white-space:pre">		</span>});</div></div><div></pre></td></tr></table></div><div><br></div><div>I hope this could help someone else in similar situation and hopefully I will get some feedback from Ideablade shedding some light on this..</div><div><br></div>]]>
   </description>
   <pubDate>Mon, 04 Oct 2010 06:07:32 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=2204&amp;PID=8694#8694</guid>
  </item> 
  <item>
   <title>ExecuteAsync leaking memory? :   I have been memory profiling...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=2204&amp;PID=8672#8672</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=857" rel="nofollow">sarmaad</a><br /><strong>Subject:</strong> 2204<br /><strong>Posted:</strong> 30-Sep-2010 at 5:31am<br /><br /><div><span apple-style-span="Apple-style-span" style="font-size: small;">I have been memory profiling my WPF application and came across this behaviour...</span></div><div><span apple-style-span="Apple-style-span" style="font-size: small;"><br></span></div><div><span apple-style-span="Apple-style-span" style="font-size: small;">when I execute this statement:</span></div><div><span apple-style-span="Apple-style-span" style="font-size: small;"><br></span></div><div><span apple-style-span="Apple-style-span" style="font-size: small;"><table width="99%"><tr><td><pre class="BBcode"></span></div><div><div><span apple-style-span="Apple-style-span" style="font-size: small;">_repository.GlobalManager</span></div><div><span apple-style-span="Apple-style-span" style="font-size: small;">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;.Contacts</span></div><div><span apple-style-span="Apple-style-span" style="font-size: small;">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;.AddIncludePaths("ContactAddresses")</span></div><div><span apple-style-span="Apple-style-span" style="font-size: small;">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;.Where(col =&gt; col.ContactID == contactId)</span></div><div><span apple-style-span="Apple-style-span" style="font-size: small;">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;.With(QueryStrategy.DataSourceThenCache)</span></div><div><span apple-style-span="Apple-style-span" style="font-size: small;">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;.ExecuteAsync()</span></div></div><div><span apple-style-span="Apple-style-span" style="font-size: small;"></pre></td></tr></table></span></div><div><pre><font Apple-style-span="Apple-style-span" face="Verdana, Arial, Helvetica, sans-serif"><span apple-style-span="Apple-style-span" style="font-size: small;">it creates a reference leading back to m</span></font><span apple-style-span="Apple-style-span" style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 18px; font-size: small; ">y ViewModel that will stop the GC from doing its job...</span></pre><pre><font Apple-style-span="Apple-style-span" face="Verdana, Arial, Helvetica, sans-serif"><span Apple-style-span="Apple-style-span" style="line-height: 18px; font-size: small;"><s>However, Execute() works as expected.</s></span></font></pre><pre><font Apple-style-span="Apple-style-span" face="Verdana, Arial, Helvetica, sans-serif"><span apple-style-span="Apple-style-span" style="font-size: small;">not sure if I am doing something wrong or I am not understanding the proper use of ExecuteAsync.</span></font></pre><pre><font Apple-style-span="Apple-style-span" face="Verdana, Arial, Helvetica, sans-serif"><span apple-style-span="Apple-style-span" style="font-size: small;">I have attached a Object Retention Graph to clarify the question...</span></font></pre><pre><font Apple-style-span="Apple-style-span" face="Verdana, Arial, Helvetica, sans-serif"><span apple-style-span="Apple-style-span" style="font-size: small;"><a href="http://www.sarmaad.com/files/cc_org_executeasync.jpg" target="_blank">http://www.sarmaad.com/files/cc_org_executeasync.jpg</a><br></span></font></pre><pre><font Apple-style-span="Apple-style-span" face="Verdana, Arial, Helvetica, sans-serif"><span apple-style-span="Apple-style-span" style="font-size: small;">any feedback would be appreciated.</span></font></pre><pre><font Apple-style-span="Apple-style-span" face="Verdana, Arial, Helvetica, sans-serif"><span Apple-style-span="Apple-style-span" style="line-height: 18px; font-size: small;">EDIT:</span></font></pre><pre><font Apple-style-span="Apple-style-span" face="Verdana, Arial, Helvetica, sans-serif"><span Apple-style-span="Apple-style-span" style="line-height: 18px; font-size: small;">after some more testing... I was mistakenly thinking its holding the ViewModel. </span></font></pre><pre><font Apple-style-span="Apple-style-span" face="Verdana, Arial, Helvetica, sans-serif"><span Apple-style-span="Apple-style-span" style="line-height: 18px; font-size: small;">Execute/FirstOrNullEntity produced exactly the same results!</span></font></pre><pre><font Apple-style-span="Apple-style-span" face="Verdana, Arial, Helvetica, sans-serif"><span Apple-style-span="Apple-style-span" style="line-height: 18px; font-size: small;"><br></span></font></pre></div><span style="font-size:10px"><br /><br />Edited by sarmaad - 07-Oct-2010 at 12:50pm</span>]]>
   </description>
   <pubDate>Thu, 30 Sep 2010 05:31:04 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=2204&amp;PID=8672#8672</guid>
  </item> 
 </channel>
</rss>