Note: This bug is displayed in read-only format because the product is no longer active in Red Hat Bugzilla.

Bug 112208

Summary: memory leak when creating threads with user defined stacksize
Product: [Retired] Red Hat Linux Reporter: Thoralf Will <thoralf>
Component: glibcAssignee: Jakub Jelinek <jakub>
Status: CLOSED ERRATA QA Contact: Brian Brock <bbrock>
Severity: high Docs Contact:
Priority: medium    
Version: 9CC: fweimer
Target Milestone: ---   
Target Release: ---   
Hardware: i686   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2004-01-14 15:52:51 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Thoralf Will 2003-12-16 08:53:15 UTC
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.5)
Gecko/20031007

Description of problem:
When creating threads with user defined stacksize a memory leak of
4kB/thread occurs when making a join to destroy the thread. This only
occurs with glibc compiled for i686, not with glibc for i386. I assume
that the bug is caused by the nptl-patch.

Version-Release number of selected component (if applicable):
glibc-2.3.2 (all releases)

How reproducible:
Always

Steps to Reproduce:
The following code example shows the problem. Simply use top to watch
the memory usage or do a cat at /proc/pid/mem.

---begin example code---
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <sys/mman.h>
                            
void* th_func1(void *);

main(int argc, char** argv)
{
  pthread_t th;
  int i,StackSize=65535;
  pthread_attr_t Attributes;
  void* Stack[100];

  for (i=0; i<100; i++) Stack[i] = (void*)malloc(StackSize);
                                                          
  printf("Starting in 10 seconds.\npid = %d\n",getpid());
  sleep(10);
  printf("Starting...\n");
  
  for (i = 0; i < 100000; i++) {

    pthread_attr_init(&Attributes);
    pthread_attr_setstack(&Attributes,Stack[i%100],StackSize);

    if ( pthread_create(&th, &Attributes, th_func1, (void*) "Th1") != 0) {
      printf("Can't create thread!");
    } else {
      pthread_join(th, NULL);
    }
  }

  printf("\n... finished.\n");
  printf("waiting 20 more seconds.\n");
  sleep(20);
  for (i=0; i<100; i++) free(Stack[i]);
}

void* th_func1(void *X)
{
  return NULL;
}
---end example code---

Actual Results:  Memory usage rises until server runs out of (virtual)
memory.

Expected Results:  Memory should get freed (completely) when thread
gets destroyed.

Comment 1 Jakub Jelinek 2003-12-17 13:14:04 UTC
You forgot to mention you're using linuxthreads, which is not the
default threading library.
Fixed thusly:
http://sources.redhat.com/ml/libc-hacker/2003-12/msg00069.html

Comment 2 Jakub Jelinek 2004-01-14 15:52:51 UTC
Fixed in glibc-2.3.2-101.4 in FC1.