Is there any way to get GC.Collect() to be called before throwing an OutOfMemoryException?
I suppose I'm looking for a way for it to do the following code flow:
Try to Allocate Memory
On Pass Return
Call GC.Collect()
Try to Allocate Memory
On Fail Throw New OutOfMemoryException()
I'm writing a caching implementation and currently I'm running into memory exceptions so currently to resolve it I am using:
If GC.GetTotalMemory(False) >= cache.CacheMemoryLimit + (100 * 1024 * 1024) Then
// When Total Memory exceeds CacheMemoryLimit + 100MB
GC.Collect()
End If