0

I have few questions how Garbage collector works in C# applications

Application performs heavy I/O operations and database operations over the course of 10 days.

I read at places where some experts advise not to use GC since it is Framework's responsibility and let it do its job or if you force it, application's performance will be slow down.

I somehow need to use 32bit APIs where memory is limited to 2GB and leaving other resources, our application in this discussion could be crashed if it starts showing up at 1.5GB.

For managed resources, I am able to easily get back the memory that is used after every iteration in loop. But when it comes to COM components that I use for geoprocessing, it is very inconsistent how memory gets released. I take good care of releasing all sorts of COM objects after using them in a given iteration still I end up of getting error such as 'not enough memory'. So after about 300 to 400 iterations[each iteration takes some amount of time close to few mins], I only need to close and reopen to release all the resources.

I will provide more inputs if someone wants to listen more.

The main issuethat I am looking to resolve is, how to get all my memory back consistently after each iteration let us say in for loop by cleaning up everything especially with COM objects.

Best Regards

  • Can you show some code. You should be implementing the `IDisposable` interface then releasing your COM objects in there. From COM the main objects to use when disposing are 'GC' and `Marshal` with specific reference to `Marshal.FinalReleaseComObject` method – bilpor Nov 05 '18 at 10:28
  • This question is really broad. You might want to post an exact example of a loop that is leaking resources. Make the problem a bit more concrete. – fstam Nov 05 '18 at 10:43
  • If you're doing geoprocessing, if you do it in background you can use 64bit. http://desktop.arcgis.com/en/arcmap/10.3/analyze/executing-tools/64bit-background.htm – Kirk Kuykendall Nov 07 '18 at 17:43
  • Hi, Thank you for the suggestion. Background geoprocessing tool is available with engine license as well? – user3174598 Nov 19 '18 at 05:49
  • Here is the code geoprocessor.ClearMessages(); // Set the overwrite output option to true geoprocessor.OverwriteOutput = true; try { Logger.Write("RunTool is started", "AppLog"); // Execute the tool geoprocessor.Execute(process, null); ReturnMessages(geoprocessor); ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(geoprocessor); ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(process); geoprocessor = null; process = null; } catch (Exception ex) { Logger.Write("Exception in RunTool::"+ex.Message, "ExceptionHandling"); ReturnMessages(geoprocessor); } – user3174598 Nov 27 '18 at 08:02
  • Background geoprocessing is available with Engine. Also is a separate install. http://desktop.arcgis.com/en/quick-start-guides/latest/arcgis-engine-developer-kit-and-engine-quick-start-guide.htm#ESRI_SECTION1_B1DF7D5DABEC485DA17181F962B6A867 – Kirk Kuykendall Dec 08 '18 at 03:45
  • Thank you for this, I am having this question since your first reply but couldn't get it answered from vendor yet.I think this should solve my many issues. – user3174598 Dec 09 '18 at 05:57

1 Answers1

0

Thank you Kirk for the help. I will take it as permanent solution for this problem. But I am able to collect the memory for rest of the classes is it is beyond C#. We needed to cautiously find out where all cursors are opened and release the memory right away as soon as its use is done. The problem that I had is due to some legacy code and many global variables were declared for arcobjects classes that are not releasing memory. Still I noticed that IFeatureClass is not releasing the memory. But, for now I am able to clear the memory dynamically that lets the process continue as long as possible.