.Net 4 MemoryCache Leaks with Concurrent Garbage Collection

前端 未结 4 551
刺人心
刺人心 2021-01-30 13:24

I\'m using the new MemoryCache in .Net 4, with a max cache size limit in MB (I\'ve tested it set between 10 and 200MB, on systems with between 1.75 and 8GB of memory). I don\'t

4条回答
  •  抹茶落季
    2021-01-30 14:16

    I found this entry while searching for a similiar topic and I'm focusing on your Out of Memory exception.

    If you put an object in the cache then it still may be referencing other objects and therefore these objects would not be garbage collected -- hence the out of memory exception and probably a CPU being pegged out due to Gen 2 garbage collection.

    Are you putting "used" objects on the cache or clones of "used" objects on the cache? If you put a clone on the cache then the "used" object that possible references other objects could be garbage collected.

    If you shut off your caching mechanism does your program still run out of memory? If it doesn't run out of memory then that would prove that the objects you would otherwise be putting on the cache are still holding references to other objects hindering garbage collection.

    Forcing garbage collection is not a best practice and shouldn't have to be done. In this scenario forcing a garbage collection wouldn't dispose of referenced objects anyway.

提交回复
热议问题