Skip to main content

Notifications

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

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,868 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.

  • Suggested answer
    Anton Venter Profile Picture
    Anton Venter 19,493 Super User 2025 Season 1 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
        }
    }

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,031 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,868 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans