Free list can store enormous amounts of memory

Bug #1462853 reported by Removed by request
6
Affects Status Importance Assigned to Milestone
glibc (Ubuntu)
New
Undecided
Unassigned

Bug Description

I'm using Ubuntu 15.10 dev (x86_64) with libc6 2.21-0ubuntu4 and I have noticed that the free list can store a huge amount of memory and on analyzing this and reading manpages I have found some strange things. First here is a testcase to reproduce this issue (compiled with "gcc -Wall -pedantic -o test test.c"):

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define BLOCKSIZE 4096
#define NUMBER_BLOCKS 524288

int main()
{
 char *block[NUMBER_BLOCKS];
 int unsigned i;

 for(i = 0; i < NUMBER_BLOCKS; ++i)
  memset(block[i] = malloc(BLOCKSIZE), 0, BLOCKSIZE);
 for(i = 0; i < NUMBER_BLOCKS - 1; ++i)
  free(block[i]);
 pause();
 return 0;
}

On executing this the application uses actively only 4 KiB on the heap while RES is ~2 GiB because of glibc's caching methods. But there are some strange things:

- Freeing up the memory by decrementing in the testcase results that RES is ~5 MiB. On reading the manpage of mallopt() I'm assuming this happens because in the original testcase the last allocated block is at the end of the heap which prevents trimming it. I'm thinking that this part describes it: "(By contrast, the heap can be
              trimmed only if memory is freed at the top end.)". If I'm right maybe this could be stated in a better context as directly before it is talked about mmap().
- Using malloc_trim() before pause() in the testcase causes that RES is ~5 MiB too. The manpage of malloc_trim() says that releasing is done at the top of the heap which should not cause this result if my previous assumption should be correct.

In the end I think it is not healthy that the caching behavior from glibc can reserve such huge amounts of memory without automatically releasing it. The potential performance penalty if this would cause any swapping could be insane. For example I'm seeing this caching behavior on GIMP if I'm making huge scaling operations which causes ~9 GiB to be in the free list. Who knows how many other applications are caching moderate amounts of memory without being as suspicious as in this case with GIMP.

description: updated
Revision history for this message
Removed by request (removed3425744) wrote :

I have now forwarded this issue to upstream: https://sourceware.org/bugzilla/show_bug.cgi?id=18504

Revision history for this message
Seth Arnold (seth-arnold) wrote :

Try various values of MMAP_THRESHOLD environment variable; I suspect if you sets MMAP_THRESHOLD to something like 1024 bytes all the memory will be appropriately freed and returned to the OS when you hit the pause().

Thanks

Changed in glibc (Ubuntu):
status: New → Incomplete
Revision history for this message
Removed by request (removed3425744) wrote :

Executing "MALLOC_MMAP_THRESHOLD_=1024 ./test" results that ~1.8 GiB are in use. A little less than before but still too much. Using 0 as threshold causes the same result.

Changed in glibc (Ubuntu):
status: Incomplete → New
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.