Select to view content in your preferred language

Memory Leak on referencing IMap instance

1859
6
03-25-2021 08:08 PM
mannokmannok
New Contributor II

var pMd = new MapDocumentClass();
pMd.Open(RevisionState.CurrentBackupMXDPath_.BSValue());
var pMap = pMd.Map[0];
pMd.Close();
Marshal.FinalReleaseComObject(pMap);
pMap = null;
Marshal.FinalReleaseComObject(pMd);
pMd = null;
GC.Collect();
GC.WaitForPendingFinalizers();

 

Please see the code above. I found that memory usage increased so much while executing var pMap = pMd.Map[0];. However, I cannot release these memory even I run all the rest of the statement. May I know how can I release IMap reference? Thanks!

0 Kudos
6 Replies
KirkKuykendall1
Occasional Contributor III

Maybe try calling Collect() and WaitforPendingFinalizers before closing, as described here:

https://community.esri.com/t5/arcobjects-sdk-questions/layers-from-sde-database-are-shown-as-invalid...

0 Kudos
mannokmannok
New Contributor II

Thank you @KirkKuykendall1 

Have tried just now but no luck... Memory usage of this statement cost me 50000KB of memory. Even I run the rest statements in this order...

var pMd = new MapDocumentClass();
pMd.Open(RevisionState.CurrentBackupMXDPath_.BSValue());
var pMap = pMd.Map[0];
GC.Collect();
GC.WaitForPendingFinalizers();
pMd.Close();
Marshal.FinalReleaseComObject(pMap);
pMap = null;
Marshal.FinalReleaseComObject(pMd);
pMd = null;

 

The memory usage drops only ~1000KB   😞

 

0 Kudos
mannokmannok
New Contributor II

Anyone help me out? 😞

0 Kudos
BrentHoskisson
Occasional Contributor III

At the very end of all of my projects in "Dispose" of my form with the map I always include

ESRI.ArcGIS.ADF.COMSupport.AOUninitialize.Shutdown();

This is in the ESRI.ArcGIS.ADF.Local resource.  

I have never had an issue and I don't need to do the garbage collecting and marshalling.

Brent Hoskisson

 

0 Kudos
mannokmannok
New Contributor II

This is because you are playing with a short-run process. For me, I am working with a long run process which lives as long as the lifetime of an ArcMap session.

0 Kudos
DuncanHornby
MVP Notable Contributor

What is in the map? Do you have basemaps displaying, layers linking to say datasets stored in ArcGIS Online or ArcServer that maybe maintaining a link thus not allowing memory to be released? I would try a simple map with say a locally stored shapefile as  a test. See if you can track the problem down.

 

0 Kudos