multi-threaded memory leak


 
Thread Tools Search this Thread
Top Forums Programming multi-threaded memory leak
# 1  
Old 04-14-2010
multi-threaded memory leak

Hello All :
I write a .c program to test the exactually resource the memory leak
as follows:
Code:
  1 #include <stdio.h>
  2 #define NUM  100000 
  3 void *Thread_Run(void * arg){
  4     //TODO
  5     //pthread_datch(pthread_self());
  6     int socket= (int)arg;
  7     close(socket);
  8     return NULL;
  9 }
 10 int main(void){
 11     
 12     int i,j;   
 13     pthread_t  pid[NUM];  
 14     for( i = 0; i < NUM ; i++){
 15         j = 0;
 16         j = socket(AF_INET, SOCK_DGRAM, 0);
 17         pthread_create(&pid[i],NULL,Thread_Run,(void *)j);
 18     
 19     }
 20     while(1){
 21         
 22         ;
 23     }
 24     return 0 ;
 25 }

I create NUM threads and let them not detached .so their must memory leak , the question is how I know exactly what resource the memory leaks ?

Best regard

aobai

---------- Post updated at 09:38 PM ---------- Previous update was at 07:04 AM ----------

Maybe it's nothing to do with memory , I let the created threads joinable , and return implicitly (return NULL) ,but don't use pthread_join() to release the exited thread ID , if I create too many threads, It's must beyond the nums of system allowed

Last edited by aobai; 04-14-2010 at 11:44 PM..
# 2  
Old 04-19-2010
it's not just memory

consider this :

first: creating a lot of threads does not mean necessarily that a lot of memory will be consumed ;

(that's because the amount of memory used just to create and manage a thread is rather small)

-- then you would have to make your thread function "use" (really use) some - or a lot of - memory in order to actually see a system memory shortage ;

second: by the code you provided, I suspect you will run out of file descriptors way much sooner than any memory shortage can be perceived ;

and last, but not least: creating a very large number of threads may result in an overall system response time degradation, due to the scheduling of so many threads, depending on the thread support implementation on your host ;

(that's because "N:1" or "pure user mode" threads would not harm your system's performance, for the operating system would not perceive that many threads as they are all in userland, as well as the thread scheduling ;
but if, on the other hand, your thread implementation is "1:1" or "pure kernel threads", then this code of yours is sure to slow all of your system down to a near halt - unless you have a plethora of cores and a really fast clock)

good luck, and success !
alexandre botao
<< botao {dot} org >>
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

script for multi-threaded bash processes

hey everyone, I'm having some trouble breaking down some code. It's simple a control script that takes machines meant to be backed up from a list. Then according to that will run multi-threaded processes up until the specified thread limit. for example if there are 4 machines to be backed up,... (2 Replies)
Discussion started by: terrell
2 Replies

2. Programming

Deallocating memory in multi-threaded environment.

I'm having a hard time figuring out how to manage deallocation of memory in multithreaded environments. Specifically what I'm having a hard time with is using a lock to protect a structure, but when it's time to free the structure, you have to unlock the lock to destroy the lock itself. Which will... (5 Replies)
Discussion started by: gngrwzrd
5 Replies

3. Programming

Memory Leak

Hi, I am trying a database server which keeps a B+ plus tree structure and works on it. I am trying to find the memory used/leak while executing this process. I check the memory leak by using ps uax command. When i execute a delete query i am sure that my code frees up the existing... (9 Replies)
Discussion started by: kumaran_5555
9 Replies

4. Linux

Multi-threaded encryption @ Fedora 11

Hello, are any of the encryption programs capable of true multi-threading ? Friend of mine tells me that he's been running some testing on Fedora 11 and that the kernel doesn't support multi-threading at that level. I've been looking into TrueCrypt, encfs and both calm to support... (1 Reply)
Discussion started by: TehOne
1 Replies

5. UNIX for Advanced & Expert Users

Multi-threaded encryption @ Fedora 11

Hello, are any of the encryption programs capable of true multi-threading ? Friend of mine tells me that he's been running some testing on Fedora 11 and that the kernel doesn't support multi-threading at that level. I've been looking into TrueCrypt, encfs and both calm to support... (0 Replies)
Discussion started by: TehOne
0 Replies

6. Shell Programming and Scripting

In need of multi threaded perl assistance

I need to write a perl script to execute external programs and grab the output and return code. Each program should be killed if it has not completed within X seconds. Imagine that the script goes something like this : @commands = &get_commands(); foreach $cmd (@commands) { $pid =... (4 Replies)
Discussion started by: SandmanCL
4 Replies

7. AIX

multi threaded program is hanging

I have a Multithreaded program which is hanging on AIX. OS Version: AIX 5.2 and thread library version : 5.2.0.75 We Initiate the process with 50 threads..when we are disconnecting from the process it hangs.There is lots of other stuff involved here.I am just sending the piece of the problem with... (0 Replies)
Discussion started by: hikrishn
0 Replies

8. Programming

HOWTO: Calculate the balance of work in multi-threaded app.

I was wondering if anyone could give me a good idea how to calculate how balanced the threading is on a multi-threaded application. I want a percentage, such as "threads are 80% balanced." This is the way I am currently going about it, maybe it is good, maybe not. First, whenever a thread... (2 Replies)
Discussion started by: DreamWarrior
2 Replies

9. Programming

about virtual memory and memory leak

Hi, First of all I appreciate this group very much for its informative discussions and posts. Here is my question. I have one process whose virtual memory size increases linearly from 6MB to 12MB in 20 minutes. Does that mean my process has memory leaks? In what cases does the... (4 Replies)
Discussion started by: shriashishpatil
4 Replies

10. Programming

multi-threaded server, pthreads, sleep

I am trying to writa a multi-client & multi-threaded TCP server. There is a thread pool. Each thread in the pool will handle requests of multiple clients. But here I have a problem. I find a solution but it is not how it must be... i think. When threads working without sleep(1) I can't... (0 Replies)
Discussion started by: Parahat Melayev
0 Replies
Login or Register to Ask a Question