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:
I am checking the memory usage with "ps -o pid,pcpu,vsz,rss,user -p <pid>". Here is the result: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; }
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!



LinkBack URL
About LinkBacks



