Memory leak with detached pthreads - how to free?

This is a discussion on Memory leak with detached pthreads - how to free? within the Linux Programming forums, part of the Platform Specific Boards category; Using detached POSIX threads I recognized that they leak memory. I've searched for a long time but most answers told ...

  1. #1
    rfk
    rfk is offline
    Registered User
    Join Date
    Aug 2007
    Posts
    5

    Exclamation Memory leak with detached pthreads - how to free?

    Using detached POSIX threads I recognized that they leak memory. I've searched for a long time but most answers told me to use either pthread_join() or pthread_detach(). Of course I've tried both without success. Here's a sample code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <pthread.h>
    
    void *func(void *dat) {
    	int i;
    	//pthread_detach(pthread_self());
    	
    	char stack[4234234];
    
    	char *heap;
    	printf("Enter amount: ");
    	scanf("%d", &i); __fpurge(stdin);
    	heap = (char *)malloc(i*1024);
    	memset(heap, 'a', i*1024);
    	
    	printf("Press enter to stop thread");
    	getchar();
    	
    	//free(heap);
    	pthread_exit(NULL);
    }
    
    int main(void) {
    	int i;
    	pthread_t t;
    
    	printf("Press enter to start thread");
    	getchar();
    
    	pthread_create(&t, NULL, func, NULL);
    	pthread_join(t, NULL);
    	
    	printf("Press enter to exit");
    	getchar();
    
    	return 0;
    }
    I am checking the memory usage with "ps -o pid,pcpu,vsz,rss,user -p <pid>". Here is the result:

    Before the thread starts: VSZ=1492, RSS=400
    Inside the thread with i=1,000,000: VSZ=1009824 RSS=1000512
    After exiting the thread: VSZ=1009868 RSS=1000576

    As you can see the memory usage even increases after terminating the thread. Using pthread_detach() instead gives me the same results. Is it somehow possible to free the entire memory used within the thread?

    Thanks in advance!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > heap = (char *)malloc(i*1024);
    There's no need to cast the result of malloc in C, see the FAQ.

    > //free(heap);
    You do need to call free, since you called malloc.

    > Is it somehow possible to free the entire memory used within the thread?
    Call free() on the memory you malloc().

    What you're not aware of is that calling free() does NOT give the memory back to the OS but it is kept by the memory pool manager in the process. This is so if you call malloc again, the process can satisfy the request quickly rather than trapping into the OS (which is very slow by comparison).

    Code:
    for ( i = 0 ; i < 1000 ; i++ ) {
      char *p = malloc( 100000 );
      usleep( 100000 );
      free( p );
    }
    With the free(), you'll see an initial jump in things like VSZ and then it will settle down to a steady state. This is perfectly fine for normal operation.
    Without the free(), you will see the size increase over time as more and more memory is allocated because of the leak.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    rfk
    rfk is offline
    Registered User
    Join Date
    Aug 2007
    Posts
    5
    Quote Originally Posted by Salem View Post
    What you're not aware of is that calling free() does NOT give the memory back to the OS but it is kept by the memory pool manager in the process. This is so if you call malloc again, the process can satisfy the request quickly rather than trapping into the OS (which is very slow by comparison).
    Ah, that was the issue! Even with or without the free() call the memory was never fully released. Thanks for your answer!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For some reason I end up with memory leak...
    By RaDeuX in forum C Programming
    Replies: 10
    Last Post: 11-25-2008, 11:43 PM
  2. memory leak in vector <int>
    By franziss in forum C++ Programming
    Replies: 21
    Last Post: 10-10-2006, 06:54 AM
  3. Memory Leak Help
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 06-19-2006, 11:22 AM
  4. Memory Leak
    By Berticus in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2005, 05:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21