LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   pthreads virtual memory usage -- memory is not freed after thread exit (https://www.linuxquestions.org/questions/linux-general-1/pthreads-virtual-memory-usage-memory-is-not-freed-after-thread-exit-727215/)

minimol 05-20-2009 02:58 AM

pthreads virtual memory usage -- memory is not freed after thread exit
 
Hi,

I have a program which creates 100 threads(joinable threads) and calls pthread_join on them.

But I found that the memory is not coming back to initial value after the threads are created and destroyed .

I tried my program in FC3 and CentOS 5.2 (same behaviour is seen in both OS's).

Please find my program below


#include <stdio.h>
#include <iostream.h>
#include <unistd.h>
bool bShutDown = false;
#define TRD_CNT 100
#define LPVOID void*
void* do_infinite(LPVOID tmp)
{
while(true)
{
sleep(10);
if(bShutDown)
break;
}
return NULL;
}

int main(int argc, char* argv[])
{
int k;
cout<<"press any key"<<endl;
cin>>k;
{
{
pthread_t thrd[TRD_CNT];
for(int i = 0 ; i < TRD_CNT ; ++i )
{
unsigned long id;
int idt = pthread_create(&thrd[i],0,do_infinite,0);
cout<<idt <<endl;
}
cout<<"press any key"<<endl;
cin>>k;
bShutDown = true;
for(int i = 0 ; i < TRD_CNT ; ++i )
{
pthread_join(thrd[i],NULL);
}

}
}
cout<<"press any key"<<endl;
cin>>k;
cin>>k;
return 1;

}





Before thread creation

VIRTUAL MEM 2376 KB
RES 836 KB
SHR 728 KB

while threads were running

VIRTUAL MEM 1002mm
RES 1368 KB
SHR 844 KB

After threads were joined

VIRTUAL MEM 33244 KB
RES 992 KB
SHR 856 KB


Values are from top command output..

Please Can somebody explain this behaviour.


Thanks
Mini

Manjunath1847 05-21-2009 02:18 AM

Minimol, one thing you can try is to put pthread_exit to make notify explicitly thread exit. Not sure if this help, but just can be tried out with minimal change.

minimol 05-26-2009 01:19 AM

Quote:

Originally Posted by Manjunath1847 (Post 3547812)
Minimol, one thing you can try is to put pthread_exit to make notify explicitly thread exit. Not sure if this help, but just can be tried out with minimal change.



I tried pthread_exit. There is no difference.

After doing a bit of googling I found some documents saying .. virtual memory is not always freed by the heap manager as it is considered as an inexpensive resource.


All times are GMT -5. The time now is 11:35 AM.