Get System Memory in C

  • Thread starter Alessandro Monopoli
  • Start date
A

Alessandro Monopoli

Hi all,

I'm searching a PORTABLE way to get the available and total physical memory.

Something like "getTotalMemory" and it returns the memory installed on my PC
in bytes, and
"getAvailableMemory" and it returns the available memory in bytes.

Do you know is there's a C function, a c++ Object or anything else that
compiles in Linux and Windows to get these data?

Bye and thanks!
Alessandro
 
S

Str0nG

Alessandro said:
Hi all,

I'm searching a PORTABLE way to get the available and total physical memory.

Something like "getTotalMemory" and it returns the memory installed on my PC
in bytes, and
"getAvailableMemory" and it returns the available memory in bytes.

Do you know is there's a C function, a c++ Object or anything else that
compiles in Linux and Windows to get these data?

The POSIX symbols _SC_PHYS_PAGES and _SC_PAGE_SIZE could be what you are
looking for, at least for UNIX-like operating systems:

long phypz = sysconf(_SC_PHYS_PAGES);
long psize = sysconf(_SC_PAGE_SIZE);

Of course, you need to calculate _total_ physical memory by hand then.
Unfortunately I don't know a portable solution to obtain the _available_
physical memory the same easy way.

Windows does not support all of these symbols, so you will need to call
something like GlobalMemoryStatus() which fills a MEMORYSTATUS
structure, that itself contains a member dwTotalPhys containing the
_total_ physical memory for this system and dwAvailPhys indicating the
_available_ physical memory.

Suggestions and better solutions are welcome. Please let us know what
others would recommend.

\Steve
 
G

Gordon Burditt

I'm searching a PORTABLE way to get the available and total physical memory.

I don't believe that there is even a semi-portable DEFINITION of
"available physical memory" and "total physical memory".

And what portable way is there to USE that information?

Well, you might use "total physical memory" for an asset inventory,
although it's unclear whether that includes the buffer memory on
IDE disk drives or video memory.

"available physical memory": you're going to try to allocate that
much, aren't you? Nobody said it was all contiguous, or that
you were going to be able to hog it all. And nobody says the answer
won't change by the time you can use it.

Gordon L. Burditt
 
J

jacob navia

Alessandro said:
Hi all,

I'm searching a PORTABLE way to get the available and total physical memory.

Something like "getTotalMemory" and it returns the memory installed on my PC
in bytes, and
"getAvailableMemory" and it returns the available memory in bytes.

Do you know is there's a C function, a c++ Object or anything else that
compiles in Linux and Windows to get these data?

Bye and thanks!
Alessandro
#include <stdlib.h>
size_t GetAvailableMemory(void)
{
char *p;
size_t siz = 1;

p = calloc(1,siz);
while (p) {
siz++; // Can be more to speed up things
p = realloc(p,siz);
}
free(p);
return siz;
}

This will return the maximum size of an object that can be allocated.
Available memory could be more, depending on the malloc implementation
and the state of the memory pool (fragmentation, etc)
 
J

jacob navia

Alessandro said:
Hi all,

I'm searching a PORTABLE way to get the available and total physical memory.

Something like "getTotalMemory" and it returns the memory installed on my PC
in bytes, and
"getAvailableMemory" and it returns the available memory in bytes.

Do you know is there's a C function, a c++ Object or anything else that
compiles in Linux and Windows to get these data?

Bye and thanks!
Alessandro
#include <stdlib.h>
size_t GetAvailableMemory(void)
{
char *p,*q;
size_t siz = 1;

q = p = calloc(1,siz);
while (q) {
siz++; // Can be more to speed up things
q = realloc(p,siz);
if (q)
p = q;
}
free(p);
return siz;
}

This will return the maximum size of an object that can be allocated.
Available memory could be more, depending on the malloc implementation
and the state of the memory pool (fragmentation, etc)
 
J

Joona I Palaste

jacob navia said:
#include <stdlib.h>
size_t GetAvailableMemory(void)
{
char *p;
size_t siz = 1;
p = calloc(1,siz);
while (p) {
siz++; // Can be more to speed up things
p = realloc(p,siz);
}
free(p);
return siz;
}
This will return the maximum size of an object that can be allocated.
Available memory could be more, depending on the malloc implementation
and the state of the memory pool (fragmentation, etc)

This code is certainly portable, but it does not necessarily return the
maximum size of an object that can be allocated. It returns the maximum
size of an object that can be allocated *at the time realloc() was
called*. As you said yourself, the malloc() implementation, the state of
the memory pool, and other things, can affect the situation, so if
calloc() was called straight away it might be able to return more than
after a very long loop of realloc() calls.
 
J

jacob navia

There was a bug in the first version. I reposted a corrected one.

Yes, this doesn't get the total ram size, only the ram available to
the program...
 
A

Alessandro Monopoli

I just need to know how much is, man, nothing more.
Just not to risk to allocate something in vitual memory.

But... what I need to do shouldn't be quite out if your business??
 
J

Joona I Palaste

Alessandro Monopoli said:
I just need to know how much is, man, nothing more.
Just not to risk to allocate something in vitual memory.

"Virtual memory" and "physical memory" are not even defined in C. In C,
you have memory, period. Anything else is a matter of your own
implementation.
But... what I need to do shouldn't be quite out if your business??

It is the business of this newsgroup. comp.lang.c discusses only
portable C. If you want to make a distinction between physical and
virtual memory, ask in a newsgroup dedicated to your own implementation.
 
G

Gordon Burditt

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).
 
B

bd

Gordon said:
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"?


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.

Many OSes have mechanisms to guarentee an allocation - e.g. mlock() and
mlockall() on unixen. Of course, this behavior is non-portable, as is any
free memory inventory function.
 
C

Chris Croughton

This code is certainly portable, but it does not necessarily return the
maximum size of an object that can be allocated. It returns the maximum
size of an object that can be allocated *at the time realloc() was
called*. As you said yourself, the malloc() implementation, the state of
the memory pool, and other things, can affect the situation, so if
calloc() was called straight away it might be able to return more than
after a very long loop of realloc() calls.

Especially since realloc can (and often does) allocate a different block
of memory for the larger amount, thus leaving chunks of free memory of
smaller sizes lying around. Times when I've done similar things I have
started with trying to calloc the maximum possible, then done a binary
'chop' freeing the memory at each stage. It still only gets the
"biggest at this time" but is much less prone to fragmenting the memory.

But don't try it on a machine with lots of virtual memory and not much
real memory. I locked up one of my Linux boxes like that, it swapped
out almost everything else and took hours to swap enough back in that I
could get a bash prompt to kill the rogue process...

Chris C
 
M

Mark McIntyre

I just need to know how much is, man, nothing more.
Just not to risk to allocate something in vitual memory.

I believe people have answered your queston: you can't do it, not portably.
But... what I need to do shouldn't be quite out if your business??

Well in that case, don't ask here. People here like to understand why
you're doing something, because very often people are asking the wrong
question.
 
R

Richard Bos

This code is certainly portable, but it does not necessarily return the
maximum size of an object that can be allocated. It returns the maximum
size of an object that can be allocated *at the time realloc() was
called*.

Apart from which, if you pull this stunt on a multi-user system, your
sysadmin _is_ going to slap your wrists. Hard.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top