[Linux] malloc: small size - small pointer?

September 13th, 2011 - 04:17 am ET by Alex Vinokur | Report spam
Hi,



Linux 2.6.18-238.1.1.el5 #1 SMP Tue Jan 4 13:32:19 EST 2011 x86_64
x86_64 x86_64 GNU/Linux

Intel(R) C++ Intel(R) 64 Compiler XE for applications running on
Intel(R) 64, Version 12.0.4.191 Build 20110427


malloc() returns "small" pointers for small sizes.
Any suugestions for such behavior of the malloc function.

Are "small" pointers on heap?



- Program foo.cpp -
#include <stdio.h>
#include <stdlib.h>

#define SHOW(s_i_z_e, p_o_i_n_t_e_r) printf ("Size = 0x%lx, Pointer %p", s_i_z_e, p_o_i_n_t_e_r)

int main()
{
size_t size = 0x1ffe0;
void* pointer = 0;

for (size_t i = 0; i < 0x10; i++)
{
pointer = malloc(size);
SHOW(size, pointer);
size++;
}

return 0;
}

-



Compilation:

icpc foo.cpp




Running:


./a.out



Size = 0x1ffe0, Pointer = 0x199cc010
Size = 0x1ffe1, Pointer = 0x199ec000
Size = 0x1ffe2, Pointer = 0x19a0bff0
Size = 0x1ffe3, Pointer = 0x19a2bfe0
Size = 0x1ffe4, Pointer = 0x19a4bfd0
Size = 0x1ffe5, Pointer = 0x19a6bfc0
Size = 0x1ffe6, Pointer = 0x19a8bfb0
Size = 0x1ffe7, Pointer = 0x19aabfa0
Size = 0x1ffe8, Pointer = 0x19acbf90
Size = 0x1ffe9, Pointer = 0x19aebf80
Size = 0x1ffea, Pointer = 0x2ae67b770010
Size = 0x1ffeb, Pointer = 0x2ae67b791010
Size = 0x1ffec, Pointer = 0x2ae67b7b2010
Size = 0x1ffed, Pointer = 0x2ae67b7d3010
Size = 0x1ffee, Pointer = 0x2ae67b7f4010
Size = 0x1ffef, Pointer = 0x2ae67b815010


Alex
email Follow the discussionReplies 4 repliesReplies Make a reply

Similar topics

Replies

#1 Xavier Roche
September 13th, 2011 - 04:23 am ET | Report spam
On 09/13/2011 10:17 AM, Alex Vinokur wrote:
malloc() returns "small" pointers for small sizes.
Any suugestions for such behavior of the malloc function.
Are "small" pointers on heap?



From the Linux glibc man page:

"Normally, malloc() allocates memory from the heap, and adjusts the size
of the heap as required, using sbrk(2). When allocating blocks of
memory larger than MMAP_THRESHOLD bytes, the glibc malloc()
implementation allocates the memory as a private anonymous mapping
using mmap(2).
MMAP_THRESHOLD is 128 kB by default, but is adjustable using
mallopt(3). Allocations performed using mmap(2) are unaffected by
the RLIMIT_DATA resource limit (see getrlimit(2))."

In your test, malloc switched to mmap after $[0x1ffea] == 131050 ==
roughly 128KiB.
Replies Reply to this message
#2 Alex Vinokur
September 14th, 2011 - 02:18 am ET | Report spam
On Sep 13, 11:23 am, Xavier Roche
wrote:
On 09/13/2011 10:17 AM, Alex Vinokur wrote:

> malloc() returns "small" pointers for small sizes.
> Any suugestions for such behavior of the malloc function.
> Are "small" pointers on heap?

 From the Linux glibc man page:

"Normally, malloc() allocates memory from the heap, and adjusts the size
of the heap as required, using sbrk(2).  When allocating blocks of
memory larger than MMAP_THRESHOLD bytes, the glibc malloc()
implementation allocates  the  memory  as  a  private anonymous mapping
using mmap(2).
MMAP_THRESHOLD is 128 kB by  default,  but  is  adjustable  using
mallopt(3).   Allocations  performed  using  mmap(2) are unaffected by
the RLIMIT_DATA resource limit (see getrlimit(2))."

In your test, malloc switched to mmap after $[0x1ffea] == 131050 => roughly 128KiB.



Thank you.

I used function mallopt()
mallopt(M_MMAP_THRESHOLD, 0);

"Large" pointers were about 65000 times, after that malloc() returns
"small" pointers.


Thanks.

Alex
Replies Reply to this message
#3 Alex Vinokur
September 14th, 2011 - 02:21 am ET | Report spam
On Sep 13, 11:23 am, Xavier Roche
wrote:
On 09/13/2011 10:17 AM, Alex Vinokur wrote:

> malloc() returns "small" pointers for small sizes.
> Any suugestions for such behavior of the malloc function.
> Are "small" pointers on heap?

 From the Linux glibc man page:

"Normally, malloc() allocates memory from the heap, and adjusts the size
of the heap as required, using sbrk(2).  When allocating blocks of
memory larger than MMAP_THRESHOLD bytes, the glibc malloc()
implementation allocates  the  memory  as  a  private anonymous mapping
using mmap(2).
MMAP_THRESHOLD is 128 kB by  default,  but  is  adjustable  using
mallopt(3).   Allocations  performed  using  mmap(2) are unaffected by
the RLIMIT_DATA resource limit (see getrlimit(2))."

In your test, malloc switched to mmap after $[0x1ffea] == 131050 => roughly 128KiB.



Thank you.

I used mallopt(M_MMAP_THRESHOLD, 0);

"Large" pointers were about 65000 times, after that malloc() returns
"small" pointers.

Thanks

Alex
Replies Reply to this message
#4 Alex Vinokur
September 14th, 2011 - 03:08 am ET | Report spam
On Sep 14, 9:18 am, Alex Vinokur wrote:
On Sep 13, 11:23 am, Xavier Roche
wrote:





> On 09/13/2011 10:17 AM, Alex Vinokur wrote:

> > malloc() returns "small" pointers for small sizes.
> > Any suugestions for such behavior of the malloc function.
> > Are "small" pointers on heap?

>  From the Linux glibc man page:

> "Normally, malloc() allocates memory from the heap, and adjusts the size
> of the heap as required, using sbrk(2).  When allocating blocks of
> memory larger than MMAP_THRESHOLD bytes, the glibc malloc()
> implementation allocates  the  memory  as  a  private anonymous mapping
> using mmap(2).
> MMAP_THRESHOLD is 128 kB by  default,  but  is  adjustable  using
> mallopt(3).   Allocations  performed  using  mmap(2) are unaffected by
> the RLIMIT_DATA resource limit (see getrlimit(2))."

> In your test, malloc switched to mmap after $[0x1ffea] == 131050 => > roughly 128KiB.

Thank you.

I used function mallopt()
mallopt(M_MMAP_THRESHOLD, 0);

"Large" pointers were about 65000 times, after that malloc() returns
"small" pointers.

Thanks.




This produces only "large" pointers

int ret = mallopt(M_MMAP_THRESHOLD, 0);
assert (ret == 1);
ret = mallopt(M_MMAP_MAX, INT_MAX);-
assert (ret == 1);
email Follow the discussion Replies Reply to this message
Help Create a new topicReplies Make a reply
Search Make your own search