By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,159 Members | 1,941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,159 IT Pros & Developers. It's quick & easy.

Get System Memory in C

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Alessandro Monopoli <te*******@ammaza.com> scribbled the following:
Hi all, I'm searching a PORTABLE way to get the available and total physical memory.


There isn't one.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Shh! The maestro is decomposing!"
- Gary Larson
Nov 14 '05 #2

P: n/a
Alessandro Monopoli wrote:
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

--

Steve Graegert {C/C++ && Java && .NET}
CSI Technology Group (StReG)
<graegerts(AT)cs(DOT)technologies(DOT)de>
Nov 14 '05 #3

P: n/a
>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
Nov 14 '05 #4

P: n/a
Alessandro Monopoli wrote:
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)

Nov 14 '05 #5

P: n/a
Alessandro Monopoli wrote:
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)

Nov 14 '05 #6

P: n/a
jacob navia <ja***@jacob.remcomp.fr> scribbled the following:
Alessandro Monopoli wrote:
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)


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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"C++ looks like line noise."
- Fred L. Baube III
Nov 14 '05 #7

P: n/a
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...
Nov 14 '05 #8

P: n/a
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??
"Gordon Burditt" <go***********@burditt.org> ha scritto nel messaggio
news:cm********@library2.airnews.net...
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

Nov 14 '05 #9

P: n/a
Alessandro Monopoli <te*******@ammaza.com> scribbled the following:
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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Make money fast! Don't feed it!"
- Anon
Nov 14 '05 #10

P: n/a
>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).
>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

Nov 14 '05 #11

P: n/a
bd
Gordon Burditt wrote:
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.


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.

Nov 14 '05 #12

P: n/a
On 31 Oct 2004 18:47:38 GMT, Joona I Palaste
<pa*****@cc.helsinki.fi> wrote:
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
Nov 14 '05 #13

P: n/a
On Sun, 31 Oct 2004 19:40:51 GMT, in comp.lang.c , "Alessandro Monopoli"
<te*******@ammaza.com> wrote:
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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #14

P: n/a
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
jacob navia <ja***@jacob.remcomp.fr> scribbled the following:
Alessandro Monopoli wrote:
I'm searching a PORTABLE way to get the available and total physical memory.
while (p) {
siz++; // Can be more to speed up things
p = realloc(p,siz);
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
Nov 14 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.