Find the answer to your Linux question:
Results 1 to 4 of 4
I'm a fledgling linux programmer and have created an apparent memory leak. I ran the following code over night (around 250e6 iterations on my machine) and have 'lost' nearly 128MB ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2005
    Location
    Canada, Halifax
    Posts
    86

    thread memory leak 2.4.26?


    I'm a fledgling linux programmer and have created an apparent memory leak. I ran the following code over night (around 250e6 iterations on my machine) and have 'lost' nearly 128MB of RAM. Am I spawning and destroying the threads correctly or have I neglected an important step? I'm running the 2.4.26 kernel with gcc 3.3.4...

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <pthread.h> 
    
    #define CHILDERN 2 
    
    int *thread_child&#40;int *pArg&#41;; 
    
    int main&#40;void&#41; &#123; 
    pid_t PPID,PID; 
    pthread_t thread&#91;CHILDERN&#93;; 
    int **pRetPtr; 
    unsigned int i = 0,j = 0,k; 
    
    PPID = getppid&#40;&#41;; 
    PID = getpid&#40;&#41;; 
    
    printf&#40;"Root thread start&#58; Parent PID&#58; %i PID&#58; %i\n",PPID,PID&#41;; 
    for&#40;;;&#41; &#123; 
    for &#40;k=0;k<CHILDERN;k++&#41; 
    pthread_create&#40;thread+k,NULL,&#40;void *&#40;*&#41;&#40;void *&#41;&#41;thread_child,&#40;void *&#41;&PPID&#41;; 
    for &#40;k=0;k<CHILDERN;k++&#41; 
    pthread_join&#40;*&#40;thread+k&#41;,&#40;void **&#41;pRetPtr&#41;; 
    i+=CHILDERN; 
    if&#40;&#40;j+=CHILDERN&#41;>10000&#41; &#123; 
    j = 0; 
    printf&#40;"%u\n",i&#41;; 
    &#125; 
    &#125; 
    printf&#40;"Root thread stop&#58; Parent PID&#58; %i PID&#58; %i\n",PPID,PID&#41;; 
    
    exit&#40;0&#41;; 
    &#125; 
    
    int *thread_child&#40;int *pArg&#41; &#123; 
    pid_t PID; 
    int i = *pArg; 
    
    PID = getpid&#40;&#41;; 
    pthread_exit&#40;&PID&#41;; 
    &#125;

  2. #2
    Just Joined!
    Join Date
    May 2006
    Posts
    2
    Hello,
    I am facing the exact same issue with same linux kernal version 2.4.26 , gcc version 3.3.3 . Can anybody help me with the reason and the solution?

    Thanks
    Deepali

  3. #3
    Linux User fernape's Avatar
    Join Date
    Dec 2005
    Location
    Holland
    Posts
    301
    Try to monitor your process with top to ensure that is really your program who is leaking and this is not a memory management issue.

    Best regards

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    May 2006
    Posts
    2
    Thanks for the reply. I have been monitoring the process using top command and the memory consumption has so far been constant (2.8 %) . Still after about 3895445 iterations I recive "out of memory" error during pthread_create() . What other memory issue could this be? My code runs on a embedded system with Linux 2.4.26.

    Thanks.

    Deepali

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •