Rename to Devel::SizeMe
[p5sagit/Devel-Size.git] / lib / Devel / SizeMe.pm
1 package Devel::SizeMe;
2
3 # As a handy convenience, make perl -d:SizeMe automatically call heap_size
4 # in an END block, and also set some $^P flags to get more detail.
5 my $do_size_at_end; # set true below for "perl -d:SizeMe ..."
6 BEGIN {
7     if ($^P) {
8         warn "Note: Devel::SizeMe currently disables perl debugger mode\n";
9     warn scalar keys %INC;
10         # default $^P set by "perl -d" is 0x73f
11         $^P = 0x10  # Keep info about source lines on which a sub is defined
12             | 0x100 # Provide informative "file" names for evals
13             | 0x200 # Provide informative names to anonymous subroutines;
14             ;
15         $do_size_at_end = 1;
16     }
17 }
18
19 use strict;
20 use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS $warn $dangle);
21
22 require 5.005;
23 require Exporter;
24 require XSLoader;
25
26 $VERSION = '0.02';
27 @ISA = qw(Exporter);
28
29 @EXPORT_OK = qw(size total_size perl_size heap_size);
30 %EXPORT_TAGS = ( 'all' => \@EXPORT_OK ); # for use Devel::SizeMe ':all';
31
32 $warn = 1;
33 $dangle = 0; ## Set true to enable warnings about dangling pointers
34
35 $ENV{SIZEME} ||= "| sizeme_store.pl --showid --db=sizeme.db";
36
37 XSLoader::load( __PACKAGE__);
38
39 END {
40     Devel::SizeMe::perl_size() if $do_size_at_end;
41 }
42
43 1;
44 __END__
45
46 =pod
47
48 Devel::SizeMe - Perl extension for finding the memory usage of Perl variables
49
50 =head1 SYNOPSIS
51
52   use Devel::SizeMe qw(size total_size);
53
54   my $size = size("A string");
55   my @foo = (1, 2, 3, 4, 5);
56   my $other_size = size(\@foo);
57   my $total_size = total_size( $ref_to_data );
58
59 =head1 DESCRIPTION
60
61 Acts like Devel::Size 0.77 if the SIZEME env var is not set.
62
63 Except that it also provides perl_size() and heap_size() functions.
64
65 If SIZEME env var is set to an empty string then all the *_size functions
66 dump a textual representation of the memory data to stderr.
67
68 If SIZEME env var is set to a string that starts with "|" then the
69 remainder of the string is taken to be a command name and popen() is used to
70 start the command and the raw memory data is piped to it.
71
72 If SIZEME env var is set to anything else it is treated as the name of a
73 file the raw memory data should be written to.
74
75 The sizeme_store.pl script can be used to process the raw memory data.
76 Typically run via the SIZEME env var. For example:
77
78     export SIZEME='|./sizeme_store.pl --text'
79     export SIZEME='|./sizeme_store.pl --dot=sizeme.dot'
80     export SIZEME='|./sizeme_store.pl --db=sizeme.db'
81
82 The --text output is similar to the textual representation output by the module
83 when the SIZEME env var is set to an empty string.
84
85 The --dot output is suitable for feeding to Graphviz.
86
87 The --db output is a SQLite database. (Very subject to change.)
88
89 Example usage:
90
91   SIZEME='|sizeme_store.pl --db=sizeme.db' perl -MDevel::SizeMe=:all -e 'total_size(sub { })'
92
93 The sizeme_graph.pl script is a Mojolicious::Lite application that serves data to
94 an interactive treemap visualization of the memory use. It can be run as:
95
96     sizeme_graph.pl daemon
97
98 and then open http://127.0.0.1:3000
99
100 Please report bugs to:
101
102     http://rt.cpan.org/NoAuth/Bugs.html?Dist=Devel-SizeMe
103
104 =head1 COPYRIGHT
105
106 Copyright (C) 2005 Dan Sugalski,
107 Copyright (C) 2007-2008 Tels,
108 Copyright (C) 2008 BrowserUK,
109 Copyright (C) 2011-2012 Nicholas Clark,
110 Copyright (C) 2012 Tim Bunce.
111
112 This module is free software; you can redistribute it and/or modify it
113 under the same terms as Perl v5.8.8.
114
115 =head1 SEE ALSO
116
117 perl(1), L<Devel::Size>.
118
119 =cut