My code:
var list = new LinkedList<int>();
var ramCounter = new PerformanceCounter("Memory", "Available MBytes");
while (true)
{
for(int i = 0; i < 1000 * 1000; i++) list.AddLast(0);
Console.WriteLine(ramCounter.NextValue());
}
Questions:
- The documentation seems to say I can use a PerformanceCounter only as an Administrator, but I ran my application as a Standard User and it worked. Can I rely on that?
- I consistently get OutOfMemoryException when about 200 MB RAM remain, why? It can't be due to fragmentation because I allocate an int at a time. Also, it can't be because of addressability issues, as I'm already way above 2GB (the exception gets thrown when 2.8 out of my 3GB are taken). The numbers output by the test app were verified with a Task Manager window running at the side.
- Once I got a BSOD when the test app was running and about 400 MB were remaining. Any hints as to what could possibly cause this? I'll run a check for RAM integrity, anything else? Should I be careful with calling
PerformanceCounter.NextValue()
in a loop, or something? Note, that's the first time I get a BSOD on this PC. - At some points in the program execution, I get big delays. E.g. when I start at 1 GB free RAM, when I get to 700 MB the app freezes for 1 sec, then at about 400 MB it freezes for about 4 sec. Why is this? Because the OS needs to swap out disk caches to free up memory, or something?
Note: Why am I doing this? Well, I want my memory-intensive app to detect when 5 MB RAM remain, and alert the user with "Memory is low, please close other programs and come back, or this program will fail."