John Carter wrote: > Some old Pascal implementations had (and I think some still do) had a > facility to "mark" the heap, and then at some point "release" all > items allocated after that mark. > > Here is a nifty way of doing the same (and more!) in ruby.... > > ==========================try.rb====================================== > pid = Process.fork do > # Load any modules we need > require 'find' > > a = 'x' * 100*1024*1024 > > > end > > pid, result = Process.waitpid2( pid) > ====================================================================== If possible, disable GC in the fork. That can greatly reduce memory usage because the GC mark algorithm has to touch every reachable block of allocated heap memory. So the memory manager has to copy most of the original process anyway--the COW advantage is lost. This is especially true if the parent process has a lot of objects. Example: a = (1..2_000_000).map {[]} # emulate a big ObjectSpace 10.times do pid = fork do GC.disable if ARGV[0] == "nogc" a = 'x' * 10*1024*1024 # trigger GC, if enabled puts `free`[/Swap.*/] end end Process.waitall $ time ruby fork-gc.rb nogc Swap: 489940 137340 352600 Swap: 489940 137340 352600 Swap: 489940 137340 352600 Swap: 489940 137340 352600 Swap: 489940 137340 352600 Swap: 489940 137340 352600 Swap: 489940 137340 352600 Swap: 489940 137340 352600 Swap: 489940 137340 352600 Swap: 489940 137340 352600 ruby fork-gc.rb nogc 5.29s user 0.62s system 97% cpu 6.049 total $ time ruby fork-gc.rb Swap: 489940 326976 162964 Swap: 489940 327100 162840 Swap: 489940 327336 162604 Swap: 489940 330228 159712 Swap: 489940 334664 155276 Swap: 489940 330456 159484 Swap: 489940 329060 160880 Swap: 489940 328124 161816 Swap: 489940 327148 162792 Swap: 489940 327072 162868 ruby fork-gc.rb 8.82s user 2.97s system 28% cpu 40.712 total Note the big increase in swap used (second column of numbers). ** Caution: on my 512MB system this can thrash for a while. If you have less memory, change the parameters. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407