Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

free all memory allocations related to POSIX threads

SOLVED
Go to solution
blackwater
Regular Advisor

free all memory allocations related to POSIX threads

I have written a simple multithreaded test program. It is attached to this post.

When I compile it with aCC (aCC: HP C/aC++ B3910B A.06.15 [May 16 2007]) and run it on HPUX 11.31 with libRTC
I see that some memory allocations are not freed until the end of the program:
  aCC -mt +DD64 -g test_pthread_key.cpp -lpthread -o test_pthread_key.acc
  >ldd ./test_pthread_key.acc
  ./test_pthread_key.acc:
        libpthread.so.1 =>      /usr/lib/hpux64/libpthread.so.1
        libstd_v2.so.1 =>       /usr/lib/hpux64/libstd_v2.so.1
        libCsup.so.1 => /usr/lib/hpux64/libCsup.so.1
        libm.so.1 =>    /usr/lib/hpux64/libm.so.1
        libunwind.so.1 =>       /usr/lib/hpux64/libunwind.so.1
        libc.so.1 =>    /usr/lib/hpux64/libc.so.1
        libdl.so.1 =>   /usr/lib/hpux64/libdl.so.1
        libuca.so.1 =>  /usr/lib/hpux64/libuca.so.1
 
  BATCH_RTC=on LD_PRELOAD=/opt/langtools/lib/hpux64/librtc.so ./test_pthread_key.acc  1000 10 0
 
What I see is that when the program creates N POSIX threads then there will be N blocks of size 4096 bytes in a heap-report of libRTC.
I mean that N blocks of size 4096 bytes are not freed even thought I did pthread_join for each of the POSIX thread.

For example, this is one of allocations:
-------------------------------------------------------------------------
40960 bytes in 10 blocks (30.30% of all bytes allocated)
These range in size from 4096 to 4096 bytes and are allocated
#0  __pthread_setspecific() at /ux/core/libs/threadslibs/src/common/pthreads/specific.c:952
#1  __pthread_setspecific() at /ux/core/libs/threadslibs/src/common/pthreads/specific.c:692
#2  threadfunc(void*)() at test_pthread_key.cpp:22
#3  __pthread_bound_body() at /ux/core/libs/threadslibs/src/common/pthreads/pthread.c:4875


There are other allocations that are also related to the number of created threads.
I will attach a heap-report as well.
The same report I get if I compile the program with gcc.
Is it possible to free this memory allocations related to POSIX threads as soon as a POSIX thread has been joined?
My another program runs for a long time and have lots of short-lived threads and I am afraid that these memory allocations inside POSIX threads that are not freed properly will eventually cause increased memory consumption.

2 REPLIES
blackwater
Regular Advisor

Re: free all memory allocations related to POSIX threads

A heap-report file produced by libRTC

Highlighted
Dennis Handly
Acclaimed Contributor
Solution

Re: free all memory allocations related to POSIX threads

>I see that some memory allocations are not freed until the end of the program:

>I mean that N blocks of size 4096 bytes are not freed even though I did pthread_join

 

This storage is cached, so you can create new threads faster.


 > aCC -mt +DD64 -g test_pthread_key.cpp -lpthread -o test_pthread_key.acc

 

(No need for -lpthread of you use -mt.)


>Is it possible to free this memory allocations related to POSIX threads as soon as a POSIX thread has been joined?

 

I don't think so.  This isn't a leak, it will be reused when needed.  (As long as the number of threads reaches a steady state.)

 

>have lots of short-lived threads

 

Then this caching will speed things up.

 

>will eventually cause increased memory consumption.

 

If you have a leak, you should be able to see it.