From: oreillym@tartarus.uwa.edu.au (Michael O'Reilly) Subject: Re: I can malloc more memory than I have. Date: 26 Oct 1992 09:50:13 +0800
PETER E REISSNER (cs911046@ariel.yorku.ca) wrote:
: Could someone please explain the following for me.
: I suspect there is a problem with my gcc.
WEll, depending on how you look at it, it's a feature. :)
: I am using Linux version 0.97
: and gcc 2.2.2 (1st version of 2.2.2)
:
: I ran a test program to determine the largest amount of allocateable memory.
: I have 4 Mbyte of ram and an 8 Mbyte swap partition, so I expected that
: the maximum was going to be something less than 12 Mbytes. Instead, the
: program reported something between 39 and 40 Mbytes.
: I have included the program below...
:
: What it does is try to allocate memory in quantities of multiples of 1 Mbyte.
:If successful with x Mbytes, then it frees the x Mbytes and tries (x+1) Mbytes
: If unsuccessful (a NULL pointer is returned), then it reports the size that
: was unsuccessful and stops.
This is a side effect of the way UNIX (linus in particular) does
memory management. When you malloc 1 meg, all that happens that that a
few values in the kernel change, and the kernel now know that you are
allowed to use 1 more meg that you used to be able to.
You haven't actually got the memory untill you use it. If you malloc
40 megs, and only change 1 meg of it, linux will be happy.
Try re-writing your program to malloc the memory, and then memset()
over it. Your machine will die. :) The malloc() is very unlikely to
ever actually return 0. Instead it will just run slower and slower as
it runs out of memory, and eventually give a SEGV (the kernel kills
it when it REALLY runs out of memory).
: Peter Reissner
Michael