Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 24 Jul 2003 13:20:26 -0400
From:      Mike Tancsa <mike@sentex.net>
To:        Andrew Reilly <areilly@bigpond.net.au>
Cc:        freebsd-stable@freebsd.org
Subject:   Re: malloc does not return null when out of memory
Message-ID:  <5.2.0.9.0.20030724114115.0849c450@209.112.4.2>
In-Reply-To: <3F1F817E.7040504@bigpond.net.au>
References:  <5.2.0.9.0.20030723234250.052821e8@192.168.0.12> <20030723173427.GA72876@vmunix.com> <20030723173427.GA72876@vmunix.com> <5.2.0.9.0.20030723234250.052821e8@192.168.0.12>

next in thread | previous in thread | raw e-mail | index | archive | help
At 04:49 PM 24/07/2003 +1000, Andrew Reilly wrote:
>Mike Tancsa wrote:
>
>>At 08:15 PM 7/23/2003 -0700, Kris Kennaway wrote:
>>
>>>On Wed, Jul 23, 2003 at 01:34:27PM -0400, Gabor wrote:
>>>
>>> > Here is the tail end of the output.  It dies when trying to poke at
>>> > the memory using memset.  If I just malloc without the memset, it
>>> > never even dies.
>>>
>>>Ah, the annual "memory overcommit" thread.  I thought we were overdue
>>>for one.
>>
>>
>>
>>But why does the man page for malloc (3) say,
>>
>>     If malloc() fails, a NULL pointer is returned.
>
>
>Because that's what happens.  See the subthread with the ulimit examples.
>
>In the cases being cited, the malloc itself is not failing, because it is 
>able to return a pointer to a chunk of *VM*.  The problem occurrs later, 
>when the program attempts to read or write to that memory, and it doesn't 
>occur because malloc was wrong, but because no other process has exited or 
>unmapped something in the mean-time, to free up a page to back that 
>address space.

Thanks for the clarification.  I guess it would be helpful if there was an 
* or caveat perhaps in the man page after  "If malloc() fails, a NULL 
pointer is returned."  I see what you mean that it "never fails" in the 
strict sense, but there is a potential for a failure in the process of 
memory allocation and usage which I think is helpful for the programmer / 
user to understand.

What if something like this were added to
/usr/share/man/man3/malloc.3.

Note: malloc will return a pointer to memory even when there is not enough 
actual physical memory and swap to handle the request. It will only fail 
once you actually try to access that allocation and if by that time the 
system has not freed up memory from elsewhere to honor that request.  The 
idea behind this strategy being that at any given time programs will ask 
for more memory than there is actually available, and more memory than they 
actually need.  By having the VM tell the program, "yes, you can have this 
memory allocation", the chances are by the time the program actually uses 
the memory, it will be available as some other program has freed up their 
memory allocation.  For further information, see *****


         ---Mike 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?5.2.0.9.0.20030724114115.0849c450>