Limiting heap size for a Ruby process does not work

I would like to limit the heap size available for a Ruby process, ie if the process tries to grab more heap, it should abort. The Ruby process is called from a zsh. To test the concept, I did the following:

Inside my Ruby program, I calculate the heap size using the following function:

def heap_size
   GC.stat[:heap_length] *  # Number of pages
   GC::INTERNAL_CONSTANTS[:HEAP_OBJ_LIMIT] * # Number of slots in one page
   GC::INTERNAL_CONSTANTS[:RVALUE_SIZE] # Size of one slots in bytes
end

By setting suitable parameters for this program, I can see the initial heap size and verify how, for example, the heap increases, when a certain number of objects is allocated.

In my calling zsh scripts, I use the limit command to set the shell limits for the child processes, to have the following values:

cputime         unlimited
filesize        unlimited
datasize        9MB
stacksize       8MB
coredumpsize    0kB
addressspace    1MB
memorylocked    1MB
maxproc         266
descriptors     2560

With this setting, I can still manage to run processes which report the heap consumption as, for example, 1077120 bytes initial heap size, and 36214080 bytes after allocating data on the heap.

While the initial size is just below the limit of 1MB, the heap grows to approximately 36MB during execution, and I would have expected the operating system to kill the process.

Did I misunderstand the way in which the limit command is supposed to work?

I'm using OSX 10.6

BTW, I also verified the limits using ulimit, just in case the limit command of my zsh were broken somehow, but these too look reasonable:

$ /usr/bin/ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) 9216
file size               (blocks, -f) unlimited
max locked memory       (kbytes, -l) 1024
max memory size         (kbytes, -m) 1024
open files                      (-n) 2560
pipe size            (512 bytes, -p) 1
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 266
virtual memory          (kbytes, -v) 1024
链接地址: http://www.djcxy.com/p/80254.html

上一篇: 即使分配的内存少于ulimit限制,proc也会崩溃

下一篇: 限制Ruby进程的堆大小不起作用