threads not releasing memory?

This is a discussion on threads not releasing memory? within the C Programming forums, part of the General Programming Boards category; I'm not sure if this is usual or not... I spawn a new thread like this: Code: if(pthread_create(&unused_id, NULL, chat_stream, ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    41

    threads not releasing memory?

    I'm not sure if this is usual or not...
    I spawn a new thread like this:
    Code:
    		if(pthread_create(&unused_id, NULL, chat_stream, NULL) <0)
    			die("pthread_create() failed for stream");
    After executing the size of my process (monitored with top) is 4k larger than before. In chat_stream, this thread is immitiatly exited again:
    Code:
    void *chat_stream(void *unused)
    {
    	pthread_exit(0);
    }
    But the size of the process is still 4k larger. So after spawning a few hundred threads, it takes a ****load of memory. Why that?
    The problem is, when I assign a lot of data to this thread like this:
    Code:
    void *chat_stream(void *unused)
    {
                    char foobar[99999999999]
    
    	pthread_exit(0);
    }
    then all this data isn't freed either. At least top does not decrease the size after exiting a thread. Is this usual and just an issue of top or is something wrong with my code?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    The value reported by top is the total memory allocated to your program by the OS.

    Normally, when your program free's memory, it is not returned to the OS, but kept for later use.

    If you call
    pthread_create()
    pthread_exit()
    pthread_create()
    pthread_exit()

    The second pair should re-use memory originally allocated and freed by the first pair.

    If it doesn't, then you probably do have a memory leak problem

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    41
    It doesn't. The thread is closed, another is opened and it takes 4k more memory. This is really weird because the code I quoted above is everything I left in and it still does that. How can there be a memory leak??

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    41
    Last bump. If you have any idea please tell me. Otherwise I will just leave it like that and whenever I think it takes too much memory, just restart the server or something like that (fortunatly the server isn't very mem hungry).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  2. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  3. Memory Leak Help
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 06-19-2006, 11:22 AM
  4. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  5. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21