9

I am trying to test a specific condition the will only occur if perl has a malloc that fails due to there being no memory left. I would like perl to die as quickly as possible. I figured the fasted way would be create some huge arrays like

perl -le '$_->[100_000_000_000] = 1 for \(@a, @b, @c, @d); <>'

But I had to kill it after my swap hit 5 gig with no signs of stopping (I am on OS X 10.6).

I just tested it on Linux and it dies pretty quick:

time perl -le '$_->[1_000_000_000] = 1 for \(@a, @b, @c, @d); <>'
Out of memory!

real    0m0.023s
user    0m0.012s
sys     0m0.008s

So the problem seems to be OS X and its dynamic_pager.

I just tried disabling the dynamic_pager with

sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist

and rebooting, but the machine just hangs completely. My next attempt will be to change the com.apple.dynamic_pager.plist config file to write the vm files to a very small partition.

4
  • 1
    Strange that you didn't "x" x 9e9.
    – tsee
    Sep 5, 2010 at 16:00
  • @tsee It takes time to build the string, but perl -e '@a[9e8]=1' is effectively just a malloc. Sep 5, 2010 at 17:59
  • "I had to kill it after my swap hit 5 gig with no signs of stopping (I am on OS X 10.6)." - Mac OS X shows that type of behavior often, including the cases like malloc(-1). Try any other OS where you can explicitly disable swap. (Never tried doing that on my Mac - but do that casually on the Linux.)
    – Dummy00001
    Sep 5, 2010 at 20:02
  • @Dummy00001 I tired in on Linux already, it ran out of memory instantly. Sep 5, 2010 at 20:38

1 Answer 1

5

In a previous question "How to simulate memory allocation errors", user freespace suggested using ulimit with a test user account to limit the amount of memory that could be used. This may do what you want without having to allocate huge amounts of memory.

2
  • That doesn't seem to work on OS X. I tried ulimit -m 64; perl -e '$s = "x" x (1024*1024*100); <>' and checked memory usage and the perl program owned two hundred megabytes. Sep 5, 2010 at 1:20
  • 1
    IIRC it should be ulimit -d, not -m.
    – Dummy00001
    Sep 5, 2010 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.