Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting Available Memory

0 views
Skip to first unread message

Mark W. Stroberg

unread,
Nov 23, 2004, 11:51:10 PM11/23/04
to
To Anyone Who Can Help:
Are there operating system functions that can be called from a C/C++
program that can give:

1. Total Physical Memory of System.
2. Available Physical Memory of System (That is amount of available memory
which must be consumed before paging out is required).
3. Available virtual memory of system.

TIA -- I am a linux newbie and know almost nothing.

Mark W. Stroberg


Kasper Dupont

unread,
Nov 24, 2004, 12:55:33 AM11/24/04
to
"Mark W. Stroberg" wrote:
>
> 1. Total Physical Memory of System.

Easy:
http://www.daimi.au.dk/~kasperd/comp.os.linux.development.faq.html#memsize

> 2. Available Physical Memory of System (That is amount of available memory
> which must be consumed before paging out is required).

No, you are wrong. The available physical memory is not
necesarilly the same as the amount of memory you can
allocate before paging out is required. The kernel may
decide to start writing to the disk before all memory
have been consumed. OTOH there might actually be caches
that can be freed with no need to access the disk.

You can use sysconf(_SC_AVPHYS_PAGES), but you'll
probably find, that most of the time it is close to
zero, and not really usable for anything.

> 3. Available virtual memory of system.

You can parse /proc/swaps or /proc/meminfo to get the
amount of swap. That is Linux specific, I don't know
any portable way to do it. I'd recommend to look for
the SwapTotal line in /proc/meminfo. If the program
runs on something else than Linux, you will just not
find a /proc/meminfo file, the program should still
work in that case.

--
Kasper Dupont

0 new messages