From: "Michał Nazarewicz" <m.nazarewicz@samsung.com>
To: Joe <longapple@gmail.com>, linux-c-programming@vger.kernel.org
Subject: Re: strange stack limit behavior when allocating more than 2GB mem on 32bit machine
Date: Fri, 21 Aug 2009 09:09:24 +0200 [thread overview]
Message-ID: <op.uyz3hymilak3lt@amdc030.digital.local> (raw)
In-Reply-To: <56b13acf0908202047k2bf536f9vf993394d42059b8e@mail.gmail.com>
On Fri, 21 Aug 2009 05:47:28 +0200, Joe <longapple@gmail.com> wrote:
> Here I encounter something which I can't understand.
> What I want to do is to allocate ~2.5GB mem, it fails when stack limit
> is unlimited, but succeeded when stack limit is 10240.
I cannot really answer your question but maybe this will give
you some more insight:
> $ free -m
> total used free shared buffers cached
> Mem: 2024 1996 28 0 30 1401
> -/+ buffers/cache: 563 1461
> Swap: 10236 0 10236
> $ ulimit -s
> 10240
> $ ./malloc 2500
> Malloc succeeded <======= succeeds when stack limit is 10240
> $ ulimit -s unlimited
> $ ./malloc 2500
> malloc failed: Cannot allocate memory <======== fails when stack
> limit is unlimited???
On 32-bit system, in standard configuration, user space programs can address
up to 3GiB of RAM (the other 1GiB is reserved for kernel). It's possible
that with unlimited stack Linux reserves more address space for stack leaving
less for heap -- this may cause malloc(3) to fail.
> BTW, there is no such problem on 64bit machine
On 64-bit system, the 3GiB address space limit described above does not exist
and thus, if I'm correct, the program will succeed.
BTW. By default Linux does not allocate memory when calling malloc(3)
(sbrk(2) in fact). The pages are allocated when they are first
referenced. This means, that even though malloc(3) returns non-NULL you
may have no access to the allocated area -- ie. program will segfault
trying to access it. Yes, this is in violation of the C standard.
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Micha³ "mina86" Nazarewicz (o o)
ooo +-----<mina86@tlen.pl>----<mina86@jabber.org>----ooO--(_)--Ooo--
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
prev parent reply other threads:[~2009-08-21 7:09 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-08-21 3:47 strange stack limit behavior when allocating more than 2GB mem on 32bit machine Joe
2009-08-21 6:58 ` Glynn Clements
2009-08-21 9:12 ` Joe
2009-08-21 9:37 ` Nicholas Mc Guire
2009-08-21 10:33 ` Michał Nazarewicz
2009-08-21 9:38 ` Michał Nazarewicz
2009-08-21 11:04 ` Joe
2009-08-21 12:21 ` Michał Nazarewicz
2009-08-22 7:44 ` Glynn Clements
2009-08-21 7:09 ` Michał Nazarewicz [this message]
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=op.uyz3hymilak3lt@amdc030.digital.local \
--to=m.nazarewicz@samsung.com \
--cc=linux-c-programming@vger.kernel.org \
--cc=longapple@gmail.com \
/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).