linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "linux-os \(Dick Johnson\)" <linux-os@analogic.com>
To: "Jie Zhang" <jzhang918@gmail.com>
Cc: "Linux kernel" <linux-kernel@vger.kernel.org>, <lars.friedrich@wago.com>
Subject: Re: Question on the current behaviour of malloc () on Linux
Date: Wed, 21 Dec 2005 13:06:42 -0500	[thread overview]
Message-ID: <Pine.LNX.4.61.0512211259090.12113@chaos.analogic.com> (raw)
In-Reply-To: <6f48278f0512210936y25169c37t9fb7eb13fef3a97d@mail.gmail.com>


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.

>
> Thanks,
> Jie
>


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.

An even shorter program...

main ()
{
     for(;;)
         *(long *)malloc(0x0010000) = 1;
}

This will seg-fault because malloc will eventually return NULL and,
yawn, nothing except your program gets killed.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.13.4 on an i686 machine (5589.55 BogoMips).
Warning : 98.36% of all statistics are fiction.
.

****************************************************************
The information transmitted in this message is confidential and may be privileged.  Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited.  If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to DeliveryErrors@analogic.com - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

  parent reply	other threads:[~2005-12-21 18:06 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-21 17:36 Question on the current behaviour of malloc () on Linux Jie Zhang
2005-12-21 17:39 ` Arjan van de Ven
2005-12-21 18:05 ` Jesper Juhl
2005-12-21 18:07   ` Jesper Juhl
2005-12-22  7:15   ` Jie Zhang
2005-12-21 18:06 ` linux-os (Dick Johnson) [this message]
2005-12-21 19:00   ` Jesper Juhl
2005-12-22  7:21   ` Jie Zhang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.61.0512211259090.12113@chaos.analogic.com \
    --to=linux-os@analogic.com \
    --cc=jzhang918@gmail.com \
    --cc=lars.friedrich@wago.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).