I want to restrict memory usage in my test, so I tested the behavior of Process::setrlimit
first, and it confuses me. Here is my code:
require 'securerandom'
Process.setrlimit(:AS, 2 << 20) # 2MB total memory, on Mac OS X
Process.setrlimit(:RSS, 2 << 20) # 2MB resident memory, on both Mac and Linux
s = SecureRandom.hex(5 << 20) # hexadecimal string from 5MB random data
puts s.length
I don't have any swap partition in my Linux laptop, so I suppose the RSS is equal to the total memory.
I expect this program to crash, but it doesn't. It just prints out 10485760
which is the correct length of that string. Why doesn't it crash? How can I correctly set the memory limit in the program itself?