With MemoryFailPoint
, you can tell .NET you are going to need a certain amount of memory. The problem though is that even this system includes swap space.
I believe that it is very difficult to achieve what you want to achieve here. Say, you would use some system indicators and performance indicators to find out how much physical memory is available and based on that, perform some tasks. If after you've completed this check a different process comes in a grabs physical memory, your original calculations do not apply anymore and some of your memory is going to be pushed to swap.
I do have a suggestion. You could have a configuration setting with the maximum allowed amount of memory to be used by the application? With this, you can:
Try to figure out how much resources your application consumes based on e.g. a network connection (if your application is a network server) and throttle the number of connections based on the maximum memory consumption, or
You could have a second thread running that checks ever 10 seconds or minute of so the total memory consumption with GC.GetTotalMemory()
and start rejecting connections (again, if your application is a network server) once you get to that maximum.
This should be a configuration setting instead of e.g. the amount of physical memory available, because you do not know what other applications are running on the machine.