views:

438

answers:

4

is there anyway i can have my application tell how much memory the user has and if the application is getting close to taking up a high percentage of that.

also, how do you know how much memory the machine gives to OS, video cards, etc . .

for example, if you have 4gb of memory, how much actual memory is given to applications, can you configure this.

A: 

I think you can use WMI to get all that information

Baget
A: 

If you don't wish to use WMI, you could use GlobalMemoryStatusEx():

Function Call:
http://www.pinvoke.net/default.aspx/kernel32/GlobalMemoryStatusEx.html

Return Data:
http://www.pinvoke.net/default.aspx/Structures/MEMORYSTATUSEX.html

MemoryLoad will give you a number between 0 and 100 that represents the ~ percentage of physical memory in use and TotalPhys will tell you total total amount of physical memory in bytes.

Memory is tricky because usable memory is a blend of physical (ram) and virtual (page file) types. The specific blend, and what goes where, is determined by the operating system. Luckily, this is somewhat configurable as Windows allows you to stipulate how much virtual memory to use, if any.

Take note that not all of the memory in 32-bit Windows (XP & Vista) is available for use. Windows may report up to 4GB installed but only 3.1-3.2GB is available for actual use by the operating system and applications. This has to do with legacy addressing issues IIRC.

Good Luck

Robert Venables
+1  A: 

is there anyway i can have my application tell how much memory the user has and if the application is getting close to taking up a high percentage of that.

Yes, it's possible (see some of the other answers), but it's going to be very unlikely that your application really needs to care. What is it that you're doing where you think you need to be this sensitive to memory pressure?

also, how do you know how much memory the machine gives to OS, video cards, etc . .

Again, this should be possible using WMI calls, but the bigger question is why do you need to do this?

for example, if you have 4gb of memory, how much actual memory is given to applications, can you configure this.

No, this isn't a configurable value. When a .NET application starts up the operating system allocates a block of memory for it to use. This is handled by the OS and there is no way to configure the algorithms used to determine the amount of memory to allocate. Likewise, there is no way to configure how much of that memory the .NET runtime uses for the managed heap, stack, large object heap, etc.

Scott Dorman
+2  A: 

I think I read the question a little differently, so hopefully this response isn't too off topic!

You can get a good overview of how much memory your application is consuming by using Windows Task Manager, or even better, Sysinternals Process Monitor. This is a quick way to review your processes at their peaks to see how they are behaving.

Out of the box, an x86 process will only be able to address 2GB of RAM. This means any single process on your machine can only consume up to 2GB. In reality, your likely to be able to consume only 1.5-1.8 before getting out of memory exceptions.

How much RAM your copy of Windows can actually address will depend on the Windows version and cpu architecture.

Using your example of 4GB RAM, the OS is going to give your applications up to 2GB of RAM to play in (which all processes share) and it will reserve 2GB for itself.

Depending on the operating system your running, you can tweak this, using the /3GB switch in the boot.ini, will adjust that ratio to 3GB for applications and 1GB for the OS. This has some impact to the OS, so I'd review that impact first and see if you can live with tradeoff (YMMV).

For a single application to be able to address greater than /3GB, your going to need to set a particular bit in the PE image header. This question/answer has good info on this subject already.

The game changes under x64 architecture. :)

Some good reference information:

Memory Limits for Windows Releases

Virtual Address Space

Zach Bonham