Re: Question on the current behaviour of malloc () on Linux

From: Jie Zhang
Date: Thu Dec 22 2005 - 02:20:11 EST


On 12/22/05, linux-os (Dick Johnson) <linux-os@xxxxxxxxxxxx> wrote:
>
> On Wed, 21 Dec 2005, Jie Zhang wrote:
>
> > Hi,
> >
> > I first asked this question on uClinux mailing list. My first question
> > is <http://mailman.uclinux.org/pipermail/uclinux-dev/2005-December/036042.html>.
> > Although I found this issue on uClinux, it's also can be demostrated
> > on Linux. This is a small program:
> >
>
> Another FAQ....
>
> > $> cat test2.c
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > int
> > main ()
> > {
> > char *p;
> > int i, j;
> > for (i = 0;; i++)
> > {
> > p = (char *) malloc (8 * 1024 * 1024);
> > if (p)
> > for (j = 0; j < 8 * 1024 * 1024; j++)
> > p[j] = 0x55;
> > else
> > {
> > printf ("%d fail\n", i);
> > break;
> > }
> > }
> > return 0;
> > }
> >
> > When I run it on my Linux notebook, it will be killed. I expect to see
> > it prints out "fail".
>
> Your expectations are not based upon any logic, only wishes.
>
According to SuSv3, I think my expectation is completely based upon
logic. Actually, overcommitting is based upon wishes I think. It
wishes application not use all the memory it allocated.
>
>
> To make your wishes come true execute:
> echo "1" >/proc/sys/vm/overcommit_memory
> ... as a super-user.
>
> That will make malloc() fail when there isn't any more virtual
> memory.

That's what I'm looking for. Thanks.

Jie
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/