web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Dispose Map Class

(0) ShareShare
ReportReport
Posted on by


I need to use the map class in a loop where I need to create a new instance per iteration.  But I want to make sure that I dispose of the last instance.

MyMap = new Map(Types::Int64, Types::Container);


So I think setting the Map to null at the end of the loop should clear it for reuse at the beginning of the loop.

MyMap = null;

I think that does the trick.

Does that sound right or is there a better way?

Thanks

I have the same question (0)
  • Suggested answer
    Anton Venter Profile Picture
    20,343 Super User 2025 Season 2 on at

    You don't have to explicitly dispose the Map object by setting to null or anything like that as the system will take of that for you. If your loop does not contain many iterations, you should in general be okay becaues the system should release the memory when the Map object goes out of scope. If you are concerned about possible over memory usage, you might conside using the X using statement.

    for (Counter cntr = 1; cntr < loopCount; cntr  ) //dummy loop
    {
        using (Map m = new Map())
        {
            //do something
        }
    }

  • Suggested answer
    Martin Dráb Profile Picture
    237,697 Most Valuable Professional on at

    It doesn't really matter. The only difference is that the object will stop being referenced a few microseconds earlier than if you overwrite the value at the beginning of the loop body. The actual garbage collection will likely happen much later.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 683 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 563 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 398 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans