Is there a way to obtain the remaining free stack / heap memory in C# using only managed code?
To be specific, I do not mean the memory that is still available in the currently allocated state but all the memory that (in the future) could be allocated (if necessary) based on the main memory of the host system.
The information will be used to take measures on systems with low free memory to prevent running out of system memory.
There is a method called virtualquery that can be used to determine the size of the call stack. There are a bunch of C# examples here.
Checking available stack size in C
Checking stack size in C#
For big heap allocations you could try the MemoryFailPoint which checks to see if allocation is possible and throws a different exception then OOM
http://msdn.microsoft.com/en-us/library/system.runtime.memoryfailpoint.aspx