[ot] free memory

R

Rafal 'Raf256' Maj

What is the best way, under GCC based compilers, to get amount of free
memory, both RAM that OS can gave to my application, and total memory that
OS could gave to it.

I need to know +/- how much RAM is free and available for my application,
to optimize it's caching code (it is a bad idea to cache all data that can
be cached, because I if application will start swapping, then "cache" will
slow things down in fact).
 
T

Thomas Matthews

Rafal said:
What is the best way, under GCC based compilers, to get amount of free
memory, both RAM that OS can gave to my application, and total memory that
OS could gave to it.

By asking a GNU newsgroup. Read the FAQ and welcome.txt
below.

The _standard_ C++ language has no facilities for obtaining
the amount of available memory. One has to use platform
specific extensions. Some platforms don't offer this feature.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
R

Ron Natalie

Rafal said:
What is the best way, under GCC based compilers, to get amount of free
memory, both RAM that OS can gave to my application, and total memory that
OS could gave to it.
It's not even a GCC issue. It varies widely by the operating system. Further,
in nearly every OS I am familiar with, the number you get back is only going to
be an estimate (if you could get anything back). A multitasking system will
continually reallocate resources as necessary.
 
J

Joseph Turian

main() {
int free_memory = 0;
void *p = NULL;
while (1) if (!(p = realloc(p, ++free_memory))) break;
print("%d bytes were free.\n", --free_memory);
return 0;
}

HTH ;^)

Joseph
 
M

Mike Wahler

#include <stdio.h>
#include said:

int main() {
int free_memory = 0;

Type 'int' isn't guaranteed to be able to represent
the number of bytes of available memory on any
particular system (nor is any other type).
void *p = NULL;
while (1) if (!(p = realloc(p, ++free_memory))) break;

This loop could easily cause 'free_memory' to overflow,
producing undefined behavior. It could also conceivably
run forever, due to possible memory handling schemes
of multi-tasking systems.
print("%d bytes were free.\n", --free_memory);

printf("%d bytes were free.\n", --free_memory);
return 0;
}

Also note that aside from the problems I cited above,
that 'malloc()' is not required to allocate exactly
the requested amount of memory, it's allowed to allocate
more (iow it attempts to allocate *at least* the
requested amount). I suspect that many implementations
of 'malloc()' return more than the requested amount
in the interest of performance and/or observing operating
system requirements.

Not really. As others have already noted, what the OP
asks is *not possible* with standard C++.

-Mike
 
J

Joseph Turian

Mike,

You missed the biggest problem with the code.

Specifically, once the realloc fails and the address in p is clobbered
by NULL, it is not longer possible to free the memory that was
allocated by the penultimate realloc.
Hence, "%d bytes *were* free", not "%d bytes *are* free".

Also note the gratuitous use of winking emoticon.

I suggest check the batteries in your irony detector, which failed to
detect the high levels of irony in my post.

Joseph
 

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
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top