0

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.

Jacko
  • 11,326
  • 15
  • 66
  • 112
  • May also be related: [C# memory usage](http://stackoverflow.com/questions/3803003/c-sharp-memory-usage) – Brad Christie Nov 13 '12 at 21:33
  • Consider what happens if your OS has set itself a target of 100 MB free memory. You will be giving away your caches all of the time. I find it more useful to have a configurable self-enforced memory use limit on your applicaton. –  Nov 13 '12 at 22:09

1 Answers1

2

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

Community
  • 1
  • 1
Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859