From: Tobias Peters Date: 2002-04-19T21:48:54+09:00 Subject: Threads creating threads creating threads... I have already asked this question in [ruby-talk:19661], but I will ask it again here since I've got no answer then and the problem bothers me once again. Suppose your ruby app does its work in ruby's threads. Some of these threads will create other threads before they eventually exit, and some of these will again create other threads etc. Even if all threads exit cleanly and there is only a limited number of threads at a given time, this does not work in ruby: Ruby eats up all memory and crashes when the memory is exhausted. Example program: >>>>>>>>>>>>>>>>>>>>>>> def thread(my_number) puts "Thread No #{my_number} starts" # create a new child thread Thread.new(my_number+1, &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)} # Let the child threads do their work Thread.stop <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< The last few lines of output from this program are: Thread No 191 exits Thread No 193 starts Thread No 192 exits Thread No 194 starts Thread No 193 exits Killed I think the reason for the memory consumption of the threads is that threads carry with them the whole stack that led to their creation. Check Kernel::caller within a thread, it does not start with the creation of the thread, but with the start of the program. My current workaround is to send a signal to the main thread (via condition variables) and inform it that some thread wants to have a new thread started, together with the start method and the required arguments, but this should not be necessary. Could ruby's current behaviour be modified so that new threads do not know about the execution path that led to their creation, and therefore do not hold this stack in memory? Can someone please explain the current behaviour? Thanks, Tobias