How to know amount of free memory with C ?

How to know amount of free memory with C ?

Post by dboude » Tue, 13 Feb 2001 10:19:39



thanks,
 
 
 

How to know amount of free memory with C ?

Post by Kaz Kylhe » Tue, 13 Feb 2001 14:18:34



>thanks,

This is useless, except in very primitive computing environments with
small memories.

In a multitasking OS, even if at some instance you know how much memory
is free, by the time some decision is made based on the knowledge, the
figure is out of date because some other process could have run and
allocated megabytes of space for itself.

The best strategy is simply to try to get the memory and recover from
failure.

If your program can tune itself based on the availability of memory
(e.g. choose from a range of algorithms with different time/space
tradeoffs) this is probably best left as a configuration option.

 
 
 

How to know amount of free memory with C ?

Post by Iwo Mergle » Tue, 13 Feb 2001 22:37:48



> thanks,

You can read /proc/meminfo but don't trust the values too much.
See previous post for reason. :^)

Iwo

 
 
 

How to know amount of free memory with C ?

Post by Kasper Dupon » Wed, 14 Feb 2001 18:26:48




> > thanks,

> You can read /proc/meminfo but don't trust the values too much.
> See previous post for reason. :^)

> Iwo

Don't trust it at all.

Also look what the shell command free does,
it actually reports free+buffers+cache which
on the other hand might be to much.

The memory reported as buffers and cache in
/proc/meminfo will in many cases be freed
if a program request it, but not always.

Even if there is not enough physical memory
the program may still run using swapspace.

And NEVER write a program that refuses to
run because it THINKS there is not enough
memory. If the user wants to run the program
allow him to do so, don't report errors
before you have to do because malloc returns
NULL.

--
Kasper Dupont