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 14 20733
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
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>
>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
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)
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)
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
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...
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
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
>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
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.
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
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 =---
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Barry Anderberg |
last post by:
I'm using the .NET Memory Profiler by Sci Tech and I wrote a little
test application to verify something odd I observed and it appears
that System.Drawing.Font fails to dispose of its FontFamily.
...
|
by: lwickland |
last post by:
Summary: System.Net.ScatterGatherBuffers.MemoryChuck allocates inordinately
large bytes when sending large post data.
The following application consumes inordinate quantities of memory. My code...
|
by: Harsh_forC |
last post by:
hello folks,
when i'm using 'System' funtion in TC, perror function is outputting
the
following error msg..." Not enough memory"
wat does it mean? how to get rid of this situaiton?
plz help me...
|
by: ulyses |
last post by:
Let's assume I have following file:
2938929384902491233.....
923949919199191919112....
File contains INTs only. What is more they are huge. For example first
row in file may contain integer...
|
by: Raj |
last post by:
What is the purpose of file system caching while creating a tablespace?
Memory on the test server gets used up pretty quickly after a user
executes a complex query(database is already activated),...
|
by: Baron Samedi |
last post by:
I want to produce a piece of software for embedded systems, generally
telecoms based, mostly running on ARM processors, but I can't guarantee
that, of course.
My software should work along with...
|
by: =?Utf-8?B?UGlnZ3k=?= |
last post by:
Hi to all,
I am getting this System.OutOfMemoryException calling the
Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(<stream>,<Obj>) method.
The type of <streamis...
|
by: DanielGifford |
last post by:
Hi
I'm making some basic cryptography software and I've run into a snag.
I'm trying to retrieve some sort of system number thats specific to
the system I'm working on.
It can't change over...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |