Search code examples
c#weak-references

How to stop weak references being lost even though I have plenty of available memory


I have an application with many wpf controls but only few are visible at a single time. As users scrolls trough items we are "caching" images in weak references per view model.

Even though working set is 260MB and there is plenty of RAM available objects referenced by weakreferences are garbage collected. Process is running as x86.

Is there any reason why is GC so agressive?

This is temp solution of course and we are working on custom caching service but I am surprised by this behavior.


Solution

  • Thanks Yuval for your comment. I have made it an answer to help someone else believing weak references can be used for caching even if there is plenty of memory available.

    Unfortunately weak references are being garbage collected even if your application is nowhere near memory limit. It seems weak references are always collected with generation 0 sweep.