The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Internals::CountObjects - Report all allocated perl objects

VERSION

version 0.05

SYNOPSIS

  use Internals::CountObjects;
  print dump_objects();

DESCRIPTION

This module provides a simple count of each kind of object in memory. It can be used as an ordinary perl module or injected into running processes in production emergencies.

An example report, including the process ID on each line:

  =7201= Memory stats
  =7201= undef: 1176
  =7201= REF: 781
  =7201= ARRAY: 484
  =7201= GLOB: 388
  =7201= string/integer: 262
  =7201= integer: 253
  =7201= CODE: 162
  =7201= string/double: 139
  =7201= HASH: 87
  =7201= REGEXP: 60
  =7201= IO::File: 8
  =7201= version: 3
  =7201= double: 3
  =7201= FORMAT: 2
  =7201= string: 1
  =7201= Config: 1

FUNCTIONS

dump_objects()

Returns a formatted report.

dump_objects($prev_objects)

Returns a formatted report, showing the differences between a previous call to objects().

  $objects = objects();
  # do something
  print dump_objects($objects);

  Memory stats (delta from previous)
  =2628= integer: 272 (+11)
  =2628= REF: 798 (+4)
  =2628= string/integer: 272 (+2)
  =2628= string: 2 (+1)
  =2628= ARRAY: 493 (+1)
  =2628= double: 4 (+1)
  =2628= HASH: 90 (+1)
  =2628= FORMAT: 2
  =2628= IO::File: 8
  =2628= version: 3
  =2628= CODE: 164
  =2628= Config: 1
  =2628= REGEXP: 60
  =2628= GLOB: 394
  =2628= string/double: 139
  =2628= undef: 1362 (-21)

objects()

Returns the data used for the report in a hash reference. An example of a typical hash:

  %report = (
    'undef'          => 1176,
    'REF'            => 781,
    'ARRAY'          => 484,
    'GLOB'           => 388,
    'string/integer' => 262,
    'integer'        => 253,
    'CODE'           => 162,
    'string/double'  => 139,
    'HASH'           => 87,
    'REGEXP'         => 60,
    'IO::File'       => 8,
    'version'        => 3,
    'double'         => 3,
    'FORMAT'         => 2,
    'string'         => 1,
    'Config'         => 1
  );

IN EMERGENCIES

You can get this kind of report from a running perl / httpd process. You attach to the process with gdb and eval() some ordinary perl code.

In the below example, we'll pick a httpd, attach to it and eval some perl to get a report. It's a good idea to kill the process off afterward.

  $ ps ax | grep httpd
  11340 pts/1    t      0:00 grep httpd
  11342 ?        Ss     0:12 /usr/bin/httpd -f /var/www/conf/httpd.conf
  11343 ?        S      0:03 /usr/bin/httpd -f /var/www/conf/httpd.conf
  11344 ?        S      0:00 /usr/bin/httpd -f /var/www/conf/httpd.conf
  11346 ?        R      0:00 /usr/bin/httpd -f /var/www/conf/httpd.conf

  $ gdb -p 11346
  (gdb) call Perl_eval_pv("use Internals::CountObjects; print STDERR dump_objects()", 0)
  (gdb) detach
  (gdb) quit