Short answer:
Use GlobalMemoryStatusEx
Win32 API's ullAvailPageFile
More details:
System.Diagnostics.PerformanceCounter("Memory", "Available MBytes");
The above gives the "Physical memory"
https://blogs.msdn.microsoft.com/faber/2014/11/20/tracking-windows-performance-counters-by-application/
This measures the amount of physical memory, in megabytes, available for running processes.
There is always "Virtual Memory" in your machine and it is likely to be configured with the "Page File"
The appropriate measure that a program could use is: Available Page File
https://msdn.microsoft.com/en-us/library/windows/desktop/aa366770(v=vs.85).aspx
ullAvailPageFile
The maximum amount of memory the current process can commit, in bytes. This value is equal to or smaller than the system-wide available commit value.
I can't find a Managed library call to get this info. So, an available option to to directly call the Win32 API using pinvoke.
https://www.pinvoke.net/default.aspx/kernel32.GlobalMemoryStatusEx
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool GlobalMemoryStatusEx( [In, Out] MEMORYSTATUSEX lpBuffer);
In my local testing, I see that if the Available Page Size is less than 1 GB, the program can start to fail with "Out of memory" exception anytime.
Note this value is not a fixed value, it would change as your program / other program runs in the system. My understanding is that the as you allocate memory, the "Page File" grows (till its configured limit).
You may want to built your application as "64 bit" application to use more memory.
- http://blogs.microsoft.co.il/sasha/2012/04/04/what-anycpu-really-means-as-of-net-45-and-visual-studio-11/
- .net console app 32 vs 64 bit
For the Console App, make it 'Any CPU' and clear the 'Prefer 32-bit' check box
Page file settings
There are extensive info on the appropriate Page File size. In most simple cases, it is set as a multiple of available RAM.
https://support.microsoft.com/en-in/help/2860880/how-to-determine-the-appropriate-page-file-size-for-64-bit-versions-of
Windows 8.1 and Windows Server 2012 R2
Maximum page file size
3 × RAM or 4 GB, whichever is larger.