Test program to examine MemoryCache eviction performance under different garbage collection scenarios.Summary
Please see the original StackOverflow question
Also see the submitted Microsoft Connect issue
The new .Net 4 MemoryCache do not evict objects when size limits are reached in certain cases -- in some cases, to the effect that an OutOfMemoryException can occur. This simple console test application demonstrates at least one such case.
The test routines are setup to be run with different garbage collection settings (set in the app.config), which will effect the behavior of the MemoryCache evictions. See the code for details on how the tests are run. Run the code to see actual results.
To change the garbage collection options for the test cases (and effect different MemoryCache eviction behaviors):
- Change (in the app.config), gcConcurrent enabled -- true (the default) will display leaky behavoir, false evictions will occur as expected.
- Swap gcConcurrent for gcServer, with enabled = true. Will behave close to when gcConcurrent is enabled
- Set forceGc = true in the test cases. This will force evictions, for any gc setting.