What are the rules for releasing ArcObjects from memory in .NET?

Most notably, always explicitly release cursors when you are done with them. I also release some enumeration objects which imply database access, for example IEnumRelationship you get from IRelationshipClass.GetRelationshipsForObject.

Also, when you create a lot of COM instances which are short-lived (especially in tight loops), it is also a good idea to release them explicitly.

There are also scenarios when is it advisable to release individual feature (row) references. For example, if you create a new geodatabase version, edit data, reconcile&post, attempts to delete the version afterwards might fail since there might be unreleased rows, which in turn keep reference to the version (workspace) you are trying to delete. Mostly, though, such scenarios are rare and you do not need to account for them in your day-to-day ArcObjects development. It would only make the code cluttered with extraneous cleanup, making it less maintainable.

It is also important to say when not to release .NET wrappers - never explicitly release RCW of ArcObjects which might be in use by any other managed code. One example of this - do not release IMap when in ArcMap. In general, do not try to release ArcObjects which you did not create.


For the most part .NET garbage collection works well. There are some cases in ArcObjects that do important work on desctructors and the non-deterministic nature of the .NET wrappers can cause issues. This help topic covers the primary cases to be concerned about and how to manage releases.


Always destroy:

  • ICursors
  • IEnums

Be careful to not destroy something that's being used somewhere else.

Today I read a interesting discussion on ESRIs website in which Kirk participated. There were other very interesting opinions, such as the use of ReleaseComObject method and the FinalReleaseComObject (or something the like). Sorry I don't have the link on me right now.

Some even suggested to release IRows, but many agreed that it's just easier to let GC handle them directly.

I never release any IGeometry's. Anyone tried that?