web
You’re offline. This is a read only version of the page.
close
Skip to main content
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,303 Super User 2025 Season 2 on at
    RE: Dispose Map Class

    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,002 Most Valuable Professional on at
    RE: Dispose Map Class

    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…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

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

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 582 Most Valuable Professional

#3
CU05031448-0 Profile Picture

CU05031448-0 526

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans