Memchmark - Check memory consumption


  use Memchmark qw(cmpthese);
  my @data = map { rand($_) } 0..10000;
  cmpthese( -init => sub { my @s =  @data },
            nsort => sub { my @s = sort { $a <=> $b } @data },
            rnsort => sub { my @s = sort { -($a <=> $b) } @data } );


Memchmark is similar to Benchmark but compares memory comsumptions instead of times.

To measure memory comsumption for some subroutine, Memchmark forks a new process to run the sub and then monitors its memory usage every 100ms (aprox.) recording the maximum amount used.

The obtained quantities are only approximate, you can expect errors around 30%.

It is not reliable for small quantities (useles for anything below 1MB).


These are the subroutines available from Memchmark:

cmpthese(foo => sub { ... }, bar => sub { ... }, ...)

prints statistics about the memory comsumption for the different subs.

An entry with the name -init can be used for a do nothing entry which memory comsumption will be substracted to the results from the rest of the tests.

memchmark { &code() };

returns the memory used by &code().


This is a very early release, alpha software, expect bugs on it.

The API is not stable. I will change it when required for improvement.

It will not work under Windows (ever).


Benchmark, Proc::ProcessTable, Proc::ProcessTable::Process, fork.


Salvador Fandiño, <>


Copyright (C) 2005 by Salvador Fandiño

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.