On Thu, Oct 03, 2002 at 10:43:43AM +0100, Nicholas Clark wrote: > The reason I'm saying it might not be much use to people unfamiliar with > the internals of unix is precisely because it does return a precisely defined > but not directly useful value - the highest address on the heap. > If you read it, call perl code that creates a known perl structure, and read > it again, doesn't go up directly and exactly related to the amount of memory > that structure needed. Depending on how much other memory there is free, it > may not go up at all, or it may go up more if the allocation system grabs a > big block of free space.
Ok, now I'm really confused. - sbrk() returns, effectively, the size of the heap. - the heap may not go up when you create a perl structure but MemUsed() is shown to be used like this: use Devel::Internals; my @array = (1..10000); MemUsed "creation of array"; my @dup = @array; MemUsed "duplication of array"; and MemUsed, in scalar context, returns the sbrk value, the size of the heap. So I presume in the above code it simply returns the difference between the sbrk value between the two calls. But Nick just said this isn't actually the memory used, it's just the different in heap size. But the function is called MemUsed! Brane hurt! A lot of the problem here is the expectations from the names. - MemUsed() implies we're going to be able to figure out the amount of memory used by a hunk of Perl code without knowing a lot of magic. Instead, we get something about heap sizes. - sbrk() implies dark memory magic. Most people know it's just some memory thing beyond free() and malloc(). When they see it, they get scared, and rightfully so apparently. The problem is when you put those two next to each other, one promising a friendly interface, one a bare-metal interface, it confuses the intent of the module. Is it for Joe End User or is it for Joe Core Hacker? It would seem this module is for Joe Core Hacker, and it's all about sbrk(). So let's make that clear in the names: NAME Devel::sbrk - Access sbrk values SYNOPSIS use Devel::sbrk; my $end = sbrk (); my @array = (1..10000); print "The creation of this array used ", sbrk () - $end, "bytes\n"; # Easier use Devel::sbrk; my @array = (1..10000); sbrkChange "creation of array"; my @dup = @array; sbrkChange "duplication of array"; Ironically, I'm now advocating sbrk() as a name. :) Nick is right, if sbrk() is something special, call it that. With this interface, nobody's going to complain that sbrkChange() didn't actually return the amount of memory used by "my @dup = @array;" You might want to generalize the concept a bit to a more general "heap size" module. That way on systems where you don't have sbrk but can query the heap size it will still make sense. I don't know if they are quite the same thing, so I just leave it as a suggestion. NAME Devel::Memory::Heap - Access the size of the memory heap SYNOPSIS use Devel::Memory::Heap; my $end = HeapSize (); my @array = (1..10000); print "The creation of this array used ", HeapSize () - $end, "bytes\n"; # Easier use Devel::Memory::Heap; my @array = (1..10000); HeapChange "creation of array"; my @dup = @array; HeapChange "duplication of array"; A more broad Devel::Internals or a more user-friendly Devel::Memory can be left for later, and the functionality of Devel::Memory::Heap can be put into it if it makes sense. -- Michael G. Schwern <[EMAIL PROTECTED]> http://www.pobox.com/~schwern/ Perl Quality Assurance <[EMAIL PROTECTED]> Kwalitee Is Job One Here's some scholarly-ass opinions...