Subj : Re: Linux Posix Threads: Memory consumption To : comp.programming.threads From : Giancarlo Niccolai Date : Sun Apr 10 2005 01:54 pm Saraneus wrote: > Oliver Battenfeld > wrote in message news:<1202325.PpYFexS3M3@odb.my-fqdn.de>... >> >> I quote from 'man pthread_join': >> When a joinable thread terminates, its memory resources (thread >> descriptor and stack) are not deallocated until another thread performs >> pthread_join on it. Therefore, pthread_join must be called once for each >> joinable thread created to avoid memory leaks. > > That's good news, thanks a lot. Sorry for not reading the man page, I > just didn't know of it, really! > > As I mentioned, I'm using mtrace over the whole program to find memory > leaks. I expect the console "mtrace" to say "No memory leaks.", but > that never comes up, wenn I use threads. It always sais "Memory not > freed" and just one line with a size of 0x1fe0. And I'm joining this > thread. Is that memory needed by the controlling thread and never > freed? May depend on pthread implementation, but usually yes; some resources may be allocated and never freed until the process exits; or just never freed, because the OS will claim them anyhow, so keeping track of them may be considered by someone "inefficient" or "inelegant". However, try to use just one thread to serve all the clients, and use threads to do some other parallel tasks, as database query, disk updates, control connection services, logging, and so on. If you use one thread per client, your app may not scale up very well; the number of threads you have is limited (also in hyperthreading, just the limit is higher), and the time needed to manage any thread in the os is usually something like O( n^K ), where n is a number and k is a constant 1 < K < 2. Or in other words, every thread you add makes the whole application (and machine) slower (given that the number of processors is irrelevant with respect to the number of threads). Bests, Giancarlo Niccolai .