tags:

views:

61

answers:

1

Is there a portable way, that works on both .Net and Mono on the various supported operating systems, for a program to tell how much RAM (i.e. physical not virtual memory) is available in the machine it's running on?

The context is a program whose memory requirement is "as much as possible please"; it needs to know how much memory it should aim to allocate, while stopping short of going into swap (thereby grinding to a halt and locking up the whole machine).

+2  A: 

There's no portable way to do that. Even more, the system could interpret 'As much as possible' in a number of ways because of the complexity of memory usage. For example the system could make almost entire RAM available to you by simply moving every running process to swap.

P.S. using swap is not the same as locking up the whole machine. Think of it as just a slower type of RAM available to use.

buratinas
Trust me, when you're trying to do heavy crunch on a machine with a consumer grade disk system, going into swap is the same as locking up the whole machine ;-) but fair enough, I'll just default to something conservative and provide a commandline option for the user to tweak.
rwallace
do you want to implement some kind of cache? if disk access is involved in your operations you should leave caching for the OS as it does it anyways. You can even open mmapped file to store your temporary results. However you should try to minimize any kind of temporary results because it deteriorates the performance by causing processor cache misses.
buratinas