Out of Memory Errors during Merge

384
1
07-21-2020 01:31 PM
MartyTippin
New Contributor

I've built a console app using ArcObjects 10.8 to manipulate shapefiles in various ways. One of the tasks is to combine groups of shapefiles into a single shapefile, so I'm using the Merge operation. 

What I'm finding is that, no matter how I try to manage the lifetime of the COM objects created by the ArcObjects SDK calls, the memory footprint only grows larger and larger on each subsquent Merge execution until the application eventually falls over with an OutOfMemory exception.

Can anyone explain the *correct* way to manage memory when calling the ArcObjects SDK methods? 

My code looks roughly like so (but I call the `Merge()` method dozens of times with different parameters)  - apologies for the formatting...

using System;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geoprocessor;

namespace DesktopConsoleApplication1
{
 class Program
 {
 private static readonly LicenseInitializer m_AOLicenseInitializer = new LicenseInitializer();

 [STAThread()]
 static void Main(string[] args)
 {
     if (!m_AOLicenseInitializer.InitializeApplication(new[] { esriLicenseProductCode.esriLicenseProductCodeStandard }, new esriLicenseExtensionCode[] { }))
     {
       throw new Exception("ArcObjects License Initialization Failed");
     }
     MergeShapefiles(new[] { "file1.shp", "file2.shp", "file3.shp" }, "outputFile.shp");
 }

 private static void MergeShapefiles(string[] inputFiles, string outputFile)
 {
    object sev = null;
    using (var releaser = new ComReleaser())
    {
      var gp1 = new Geoprocessor
      {
         OverwriteOutput = true,
      };

      releaser.ManageLifetime(gp1);

      var processor = new Merge(string.Join(";", inputFiles), outputFile);
      releaser.ManageLifetime(processor);

      var result = gp1.Execute(processor, null);
      releaser.ManageLifetime(result);

      var successMessages = gp1.GetMessages(ref sev);
   }
 }
 }
}

Tags (1)
0 Kudos
1 Reply
DuncanHornby
MVP Notable Contributor

Just throwing out ideas, instead on creating and destroying the geoprocessor on every call, why not  create it once?