This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug malloc/15321] malloc/free can't give the memory back to kernel when main_arena is discontinous


https://sourceware.org/bugzilla/show_bug.cgi?id=15321

ma.jiang at zte dot com.cn changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
  Attachment #10003|0                           |1
        is obsolete|                            |
  Attachment #10003|test_case to  show the peak |test_case to  show the peak
        description|memory usage  retain after  |memory usage  retain after
                   |memory all freed            |memory all freed(use write
                   |                            |to avoid mallocs in printf)

--- Comment #8 from ma.jiang at zte dot com.cn ---
Comment on attachment 10003
  --> https://sourceware.org/bugzilla/attachment.cgi?id=10003
test_case to  show the peak memory usage  retain after memory all freed(use
write to avoid mallocs in printf)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <assert.h>
#define MEM_SIZE 1024*512
char print_buf[2048];
const char msg1[1024] = "now we will see the peak physical memory usage\n";
const char msg2[1024] = "I have freed all memory, now I'm using \n";
int main(int argc,char *argv[])
{       

        int i = 0;      
        void **mem = (void**) malloc(sizeof(void*) * MEM_SIZE * 2);; 

        for (; i < MEM_SIZE;i++)
        {
          mem[i]=malloc(4096);
          if(mem[i] == NULL) 
          {
            printf("malloc failed.\n");
            exit(1);
          }
        }

        void *tt = mmap(mem[i-1]+1024*1024, 1024*213, PROT_READ |
PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, -1, 0);

        for (; i < MEM_SIZE * 2; i++)
        {
          mem[i]=malloc(4096);
          if(mem[i] == NULL)
          {
            printf("malloc failed.\n");
            exit(1);
          } 
        }

        char cmd[1024];
        write(1, msg1, strlen(msg1));
        snprintf(cmd, sizeof(cmd), "cat /proc/%d/status |grep VmRSS:",
getpid());
        system(cmd);

        while(i--)
        {
          free(mem[i]);
          mem[i]=NULL;  
        }
        free(mem);

        //assert(malloc_trim(0)==1);
        write(1, msg2, strlen(msg2));
        system(cmd);

        return 0;
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]