linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Michał Nazarewicz" <m.nazarewicz@samsung.com>
To: Joe <longapple@gmail.com>, Glynn Clements <glynn@gclements.plus.com>
Cc: 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 11:38:10 +0200	[thread overview]
Message-ID: <op.uy0adwzdlak3lt@amdc030.digital.local> (raw)
In-Reply-To: <56b13acf0908210212t5b6921f5h4c224efa21d2f067@mail.gmail.com>

On Fri, 21 Aug 2009 11:12:17 +0200, Joe wrote:
> Thanks for your explanation. However as you can see, I got 2GB mem and
> ~10GB swap, totally 12GB.
>
> With ulimit -s 10240(KB), I can allocate 2.5GB, I guess these are in
> swap, right?
> With ulimit -s unlimited, as you said, kernel reserved 1GB, stack
> reserved 2GB, there are still 12-3=9GB left??

Physical memory and swap are not the only limitations -- the other is
address space.  On 32-bit x86 systems CPU can address at most 4 GiB
of RAM[1].  Furthermore, in default configuration of Linux top 1 GiB
is reserved for kernel.  This means user space application can
address up to 3GiB of memory.

Now, as Glynn explained:

> On Thu, Aug 20, 2009 at 10:58 PM, Glynn Clements wrote:
>> If you set a stack size of unlimited, 2 GiB are reserved
>> for the stack and shared libraries, causing shared libraries to be
>> mapped at 1GiB and up. This leaves around 860 MiB for the heap.
>>
>> The result is that there isn't any area of the address space which is
>> large enough for a single 2500 MiB allocation:

> Why did malloc failed, instead of allocating this abundant swap space?

malloc(3) failed because it failed to allocate *address space* not memory.
In default configuration malloc(3) won't fail if there is not enough
memory anyways (try it yourself -- disable swap and try allocating
1.5 GiB).

As you can see on the memory map's Glenn provided:

>> glynn@cerise:~ $ cat /proc/self/maps
>> 08048000-08053000 r-xp 00000000 08:01 3966484    /bin/cat
>> 08053000-08054000 r--p 0000a000 08:01 3966484    /bin/cat
>> 08054000-08055000 rw-p 0000b000 08:01 3966484    /bin/cat
>> 0a016000-0a038000 rw-p 0a016000 00:00 0          [heap]
>> 40000000-4001c000 r-xp 00000000 08:01 9785919    /lib/ld-2.9.so
>> 4001c000-4001d000 r--p 0001b000 08:01 9785919    /lib/ld-2.9.so
>> 4001d000-4001e000 rw-p 0001c000 08:01 9785919    /lib/ld-2.9.so
>> 4001e000-4001f000 r-xp 4001e000 00:00 0          [vdso]
>> 4001f000-40020000 rw-p 4001f000 00:00 0
>> 40037000-4016f000 r-xp 00000000 08:01 9784624    /lib/libc-2.9.so
>> 4016f000-40171000 r--p 00138000 08:01 9784624    /lib/libc-2.9.so
>> 40171000-40172000 rw-p 0013a000 08:01 9784624    /lib/libc-2.9.so
>> 40172000-40176000 rw-p 40172000 00:00 0
>> bfb35000-bfb4a000 rw-p bffeb000 00:00 0          [stack]

there is no continuous block of 2 GiB virtual address space (this is
because Linux changes the location where libraries are mapped).  When
you request allocation of 2.5 GiB system has to find a large enough
hole between allocated regions and there isn't any.  See for
yourself and analyze the hexadecimal numbers on the left column

On 64-bit systems the problem does not occur because applications
use larger virtual address (48-bit if I'm not mistaken which is
256 TiB)


PS. Do not top-post.


[1] With Physical Address Extension[2] CPU can address more memory (64 GiB) but
     each application can address up to 4GiB anyways so lets ignore it for now.
[2] http://en.wikipedia.org/wiki/Physical_Address_Extension

-- 
Best regards,                                            _     _
  .o. | Liege of Serenly Enlightened Majesty of         o' \,=./ `o
  ..o | Computer Science,  Micha³ "mina86" Nazarewicz      (o o)
  ooo +----<mina86@mina86.com>---<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

  parent reply	other threads:[~2009-08-21  9:38 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 [this message]
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

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.uy0adwzdlak3lt@amdc030.digital.local \
    --to=m.nazarewicz@samsung.com \
    --cc=glynn@gclements.plus.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).