From: Tobias Peters Date: 2002-04-20T05:49:55+09:00 Subject: Re: Threads creating threads creating threads... My previous posting was not clear enough, I'll try to enhance it. I include a ruby program in this message that should behave like this: 1) The main thread creates child thread with No n=1 2) Child thread n creates child thread n+1 3) Child thread n exits and some other thread calls join upon it 4) Goto 2) with n=n+1 So this is an endless loop, the example program should run forever, and threads are created and destroyed as it runs. The memory usage of this program should be constant, because at any given time, there are at most 3 threads living simultaneously. This is the (enhanced) example program that implements the above: >>>>>>>>>>>>>>>>>>>>>> def thread(my_number, parent_thread) puts "Thread No #{my_number} starts" # wait for the parent thread to finish before creating a new child # thread: parent_thread.join if (parent_thread) # create a new child thread that starts with this function: Thread.new(my_number+1, Thread.current, &method(:thread)) # we use a method here so no block can keep this stack frame alive puts "Thread No #{my_number} exits" end Thread.new{thread(1, nil)} # Let the child threads do their work Thread.stop <<<<<<<<<<<<<<<<<<<<<<<<< When this program runs, it eats up all memory and finally crashes: .... Thread No 276 exits Thread No 278 starts Thread No 277 exits Thread No 279 starts Thread No 278 exits /tmp/t.rb:8:in `initialize': failed to allocate memory (fatal) from /tmp/t.rb:8:in `new' from /tmp/t.rb:8:in `thread' from /tmp/t.rb:8:in `thread' from /tmp/t.rb:8:in `initialize' from /tmp/t.rb:8:in `new' from /tmp/t.rb:8:in `thread' from /tmp/t.rb:8:in `thread' from /tmp/t.rb:8:in `initialize' ... 1106 levels... from /tmp/t.rb:14 from /tmp/t.rb:14:in `initialize' from /tmp/t.rb:14:in `new' from /tmp/t.rb:14 The line "... 1106 levels..." indicates that the whole stack that led to the creation of the last child thread is kept in memory by this thread, while I would expect that the stack of a child thread has no entries prior to this thread's creation. Why does a thread hold the stack prior to its creation in memory, and can this be changed? Tobias