开发者

Garbage collection not functioning as expected

This may likely be an issue with my inexperience using a Managed Language开发者_StackOverflow. The issue essentially is a loop within an objects method, which executes over about 20 seconds, throughout the duration of this loop the programs overall memory usage constantly goes up. Now all the variables within the loop which are modified are variables defined within the loops scope (ie. no class members are changed/re-allocated within the loop). After the entire method completes the excess memory is still being used.

I have absolutely no idea why/where this issue is but here are some things which may be a factor:

  1. I use fonts within the loop, but I '.Dispose()' of them and have verified that there is no GDI leak.
  2. I have try/catch statements which are in active use.
  3. Objects are allocated...

So again, any ideas on where this issue may be coming from would be very helpful, I would post code but there is a lot of it. Also as mentioned above the memory is not cleaned up after the method call is done, and even after the object on which the method was invoked has gone out of scope.

Edit

I also just tried the GC.Collect() method, and nothing has changed in the overall result. I have no idea, but does this mean that the memory is not considered 'Garbage'? Again all the allocation is done within the scope of the loop so shouldn't it be considered garbage after the loop terminates. I understand the GC won't immediately get to cleaning it up, but using the GC.Collect() call should force that?


.NET uses traced garbage collection instead of the classic reference counting mechanism.

As soon as your .NET code releases an object or data, it doesn't get cleaned up instantly. It sits around for a while before being cleaned up. The garbage collector is a separate entity wandering around.

Microsoft states about the garbage collector

However, memory is not infinite. Eventually the garbage collector must perform a collection in order to free some memory.

The garbage collector will come around at its own leisure based on complex algorithms. It will eventually clean everything up, if not at the end of the program lifetime. It's not recommended we poke or prod the garbage collector through System.GC members because we're supposed to assume it knows best.


Garbage collector will release objects if there is no pointer pointing to them. Be sure you don't keep not necessary objects in your variables (especially in arrays).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜