Search code examples
c#.netmemory-leaksarcobjects

How to manage memory in c# and COM objects


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


Solution

  • 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.