[uClibc] FW: malloc/free memory leak in uClibc 0.9-20?

Wen Yang weny at promise.com
Wed Oct 1 01:20:29 UTC 2003

A correction: it was 8664KB, instead of 8664MB.
Hit the "Send" button a bit too early :-(
-----Original Message-----
From: Wen Yang 
Sent: Tuesday, September 30, 2003 6:16 PM
To: uclibc at uclibc.org
Subject: malloc/free memory leak in uClibc 0.9-20?
I encountered  a memory leak like problem while frequently calling
malloc and free. I am using Toolchain gcc 3.2.3 based on uClibc 0.9-20
on i386 LINUX kernel 2.4.18.
I wrote a small sample program to test it. What it does is pretty much
doing malloc and free in a loop from 8 bytes to 4MB. Before it enters
the allocate loop, the memory usage is about 372KB (vmSize), after it
enters the loop, the memory keeps increasing until 8664MB, after it
comes out of the loop and frees the buffer, the memory consumption
remains 8664MB.
I used glibc based gcc (2.9.6)  to compile the same program and the
result is: the initial memory  is 1436KB, in the allocation loop it goes
up to 5536KB, and then after freeing the buffer, it comes down to 1436KB
- the same as initial.
The appended is the main function of my sample program. The actual .c
file and the dump of uclibc and glibc are attached. In
"printMemoryStatus", I simply dump the /proc/<pid>/status file.
Anybody has this problem before? Or has it been fixed in 0.9-21? Can
anyone help to try it out? Thanks a lot!
int main()
            unsigned char * pBuf = NULL;
      unsigned char * pBuf1 = NULL;
      unsigned int capacity = 8, capacitySave = 0;
      char * pFileBuf = NULL;

      pFileBuf = (char *)malloc(FILE_BUFFER_SIZE);
      if (pFileBuf == NULL)
            printf("failed to allocate file buffer\n");
            return 1;
      printf("before allocate loop\n");
      capacity = 8;

      pBuf = (unsigned char *)malloc(capacity);
      if (pBuf == NULL)
            printf("allocate memory %d failed\n", capacity);
            return 1;
      printf("after allocate %d\n", capacity);
      memset(pBuf, capacity, capacity);
      while (capacity < (4*1024*1024))
            capacitySave = capacity;
            capacity = capacity << 1;

            printf("allocating memory %d\n", capacity);
            pBuf1 = (unsigned char * )malloc(capacity);
            if (pBuf1 == NULL)
                  printf("failed to allocate memory\n");
            memcpy(pBuf1, pBuf, capacitySave);
            memset((pBuf1+capacitySave), capacity, (capacity -
            pBuf = pBuf1;


      printf("after free pBuf\n");
      printf("after sleep for 2 second\n");

      printf("after sleep for 2 second\n");


      printf("program quit\n");

      return 0;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://codepoet.org/lists/uclibc/attachments/20030930/4582a953/attachment-0001.html

More information about the uClibc mailing list