What is the best ways to get notified about the critical memory usage / remaining available memory? I'm currently working on a cache implementation, where cache size should adapt dynamically to available memory.

有帮助吗?

解决方案

You can use GC.RegisterForFullGCNotification() so that you will get notified when a full GC collection is imminent. See Garbage Collection Notifications on MSDN for details.

This doesn't really tell you that memory is getting low, but it does give you a good place to check it. However, it doesn't tell you when a lot of memory has been freed up so it might not be totally useful for you.

You could also look into the MemoryFailPoint class. This doesn't give you notifications, but it does let you determine if a memory allocation is likely to fail, before you actually allocate it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top