kuldeep paranjpe
|
Memory release issue after pthread_detach and pthread_cancel
|
kuldeep paranjpe
07/15/2009 8:44 AM
post33805
|
Memory release issue after pthread_detach and pthread_cancel
Hi,
I am trying to create a thread in 'main' process. This thread allocates about 100K memory, sleeps for few seconds and
then releases the memory. 3 such threads are created by the parent process.
After creating each thread parent process sleeps for some time and calls pthread_detach() and pthread_cancel() on
particular 'tid'.
When I do 'pidin m' for that process, I could see the memory of parent process goes on increasing. with each thread
create the parent process starts with new memory offset(actual + allocated). i.e when thread finishes it does not
liberate the complete memory and parent process does not return to it's original value
The logs says that;
parent process starts with about 36K, and after finishing all the threads it ends up acquiring 456K.
pthread_detach() synopsis says '"When a detached thread terminates, all system resources allocated to that thread are
immediately reclaimed."
OS should destroy all the thread stack once its function is done and is cancelled(also pidin dosen't show that thread).
from where this memory is getting accumulated in parent process? OR is there any other way to clean up the thread stack?
??
Here is the sample program;
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/resource.h>
static void* cliente(void *datos)
{
while(true)
{
//Allocating dummy memory of about 100K
char *buff= new char[100000];
sleep(20);
//releasing memory
delete [] buff;
buff=NULL;
}
}
int main()
{
int ret1;
ret1=0;
pthread_t mythread[4];
while(ret1 < 3)
{
//create thread
if(!pthread_create((pthread_t *)&mythread[ret1],NULL,cliente,NULL))
{
perror("pthread_create");
}
//dummy sleep
sleep(60);
//detach the thread
int ret = pthread_detach((pthread_t)mythread[ret1]);
if(EOK == ret)
{
printf("Thread %d got detached\n",mythread[ret1]);
fflush(stdout);
}
//cancel the thread
int iRet = pthread_cancel((pthread_t)mythread[ret1]);
if(EOK == iRet)
{
printf("Thread %d got cancelled\n",mythread[ret1]);
fflush(stdout);
}
mythread[ret1] = NULL;
ret1++;
}
while(true)
{
}
return EXIT_SUCCESS;
}
|
|
|