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’ * 10010241024
end
pid, result = Process.waitpid2( pid)
Here is an edited version of the result of running (from root)
strace -v -f -o strace.log ruby try.rb
======================================================================
1597 execve("/usr/local/bin/ruby", [“ruby”, “try.rb”], [“HZ=100”,
“SHELL=/bin/bash”, “TERM=xterm”, “OLDPWD=/root”, “USER=root”,
“MAIL=/var/mail/root”, “PATH=/usr/local/sbin:/usr/local/”…,
“PWD=/home/johnc/tmp”, "PS1=\h:\w\$ ", “SHLVL=1”, “HOME=/root”,
“LOGNAME=root”, “DISPLAY=:0.0”, “_=/usr/bin/strace”]) = 0
…all the start up cost of invoking ruby paid once and only once…
Here is the OS level call to fork…
1597 clone(child_stack=0,
flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD,
child_tidptr=0xb7d57708) = 1598
Note this is really really very fast as unix just creates a complete
copy via COW pages (copy on write) using virtual memory magic.
Parent proc hands waiting for child…
1597 waitpid(1598, <unfinished …>
Child proc loads and evals find.rb
1598 open("/usr/local/lib/ruby/1.9/find.rb", O_RDONLY|O_LARGEFILE) = 3
1598 close(3) = 0
1598 open("/usr/local/lib/ruby/1.9/find.rb", O_RDONLY|O_LARGEFILE) = 3
1598 ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0xbfd1baa8) = -1 ENOTTY
(Inappropriate ioctl for device)
1598 read(3, “#\n# find.rb: the Find module for”…, 8192) = 1922
Child proc grabs a huge chunk more memory
1598 brk(0x81c1000) = 0x81c1000
1598 mmap2(NULL, 104861696, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb1925000
Child exits…
1598 exit_group(0) = ?
1597 <… waitpid resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0)
= 1598
1597 — SIGCHLD (Child exited) @ 0 (0) —
WHEE! Bang! All the memory and resources associated with the child
are reclaimed completely and instantly by the OS.
parent continues on it merry way light and free…
John C. Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : [email protected]
New Zealand
Carter’s Clarification of Murphy’s Law.
“Things only ever go right so that they may go more spectacularly wrong
later.”
From this principle, all of life and physics may be deduced.