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:
PerformanceCounter.NextValue()
in a loop, or something? Note, that's the first time I get a BSOD on this PC.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."
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.