tags:

views:

114

answers:

3

Can we Determine the availability of sufficient memory for an operation? if yes, then how can? Thanks

+3  A: 

For checking the available memory, I would suggest looking at this, this, this, but basically all you need to do is use a performance counter and do this:

PerformanceCounter pc = new PerformanceCounter("Memory","Available Bytes");
long availableMemory = Convert.ToInt64(pc.NextValue());
Console.WriteLine("Available Memory: {0}", availableMemory);

If you don't know how much memory the operation needs, though, checking available memory won't help you.

Banang
Thanks...........
Muhammad Akhtar
+5  A: 

No, you absolutely cannot do this as you don't know in advance how much memory the operation will consume. If anyhow you know exactly how much memory the operation will consume you could query the available system memory and make an approximation but don't rely on it. Remember that Garbage Collection is pretty indeterministic and might kick in at any moment messing up with your approximations. You could get an OutOfMemoryException anytime.

So focus on writing quality code instead of this.

Darin Dimitrov
Thanks.........
Muhammad Akhtar
+1  A: 

You can check that there is not enough, if you have the minimum requirement and use the code from Banang.

But say you check the memory, next line you start your opperation, in the time between these 2 lines run another process starts that eats memory. You will then risk getting an out of memory exception.

Shiraz Bhaiji