Reputation: 13195
I have a memory intensive c# 4.0 graphics program that must run on windows xp, so frequently is running out of memory. What is the best way of estimating the available physical memory for my process? I want to stop allocating buffers when the physical memory drops below 250 MB.
Upvotes: 0
Views: 1361
Reputation: 460018
You can use a perfomance counter, for example:
private PerformanceCounter memoryCounter =
new PerformanceCounter("Memory", "Available MBytes");
// ...
float mb = this.memoryCounter.NextValue();
float available = (mb * 1024 * 1024) - Process.GetCurrentProcess().PrivateMemorySize64;
Console.Write("RAM: {0} MB"
, (1.0 * available / 1024 / 1024).ToString("0.##"));
Have a look at this answer for more informations: https://stackoverflow.com/a/4680030/284240
Upvotes: 2