Joe Jack
Joe Jack

Reputation: 21

Use all system memory in Solaris

I am doing server testing and intentionally trying to crash a Solaris server by using all memory. Server has a ulimit of 15 GB (several hundred GB of total system/swap memory). Is there a straight forward way of doing this using a shell/Perl script? (Edit: This is in a hardened/secured environment so the only tool I have access to is Perl [and shell of course]).

Background: What is actually being tested is failover of essential processes to another server. Lately we have been having a number of processes "go out of control" and consume all system resources thus causing server to crash (separate issue). I am trying to create a simple repeatable scenario to test the response to this situation.

Upvotes: 2

Views: 944

Answers (2)

amon
amon

Reputation: 57640

In Perl, you can allocate a fixed amount of memory quite precisely through strings, and the repetition operator x. As Perl stores characters in a two-byte representation, the following function will return a scalar that points to $x MB of memory:

sub allocate_MB {
   my ($x) = @_;
   "0" x ( $x / 2 * 2**(20))
}

(however, only tested on Ubuntu with perl 5.12)

When Perl can't allocate any more memory, it will die with a message telling you so.

A simple my @stor; push @stor, allocate_MB(200) while 1 should quickly hit any limit.

Upvotes: 0

jlliagre
jlliagre

Reputation: 30823

Assuming /tmp is mounted on tmpfs and you didn't set a quota on it, (i.e. the default configuration), here is a way to use almost all of your memory using a shell script (beware that your system will be barely usable as soon as your RAM is exhausted):

#!/bin/ksh

function trapped
{
  echo "Eatswap done"
  rm -f /tmp/eatSwap.*
  exit
}

function showFreeSpace
{
  freeSpace=$(swap -s | sed -e 's/.*, //' -e 's/k.*//')
  echo free space = $freeSpace KB
}

trap trapped 2
counter=0
i=0
showFreeSpace
fs=$freeSpace
while [ $i -lt 10 ]; do
  dd if=/dev/zero of=/tmp/eatSwap.$i bs=$(($fs/10)) count=1024 2>/dev/null || trapped
  sleep 2
  showFreeSpace
  i=$((i+1))
done
echo "done"
sleep 2
rm -f /tmp/eatSwap.*

Upvotes: 3

Related Questions