Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 24 Jul 2003 14:01:06 -0400
From:      Gabor <gabor@vmunix.com>
To:        Darryl Okahata <darrylo@soco.agilent.com>
Cc:        freebsd-stable@freebsd.org
Subject:   Re: malloc does not return null when out of memory
Message-ID:  <20030724180106.GA86680@vmunix.com>
In-Reply-To: <200307232222.PAA26360@mina.soco.agilent.com>
References:  <20030723221336.GA26555@pit.databus.com> <200307232222.PAA26360@mina.soco.agilent.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, Jul 23, 2003 at 03:22:30PM -0700, Darryl Okahata wrote:
# Barney Wolff <barney@databus.com> wrote:
# 
# > Shouldn't malloc return 0 when out of memory rather than returning
# > an address that won't work?  I believe that was the complaint.  Presumably
# > having NO_SWAPPING has something to do with it, but filling the swap
# > might well do the same thing.
# 
#      Well, it goes against the man page, but FreeBSD typically never
# returns NULL [*] when you run out of VM.  It's been this way for YEARS.
# 
# [*] -- Yes, it's annoying as h*ll, but that's the way it works.  I
#        thought that there was a FAQ or handbook entry on this, but I
#        couldn't find any.

Here is another test.  This box has swap and I get a null pointer even
though ulimit -a says memory unlimited.

5230 0x27ef1000
5231 0x27f0a000
5232 0x27f23000
5233 0x27f3c000
5234 0x27f55000
5235 0x27f6e000
5236 0x27f87000
5237 0x27fa0000
5238 0x27fb9000
5239 0x27fd2000
5240 0x27feb000
5241 0x28004000
5242 0x2801d000
5243 0x0
No more mem
=0= 6g # ulimit -a
time(cpu-seconds)    unlimited
file(blocks)         unlimited
coredump(blocks)     unlimited
data(kbytes)         524288
stack(kbytes)        65536
lockedmem(kbytes)    unlimited
memory(kbytes)       unlimited
nofiles(descriptors) 11095
processes            5547
vmemory(kbytes)      unlimited

#include <stdlib.h>

int
main(int argc, char **argv)
{
        char *p;
        int cnt = 0;

        while (1) {
                p = malloc(100000);
                printf("%d %p\n", ++cnt, p);
                if (!p) {
                        puts("No more mem");
                        break;
                }
        }
        return 0;
}

=0= 6g # sysctl vm.swap_enabled
vm.swap_enabled: 1
=0= 6g # sysctl hw.physmem
hw.physmem: 1061859328
=0= 6g # sysctl hw.usermem
hw.usermem: 906907648
=0= 6g # pstat -sk
Device          1K-blocks     Used    Avail Capacity  Type
/dev/twed0s1b     1048448        0  1048448     0%    Interleaved



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