From: Matma Rex Date: 2013-01-14T09:34:30+09:00 Subject: Re: Garbage Collection and Fibers On Mon, 14 Jan 2013 00:48:09 +0100, Na Na wrote: > Hi, > > I've been having an issue with my program running slow at points where > it really just shouldn't. I've finally determined (I think) that the > culprit is actually garbage collection being called. > > I want to have more control over GC. However, the problem is, I suspect > that later on it could take 1 second or longer each time it runs, and I > can't have my program being blocked for that long. One option is that I > could put GC in a separate thread (I haven't tested whether this will > cause stability issues). Another, I wondered, is whether or not GC > could be called in a fiber somehow so that its execution load can be > stretched out over 5 seconds or so as I deem appropriate? > > Other things I'll be doing will be seeing if I can find ways to reduce > the number of objects I am creating, but I suspect that I will need to > look at the above kinds of improvements even after this. > > Thanks! You can't do any of that. Ruby has a stop-the-world mark-and-sweep garbage collector, which means that every time GC is called *everything* (all threads, fibers, whatever) has to stop executing while GC walks *all* of your objects (starting from constants, global variables and variables in active scopes) to mark them as used and then deallocates the rest. This process can't be paused or executed partially, nor can any Ruby code run while it's in progress, because modifying the object tree while GC is traversing it could cause Ruby to miss some of your objects, and mistakenly deallocate them when they're still used. Only thing you can do is call GC.start when you want the GC to happen. -- Matma Rex