Latest Ruby on Rails prefork script

In my previous blog entries (here and here), I blogged about using fork() and copy-on-write semantics to reduce memory usage in Ruby on Rails.

Kevin requested that I post the latest version of my prefork script, so here it is. For the time being, I call it “RailsFork”. The code has been cleaned up a lot. This version kills all child processes when you kill RailsFork with Ctrl-C.

6 Comments »

  1. Ezra said,

    May 14, 2007 @ 9:52 am

    The problem with fork and cow memory in ruby is that as soon as ruby’s GC runs in a child process it walks the ObjectSpace touching every object and basically makes the kernel think it needs its own memory space for all objects. So as soon as GC runs in the forked child it needs as much ram as the parent and cow semantics are ruined.

  2. Hongli said,

    May 14, 2007 @ 2:01 pm

    Is that really so? I have heard of that story but I’m unable to reproduce it. For instance, look at this test script:

    a = " " * 1024 * 1024 * 100
    b = " " * 1024 * 1024 * 50
    pid = fork do
    	$stdin.readline
    	b = nil
    	ObjectSpace.garbage_collect
    	$stdin.readline
    end
    Process.waitpid(pid)

    After I press Enter for the first time (thus forcing garbage collection), I do not see my memory usage go up at all.

  3. Hongli said,

    May 22, 2007 @ 9:36 am

    I’ve had more success with this script:

    a = []
    (1024 * 1024).times do
    	a.push(" " * 200)
    end
    pid = fork do
    	while true
    		$stdin.readline
    		a = nil
    		ObjectSpace.garbage_collect
    	end
    	exit!
    end
    Process.waitpid(pid)
    

    Memory usage increased a bit, but not as much as I heard it would.

  4. R.O. said,

    June 25, 2007 @ 3:26 pm

    Would like a followup to this. This single issue about GC ruining COW semantics. Is there any work on improving to a more intelligent GC?
    Thanks.

  5. 赖洪礼的 blog » Status report said,

    July 20, 2007 @ 1:25 pm

    [...] been a while since I worked on my Ruby on Rails-related projects, saving memory by using copy-on-write and prepared statements support in ActiveRecord. They were on hold for a while since I’ve [...]

  6. Nina said,

    January 21, 2009 @ 4:05 pm

    Oh, it’s true, I know!

RSS feed for comments on this post · TrackBack URI

Leave a Comment