2 Memory Regression Perl Scripts for Linux

by SathiyaMoorthy on November 4, 2011

During performance testing of your application you might want to perform some sort of memory regression testing. This article contains two memory regression scripts that are written in perl which will occupy a specific amount of memory for a certain amount of time, for your testing.

1. Basic Memory Regression Script

To execute this script, do the following:

$ perl memtest.pl 100

The above example will occupy 100 MB of memory, and waits for your input – a simple enter will terminate the script and releases the memory. This way you can let this script occupy the amount of memory you need for as much time as you need.

First argument is taken as the amount of memory to be occupied ( in MB ).

Note: Don’t execute this script on any critical system. Be careful while running this script. Don’t give a large memory value to this script. If the amount of memory given is huge or not available, your system might hang.

Do ‘vi memtest.pl’ and copy/paste the following perl code to create this file.

# store and validate the command line parameter
$mb = $ARGV[0];
unless ( defined $mb and $mb =~ /^\d+$/ and $mb >= 1)  {
    die "Usage: $0 <occupy MB>\nEx: $0 100 - occupies 100 MB memory\n"
}
# convert it to bytes.
$b = $mb * 1024 * 1024;

# open in-memory file, and seek to size specified to get memory from OS.
open MEM, '>', \$memfile;
seek MEM, $b - 1, 0;
print MEM 'A';
close MEM;
printf "$mb MB memory is occupied, press ENTER to release: "; <STDIN>;

# till here the memory is occupied by this program.
undef $memfile;
printf "Memory released";

2. Advanced Memory Regression Script

To execute this script, do the following:

$ perl memtest-adv.pl 250

(or)

$ perl memtest-adv.pl 25%

This memtest-adv.pl works the same way as basic script, but you can also specify percentage as an input. When your system has total of 2GB of physical memory, you can specify 25%, which will occupy 500MB of memory for testing.

First argument can be either the amount of memory in MB or percentage of memory to be occupied, where percentage represents the percentage against total primary memory available.

Note: This Perl script occupies approximately the given amount of memory. On the system we tested, it took a maximum of 5MB more. So, do not use this on any critical system. Use this script only on a test system, as the system might hang.

Do ‘vi memtest-adv.pl’ and copy/paste the following perl code to create this file.

# calculate memory to be occupied from percentage given
sub find_memto_occupy
{
    $pc = $_[0];
    die "Wrong percentage given $pc\n" if ($pc > 100);

    open MEMINFO, '<', '/proc/meminfo' or die "Unable to open /proc/meminfo to find available memory\n";
    my $mem = <MEMINFO>;

    if ( $mem =~ /^MemTotal:\s+(\d+)\s.*$/ )  {
        $mem = $1;
    } else {
        die "Unable to find the available memory\n";
    }

    $mem = ( $mem / 100 ) * $pc;
    return int($mem / 1024);
}

# main script
{
    $num = $ARGV[0];
    unless ( defined $num and $num =~ /^\d+%?$/ and $num >= 1)  {
             die "Usage: $0 <occupy MB>\nEx: $0 100 - occupies 100 MB memory\n"
    }

    if ( $num =~ /^(\d+)%$/ )  {
        # convert percentage to bytes.
        $pc = $1;
        $mb = find_memto_occupy($pc);
    } else {
        $mb = $num;
    }

    $b = $mb * 1024 * 1024;
    open MEM, '>', \$memfile;
    seek MEM, $b - 1, 0;
    print MEM chr(0);
    close MEM;

    print "$mb MB memory is occupied, press ENTER to release: "; <STDIN>;
    undef $memfile;
    print "Memory released";
}

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 6 comments… read them below or add one }

1 toodoo November 4, 2011 at 1:21 am

It’s an useful script for test memory usage. But there are some issue. In the memtest-adv.pl:


print MEM chr(0);
close RAM;

print “$mb MB memory is occupied, press ENTER to release: “; ;
undef $ramfile;
print “Memory released”;
}

The “close RAM;” should be “close MEM;”, and “undef $ramfile;” should be “undef $memfile;”.

2 luis November 4, 2011 at 5:47 am

sorry, but wouldn’t it be ” undef $memfile;” in both scripts?

3 Deives Michellis November 4, 2011 at 6:17 am

Great idea, although you have a few minor errors within your code, as you would not properly release the memory file (not that it mattered, for your program would just exit and free whatever you had in use). For coding sake, here is a fixed version of your script:

#!/usr/bin/perl
use strict;
# calculate memory to be occupied from percentage given
sub find_memto_occupy
{
my $pc = $_[0];
die “Wrong percentage given $pc\n” if ($pc > 100);

open MEMINFO, ‘<', '/proc/meminfo' or die "Unable to open /proc/meminfo to find available memory\n";
my $mem = ;

if ( $mem =~ /^MemTotal:\s+(\d+)\s.*$/ ) {
$mem = $1;
} else {
die “Unable to find the available memory\n”;
}

$mem = ( $mem / 100 ) * $pc;
return int($mem / 1024);
}

# main script
{
my $num = $ARGV[0];
my ($pc, $mb, $memfile);
unless ( defined $num and $num =~ /^\d+%?$/ and $num >= 1) {
die “Usage: $0 \nEx: $0 100 – occupies 100 MB memory\n”
}

if ( $num =~ /^(\d+)%$/ ) {
# convert percentage to bytes.
$pc = $1;
$mb = find_memto_occupy($pc);
} else {
$mb = $num;
}

$b = $mb * 1024 * 1024;
open MEM, ‘>’, \$memfile;
seek MEM, $b – 1, 0;
print MEM chr(0);
close RAM;

print “$mb MB memory is occupied, press ENTER to release: “; ;
undef $memfile;
print “Memory released”;
}

4 Question November 4, 2011 at 8:05 am

Coild you explain how this helps woth regression testing? I am familiar with thee concept of regression testing but am co fused how occupying RAM helps you test your application. Shouldn’t you be instead be monitoring the application to see if it uses more memory than rhe last release?

5 Ramesh Natarajan November 4, 2011 at 10:28 pm

@All,

Thanks for pointing out the issue. It is now changed to “close MEM” and “undef $memfile”

6 Gabor Szabo November 9, 2011 at 4:37 am

You don’t need all that memory file operation there. You could simply write:

my $memfile = ‘A’ x $size_in_bytes;

As a side comment, please always, even in small scripts, “use strict” and “use warnings”.

Also it is very much recommended to always use lexical variables as file handles:

open my $MEMINFO, ‘<', '/proc/meminfo'

Leave a Comment

Previous post:

Next post: