On Wed 02 Oct 2002 22:11, Michael G Schwern <[EMAIL PROTECTED]> wrote: > On Wed, Oct 02, 2002 at 01:50:56PM +0200, H.Merijn Brand wrote: > > SYNOPSIS > > use Devel::Internals; > > A little broad. Perhaps Devel::Memory?
My intent was to gather more internal states. Whatever useful. > > my $end = sbrk (); > > > > my @array = (1..10000); > > print "The creation of this array used ", > > sbrk () - $end, "bytes\n"; > > > > # Easier > > use Devel::Internals; > > > > my @array = (1..10000); > > MemUsed "creation of array"; > > > > my @dup = @array; > > MemUsed "duplication of array"; > > > > DESCRIPTION > > sbrk > > returns the current memory top. See sbrk(2) manual page > > I realize that sbrk() is a familiar concept to deep C programmers on > BSD-style systems, but in order for this to be useful to Perl programmers, > or even users not familiar with BSD, you're going to have to name it > something more descriptive than sbrk() and explain better what it does. That's why there /is/ an Internals.pm: to hide the gory details and implementation specifics to the user. > It can still do the same thing as sbrk(), and you might even want to leave > in an alias to sbrk() so BSD core hackers can feel all warm and fuzzy. > > > > MemUsed ($msg) > > Used in void context reports the number of bytes used since the > > previous call to MemUsed with the message passed > > Global state is a Bad Thing. Consider when you have two different, But having only /one/ interpeter, it *is* a global state. Nothing you can do about it. > unrelated subroutines both calling MemUsed() in the same program. foo() > calls MemUsed() then bar() then foo() calls MemUsed() again expecting to get > the memory used since it's last call but it's actually getting the memory > used since bar() last called it. Bad luck. we don't have any tool to separate. > If you're going to keep state like this, better just have an object to > represent various views on the memory state. No way. This was meant to be a *low* level interface to help you find memory hogs and other possible snakes. I see no use whatsoever in any production environment. That's why I went for Devel:: namespace > I can see two interfaces for this, possibly parallel. One is similar to > your MemUsed() except the state is stored perl object. It simply returns > the memory used since the last check on that object. > > package A; > my $mem_state = Devel::Memory->new; > > sub foo { > printf "%d has been used since the last call to foo()\n", > $mem_state->used_since_last; > } > > The other one allows you to more easily track the memory usage of a block of > code by saying "track from this point to this point". Here's an example of > tracking the memory used inside a subroutine. > > # meanwhile, in another file > package B; > my $mem_state = Devel::Memory->new; > > sub bar { > $mem_state->start; > ...do some stuff... > printf "%d was used inside bar()\n", $mem_state->stop; > } > > For convinence, stop() returns the amount of memory used so you can do it in > two steps (start, stop/report) rather than three (start, stop, report). > > The other nice part about having an object is you can then hang lots of > methods off it without polluting the caller's namespace. So you can have > your methods which print neatly formatted diagnostics seperate from your > methods which simply return values. > > > > Used in scalar context returns the current sbrk value > > Shouldn't sbrk() do that? Is this the same value as MemUsed() would print > in void context? Complaints enough already about sbrk (). MemUsed () should hide the implementation. That's why. MemUsed ("blah"); # void context should print a message to STDERR while my $mem = MemUsed ("now"); # Scalar context should print nothing, but return the sbrk value, hiding the sbrk call to the user, but still putting it on the history stack. > > Used in list context returns the values saved on every call > > ? See another answer in this thread proposing to return a list of lists. I like that. -- H.Merijn Brand Amsterdam Perl Mongers (http://amsterdam.pm.org/) using perl-5.6.1, 5.8.0 & 633 on HP-UX 10.20 & 11.00, AIX 4.2, AIX 4.3, WinNT 4, Win2K pro & WinCE 2.11. Smoking perl CORE: [EMAIL PROTECTED] http:[EMAIL PROTECTED]/ [EMAIL PROTECTED] send smoke reports to: [EMAIL PROTECTED], QA: http://qa.perl.org