Reading the documentation, it seems that the cache is not trying to calculate the size of the objects that it caches. This makes sense because it is not something that can be made from the process itself for arbitrary types (you can do this for fixed-size structures or arrays of fixed-size structures, but that's about it); a little search query will confirm this to you. However, he knows how much RAM is available on the computer; you can get this from new Microsoft.VisualBasic.Devices.ComputerInfo().AvailablePhysicalMemory . Therefore, apparently, the cache does two things:
- It keeps track of when the last object was last used.
- Surveys on memory statistics at a certain interval.
Then, for each survey, either the amount of available memory is within acceptable limits or not. If it is within acceptable limits, it does nothing. If it does not start deleting items, the last item that was the last will be deleted first. It continues to delete items until the memory returns to acceptable limits.
If you think about it, that's almost all you can do with the information available in the cache.
This strategy is fine, but it obviously breaks if you have other objects that contain references to items in the cache, because deleting an item from the cache will not free it for garbage collection. This is a callback point to perform a cleanup to ensure that there are no more object references.
source share