DanielG
DanielG

Reputation: 1237

c# - How to get the amount of memory available for my application

I found out that I can read the available physical memory by the ComputerInfo.TotalPhysicalMemory Property. http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.devices.computerinfo.totalphysicalmemory.aspx

But is there any way to find out the memory that is actually available for my application. So, that for example, I can react if I'm running out of memory.

Is that possible? Or is that just bad practice.

Thanks in Advance

Upvotes: 3

Views: 1205

Answers (1)

Enigmativity
Enigmativity

Reputation: 117010

If you read the Eric Lippert article you'll hit this important paragraph:

An “out of memory” error almost never happens because there’s not enough storage available; as we’ve seen, storage is disk space, and disks are huge these days. Rather, an “out of memory” error happens because the process is unable to find a large enough section of contiguous unused pages in its virtual address space to do the requested mapping.

There is no way to know if there are contiguous unused pages available.

The best thing you can do is die gracefully when you have an "Out of Memory" exception.

Upvotes: 6

Related Questions