Friday, July 3, 2009

How to kill your system by allocating all memory in perl

Sometimes you want to see how things behave on a host when it runs out of memory

We actually caused a linux host with 128GB RAM to hang until we pulled the plug by doing this.

To allocate more memory, change $procs to a bigger number. If you have 64bit perl, you can kill the host with 1 process, just concatenating strings with the subroutine below.


#!perl
use forks;

my $procs = 2;
for (1..$procs) {
print "Forking: " . `date`;
threads->new(\&mem_load);
}
print "sleeping: " . `date`;
sleep 10;


sub mem_load {
my $str = "0123456789abcdef" x 1024;
$str = $str x 1024;

my $mem = $str;
for (1..130) {
$mem .= $str;
print "$$: $_\n";
}
sleep 9999999;
}

No comments:

Post a Comment