Process memory leak

In the following code, I malloc 100 buffers of ~1.6M each then I free them.
ls -ld /proc reveals that I am losing 159744 bytes even though all the
buffers have been freed. First I thought the leak was caused by the malloc
library, so I tried to use mmap. Same result. Here is the code I used.

char* buffer [100];

cout << “start” << endl;
sleep (5); // Give me enough time to do a ls -ld /proc

for (int i = 0; i < 100; ++i)
buffer = (char*) malloc (16 * 1024 * 100);

for (int i = 0; i < 100 ; ++i)
free (buffer );

cout << “end” << endl;
sleep (5); // Give me enough time to do a ls -ld /proc

Thanks
Jamal Benbrahim

Jamal Benbrahim <jamal.benbrahim@igt.com> wrote:

In the following code, I malloc 100 buffers of ~1.6M each then I free them.
ls -ld /proc reveals that I am losing 159744 bytes even though all the
buffers have been freed. First I thought the leak was caused by the malloc
library, so I tried to use mmap. Same result. Here is the code I used.

When you malloc memory, you’re really mapping in a section of memory into
you process. Malloc just arbitrates/assigns the use of that memory. When
you free(), you’re returning the memory to the pool that your process has
mapped in. There is no requirement for free() to return memory directly to
the OS immediately. If you malloc again, you process won’t have to remap
more memory, it can just pull it from the pool.

-Adam

Is there a way I can force the process to return the memory to the OS?

Thanks
Jamal

“Operating System for Tech Supp” <os@qnx.com> wrote in message
news:9oq1e8$svr$1@nntp.qnx.com

Jamal Benbrahim <> jamal.benbrahim@igt.com> > wrote:
In the following code, I malloc 100 buffers of ~1.6M each then I free
them.
ls -ld /proc reveals that I am losing 159744 bytes even though all the
buffers have been freed. First I thought the leak was caused by the
malloc
library, so I tried to use mmap. Same result. Here is the code I used.

When you malloc memory, you’re really mapping in a section of memory into
you process. Malloc just arbitrates/assigns the use of that memory. When
you free(), you’re returning the memory to the pool that your process has
mapped in. There is no requirement for free() to return memory directly
to
the OS immediately. If you malloc again, you process won’t have to remap
more memory, it can just pull it from the pool.

-Adam

Jamal Benbrahim <jamal.benbrahim@igt.com> wrote:

Is there a way I can force the process to return the memory to the OS?

If you wish to control the way memory (de)allocation occurs, you should
write your own or check out on the internet for a malloc’ing library
that is more to your taste. A good start page is http://www.cs.colorado.edu/~zorn/Malloc.html

-Adam

Incidentally, I was just looking at the page.
If I am not mistaken, all of those malloc variations end up using mmap or
mmap_device_memory. In my example, if I use mmap and munmap, I still lose
memory!

Any clues?

Thanks
Jamal

“Operating System for Tech Supp” <os@qnx.com> wrote in message
news:9osmd5$kn4$1@nntp.qnx.com

Jamal Benbrahim <> jamal.benbrahim@igt.com> > wrote:
Is there a way I can force the process to return the memory to the OS?

If you wish to control the way memory (de)allocation occurs, you should
write your own or check out on the internet for a malloc’ing library
that is more to your taste. A good start page is
http://www.cs.colorado.edu/~zorn/Malloc.html

-Adam

Jamal Benbrahim <jamal.benbrahim@igt.com> wrote:

Incidentally, I was just looking at the page.
If I am not mistaken, all of those malloc variations end up using mmap or
mmap_device_memory. In my example, if I use mmap and munmap, I still lose
memory!

I’m currently taking a closer look, I’ll post back once I have some more information

-Adam

Operating System for Tech Supp <os@qnx.com> wrote:

I’m currently taking a closer look, I’ll post back once I have some more information

See Knowledge base entry http://qdn.qnx.com/support/bok/solution.qnx?10458

-Adam

Jamal Benbrahim <jamal.benbrahim@igt.com> wrote:

In the following code, I malloc 100 buffers of ~1.6M each then I free them.
ls -ld /proc reveals that I am losing 159744 bytes even though all the
buffers have been freed.

When you map in memory Proc has to create page table entries to describe
the mappings, when you unmap the memory, I expect that Proc is not freeing
those allocated page table entries. If you re-map then re-free the same
amount of memory, do you lose the 159744 bytes again?

(I would expect Proc to allocate & then re-use, rather than freeing –
usually if the pages have been mapped once, they’ll be mapped again so
it is more efficient from Proc’s point of view to not free page table
entries/descriptors.)

-David

QNX Training Services
dagibbs@qnx.com