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
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.
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 } }
André Arnaud de Cal...
292,031
Super User 2025 Season 1
Martin Dráb
230,868
Most Valuable Professional
nmaenpaa
101,156