<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic GetFeature causing memory leak when editing in ArcObjects SDK Questions</title>
    <link>https://community.esri.com/t5/arcobjects-sdk-questions/getfeature-causing-memory-leak-when-editing/m-p/473168#M12792</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I've know that some of our custom tools occasionally run into memory leaks from time to time.&amp;nbsp; Usually, we just run the tool in smaller batches to get through it.&amp;nbsp; But after doing some investigation, I finally tracked down where the problem is coming from.&amp;nbsp; It would seem that calling IFeatureClass.GetFeature or IStandaloneTable.GetRow on a table that is currently being edited, causes the row to not be released.&amp;nbsp; I tried using Marshal.FinalReleaseComObject to force it to release the feature but it didn't help.&amp;nbsp; I think I had some minor success or improvement by doing that and forcing garbage collection.&amp;nbsp; But it didn't fix everything.&amp;nbsp; If you do the same thing on a table that is not being edited, there's no issue.&amp;nbsp; Maybe there's something in the editor extension that is grabbing on to features that are being retrieved and not letting go of them.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is some sample code I created that shows the issue.&amp;nbsp; It gets the OID of all of the features in the first layer in your map and puts them into an array.&amp;nbsp; Then goes through those OIDs and calls GetFeature for each one.&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;var StartTime = DateTime.Now;&lt;BR /&gt;var Count = 0;&lt;BR /&gt;var Total = 0;&lt;BR /&gt;var Layer = (IFeatureLayer)mxDoc.FocusMap.get_Layer(0);&lt;BR /&gt;var Cursor = Layer.FeatureClass.Search(null, true);&lt;BR /&gt;var OIDs = new List&amp;lt;int&amp;gt;();&lt;BR /&gt;var Feat = (IFeature) null;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp;&amp;nbsp; while ((Feat = Cursor.NextFeature()) != null)&lt;BR /&gt;&amp;nbsp;&amp;nbsp; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OIDs.Add(Feat.OID);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Total++;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;}&lt;BR /&gt;finally&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Marshal.FinalReleaseComObject(Cursor);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;foreach (var OID in OIDs)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if (Count % 1000 == 0)&lt;BR /&gt;&amp;nbsp;&amp;nbsp; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var Ts = DateTime.Now - StartTime;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MxApp.StatusBar.set_Message(0,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;string.Format("{0:#,0}s - {1:#,0}/{2:#,0} - GetFeature()...", Ts.TotalSeconds, Count, Total)); &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Application.DoEvents();&lt;BR /&gt;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Feat = Layer.FeatureClass.GetFeature(OID);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; System.Runtime.InteropServices.Marshal.FinalReleaseComObject(Feat);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Feat = null;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Count++;&lt;BR /&gt;}&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Attached is a video showing this code executing.&amp;nbsp; I've included Task Manager so you can watch the memory increase as it goes.&amp;nbsp; I've sped it up about 12x as it takes a few minutes to get through the whole thing.&amp;nbsp; If you run this with the layer not being edited, then there's no issue and memory usage never goes over about 200MB.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 16 May 2018 16:43:14 GMT</pubDate>
    <dc:creator>BillyBuerger</dc:creator>
    <dc:date>2018-05-16T16:43:14Z</dc:date>
    <item>
      <title>GetFeature causing memory leak when editing</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/getfeature-causing-memory-leak-when-editing/m-p/473168#M12792</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I've know that some of our custom tools occasionally run into memory leaks from time to time.&amp;nbsp; Usually, we just run the tool in smaller batches to get through it.&amp;nbsp; But after doing some investigation, I finally tracked down where the problem is coming from.&amp;nbsp; It would seem that calling IFeatureClass.GetFeature or IStandaloneTable.GetRow on a table that is currently being edited, causes the row to not be released.&amp;nbsp; I tried using Marshal.FinalReleaseComObject to force it to release the feature but it didn't help.&amp;nbsp; I think I had some minor success or improvement by doing that and forcing garbage collection.&amp;nbsp; But it didn't fix everything.&amp;nbsp; If you do the same thing on a table that is not being edited, there's no issue.&amp;nbsp; Maybe there's something in the editor extension that is grabbing on to features that are being retrieved and not letting go of them.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is some sample code I created that shows the issue.&amp;nbsp; It gets the OID of all of the features in the first layer in your map and puts them into an array.&amp;nbsp; Then goes through those OIDs and calls GetFeature for each one.&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;var StartTime = DateTime.Now;&lt;BR /&gt;var Count = 0;&lt;BR /&gt;var Total = 0;&lt;BR /&gt;var Layer = (IFeatureLayer)mxDoc.FocusMap.get_Layer(0);&lt;BR /&gt;var Cursor = Layer.FeatureClass.Search(null, true);&lt;BR /&gt;var OIDs = new List&amp;lt;int&amp;gt;();&lt;BR /&gt;var Feat = (IFeature) null;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp;&amp;nbsp; while ((Feat = Cursor.NextFeature()) != null)&lt;BR /&gt;&amp;nbsp;&amp;nbsp; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OIDs.Add(Feat.OID);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Total++;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;}&lt;BR /&gt;finally&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Marshal.FinalReleaseComObject(Cursor);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;foreach (var OID in OIDs)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if (Count % 1000 == 0)&lt;BR /&gt;&amp;nbsp;&amp;nbsp; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var Ts = DateTime.Now - StartTime;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MxApp.StatusBar.set_Message(0,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;string.Format("{0:#,0}s - {1:#,0}/{2:#,0} - GetFeature()...", Ts.TotalSeconds, Count, Total)); &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Application.DoEvents();&lt;BR /&gt;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Feat = Layer.FeatureClass.GetFeature(OID);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; System.Runtime.InteropServices.Marshal.FinalReleaseComObject(Feat);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Feat = null;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Count++;&lt;BR /&gt;}&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Attached is a video showing this code executing.&amp;nbsp; I've included Task Manager so you can watch the memory increase as it goes.&amp;nbsp; I've sped it up about 12x as it takes a few minutes to get through the whole thing.&amp;nbsp; If you run this with the layer not being edited, then there's no issue and memory usage never goes over about 200MB.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 May 2018 16:43:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/getfeature-causing-memory-leak-when-editing/m-p/473168#M12792</guid>
      <dc:creator>BillyBuerger</dc:creator>
      <dc:date>2018-05-16T16:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: GetFeature causing memory leak when editing</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/getfeature-causing-memory-leak-when-editing/m-p/473169#M12793</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Forgot to mention that the workaround for this issue is to just use GetRows/GetFeatures passing the one OID and grab the first record from the cursor.&amp;nbsp; This has the added benefit that GetRows doesn't through an exception when the OID doesn't exist.&amp;nbsp; Then I can return null and deal with that instead of having to handle an exception.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 May 2018 19:52:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/getfeature-causing-memory-leak-when-editing/m-p/473169#M12793</guid>
      <dc:creator>BillyBuerger</dc:creator>
      <dc:date>2018-05-16T19:52:33Z</dc:date>
    </item>
  </channel>
</rss>

