.Net 4 MemoryCache Leaks with Concurrent Garbage Collection

前端 未结 4 562
刺人心
刺人心 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 13:57

    Stop-the-world garbage collection is based on determining whether a strong live reference to an object exists at the moment the world is stopped. Concurrent garbage collection usually determines whether a strong live reference to an object has existed since some particular time in the past. My conjecture would be that many strong references to objects held in WeakReferences are being individually created and discarded. If a stop-the-world garbage collector fires between the time a particular object is created and the time it's discarded, that particular object will be kept alive, but previously-discarded objects will not. By contrast, a concurrent garbage collector may not detect that all strong references an object have been discarded until a certain amount of time goes by without any strong references to that object being created.

    I've sometimes wished that .net would offer something between a strong reference and a weak one, which would prevent an object from being wiped from memory, but would not protect it from being finalized or having weak WeakReferences to it invalidated. Such references would slightly complicate the GC process, requiring every object to have separate flags indicating whether strong and quasi-weak references exist to it, and whether it has been scanned for both strong and quasi-weak references, but such a feature could be helpful in many "weak event" scenarios.

提交回复
热议问题