views:

248

answers:

1

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:

  1. 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?
  2. 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.
  3. 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.
  4. 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."

A: 

I can't address all your questions, but here goes

2) Are you on 32 or 64 bit Windows? It sounds like you're running a large address aware process with access to 3 GB (i.e. on 32 bit Windows). In any case you have to keep in mind that memory is allocated in chucks of various sizes and your heap usage is not the only source. The CLR itself has numerous structures, each thread has a stack and so on. In any case, you can't really expect to be able to use exactly 3 GB,

3) BSOD is due to driver or kernel errors. AFAIK your application cannot cause BSOD, so this is most likely unrelated.

4) If you use a lot of memory the GC will have a hard time keeping up. As user threads are suspended while certain parts of GC is in progress this will slow down your application significantly.

Brian Rasmussen
I'm using a 32-bit Windows - could you elaborate how this can be a problem? I didn't get your explanation. Also, even if I can't expect to be able to use exactly 3GB, I CAN expect PerformanceCounter to return near-zero when I'm out of memory, right? As to GC, I guess that's correct, but it's a bit of a downer to have GC eat up huge processing time for little reason.
Stefan Monov
Memory allocation is a complex matter. I recommend that you take a look at Mark Russinovich's series on the matter at http://blogs.technet.com/markrussinovich/archive/2008/07/21/3092070.aspx as this is not really a subject to be discussed in comments.
Brian Rasmussen
The GC is running because you're using and holding on to a lot of memory. That is how GC works. As it rearranges memory, it needs to suspend user threads to avoid compromising state.
Brian Rasmussen