I just need to know how much is, man, nothing more.
Since the term "available physical memory" doesn't mean anything
specific enough for an answer, what you want to use the answer for
is very relevant. It could just as well mean "how much PC200 memory
can you FedEx me by the end of the week"?
Just not to risk to allocate something in vitual memory.
If the system has virtual memory, you MUST allocate virtual memory
space or you can't use the memory. You probably want to make sure
it won't get swapped to disk. Well, no matter how little you
allocate, some other program or programs can start up after your
program got this info and force that memory to disk. In particular,
if ten copies of your program start up, they all inquire about
"available physical memory", and all then allocate that much, 90%
of that memory is going to wind up swapped onto disk, as together
they just allocated 10 times more physical memory than was available.
There is no guarantee whatever, no matter how much physical memory
is "available", that you'll get any (or more than a page or two)
of it (even if malloc() succeeds it doesn't guarantee PHYSICAL
memory), or that you will hang on to it for any period of time
(because another program using your strategy ALSO asked for "available
physical memory" and tried to grab it all.
There's all sorts of possible interpretations of your question:
- The total amount of physical memory allocatable but not yet allocated.
- The total amount of physical memory allocatable to a SINGLE
UNPRIVILEGED PROCESS at one time.
- The total amount of physical memory allocatable to a SINGLE
PRIVILEGED PROCESS at one time.
- The total amount of physical memory that can be allocated and
locked (no swapping out) by a process at one time.
- The total physical memory allocatable MINUS the total virtual memory
allocated. The result of this is likely negative. This means that
all of the already-allocated memory won't fit in physical memory,
but it doesn't have to mean that there will be a lot of swapping
going on.
- The total physical memory allocatable MINUS the total virtual memroy
that will be allocated by the time this program finishes running.
This requires precognition.
But... what I need to do shouldn't be quite out if your business??
What you asked for was vague and the use you're going to make of
the result is needed to make sense of it. Example: if you ask me
to sell you a power cord, it's relevant what country you intend to
use it in (so the plug will fit a wall outlet of the type used in
that country) and what the other end is supposed to plug into
(there's all sorts of incompatible connectors for that).