sang1986 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I see the Devel::Size::total_size returnd different values in perl5.8.6 and perl5.10. Can someone please tell me why this is so?
sankaran@desb0018.nyc:> /usr/local/perl-5.8.6/bin/perl -e 'use Devel:: +Size; my @arr; foreach my $i (1..10) { push (@arr, $i); }; my $size = + Devel::Size::total_size(\@arr); print "\nSIZE : $size\n";' SIZE : 260
In Perl5.10
sankaran@desb0018.nyc:> /usr/local/bin/perl5.10 -e 'use Devel::Size; m +y @arr; foreach my $i (1..10) { push (@arr, $i); }; my $size = Devel: +:Size::total_size(\@arr); print "\nSIZE : $size\n";' SIZE : 528

Replies are listed 'Best First'.
Re: perl5.10 Devel::Size behaviour changed?
by ikegami (Patriarch) on Aug 25, 2009 at 13:58 UTC
    I get:
    >c:\progs\perl580\bin\perl test.pl SIZE : 268 >c:\progs\perl588\bin\perl test.pl SIZE : 268 >c:\progs\perl5100\bin\perl test.pl SIZE : 312

    4.4 bytes per element.

    Or I loop 100 times:

    >c:\progs\perl580\bin\perl test.pl SIZE : 2156 >c:\progs\perl588\bin\perl test.pl SIZE : 2156 >c:\progs\perl5100\bin\perl test.pl SIZE : 2200

    Still only 44 extra bytes. So they're tied to the array, not the elements. Adding Devel::Peek to the mix:

    >c:\progs\perl588\bin\perl test.pl SIZE : 2156 SV = RV(0x185d34c) at 0x1820714 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x236da4 SV = PVAV(0x23bfcc) at 0x236da4 REFCNT = 2 FLAGS = (PADBUSY,PADMY) IV = 0 NV = 0 ARRAY = 0x18799ac FILL = 99 MAX = 123 ARYLEN = 0x0 FLAGS = (REAL) >c:\progs\perl5100\bin\perl test.pl SIZE : 2200 SV = RV(0x1950668) at 0x195065c REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x1829944 SV = PVAV(0x23a120) at 0x1829944 REFCNT = 2 FLAGS = (PADMY,RMG) MAGIC = 0x1823f0c MG_VIRTUAL = &PL_vtbl_arylen_p MG_TYPE = PERL_MAGIC_arylen_p(@) MG_FLAGS = 0x02 REFCOUNTED ARRAY = 0x183502c FILL = 99 MAX = 123 ARYLEN = 0x0 FLAGS = (REAL)

    I wonder why the array comes with magic attached!

    Update: Added Devel::Peek dump.

      I couldn't leave it at that. It seems that Devel::Size actually modifies the array!
      >perl -MDevel::Size=total_size -MDevel::Peek -e"my @a; total_size(\@a) + if $ARGV[0]; Dump \@a,1" 0 SV = RV(0x239ba8) at 0x239b9c REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x1829954 SV = PVAV(0x23aa60) at 0x1829954 REFCNT = 2 FLAGS = (PADMY) ARRAY = 0x0 FILL = -1 MAX = -1 ARYLEN = 0x0 FLAGS = (REAL) >perl -MDevel::Size=total_size -MDevel::Peek -e"my @a; total_size(\@a) + if $ARGV[0]; Dump \@a,1" 1 SV = RV(0x239ba8) at 0x239b9c REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x1829954 SV = PVAV(0x23aa60) at 0x1829954 REFCNT = 2 FLAGS = (PADMY,RMG) MAGIC = 0x1824014 MG_VIRTUAL = &PL_vtbl_arylen_p MG_TYPE = PERL_MAGIC_arylen_p(@) MG_FLAGS = 0x02 REFCOUNTED ARRAY = 0x0 FILL = -1 MAX = -1 ARYLEN = 0x0 FLAGS = (REAL)

      It's similar but different than using $#a:

      >perl -MDevel::Size=total_size -MDevel::Peek -e"my @a; $#a if $ARGV[0] +; Dump \@a,1" 0 SV = RV(0x239260) at 0x239254 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x182991c SV = PVAV(0x23a168) at 0x182991c REFCNT = 2 FLAGS = (PADMY) ARRAY = 0x0 FILL = -1 MAX = -1 ARYLEN = 0x0 FLAGS = (REAL) >perl -MDevel::Size=total_size -MDevel::Peek -e"my @a; $#a if $ARGV[0] +; Dump \@a,1" 1 SV = RV(0x239340) at 0x239334 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x182991c SV = PVAV(0x23a168) at 0x182991c REFCNT = 2 FLAGS = (PADMY,RMG) MAGIC = 0x1823de4 MG_VIRTUAL = &PL_vtbl_arylen_p MG_TYPE = PERL_MAGIC_arylen_p(@) MG_FLAGS = 0x02 REFCOUNTED MG_OBJ = 0x239254 SV = PVMG(0x18fb364) at 0x239254 REFCNT = 1 FLAGS = (GMG,SMG) IV = 0 NV = 0 PV = 0 MAGIC = 0x1823f0c MG_VIRTUAL = &PL_vtbl_arylen MG_TYPE = PERL_MAGIC_arylen(#) MG_OBJ = 0x182991c ARRAY = 0x0 FILL = -1 MAX = -1 ARYLEN = 0x239254 FLAGS = (REAL)
Re: perl5.10 Devel::Size behaviour changed?
by dave_the_m (Monsignor) on Aug 25, 2009 at 12:45 UTC
    There is no way 5.10.0 is using twice as much memory as 5.8.6 under normal circumstances. I would strongly suspect that you're using a 32-bit 5.8.6 and a 64-bit 5.10.0. Either that or you're using an old version of Devel::Size that doesn't know about the new SV header arrangements in 5.10.0 (which in general use less memory than 5.8.x).

    Dave.

Re: perl5.10 Devel::Size behaviour changed?
by Anonymous Monk on Aug 25, 2009 at 09:55 UTC
    The behavior hasn't changed, the size is different.
Re: perl5.10 Devel::Size behaviour changed?
by BrowserUk (Patriarch) on Aug 25, 2009 at 17:48 UTC

    Which version of D::S are you using? This one or this one

    And which builds of perl? 32-bit or 64-bit? Both the same? Same platform?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      It turns out I was using a 64-bit 5.10 and 32-bit 5.8.6. but in some cases I see a more than twice difference between the 2 versions. The 32-bit 5.8.6 gives me 851144. The 64-bit 5.10 gives me this alarming huge number 49563404. Any idea in what cases this could be so?
        take this example. I get 56 with 5.8 32-bit but 176 with 64-bit perl5.10.
        perl -MDevel::Size=total_size -MDevel::Peek -e 'my @a; my $s = total_s +ize(\@a);print "\nSIZE : $s\n"; Dump \@a, 1' SIZE : 56 SV = RV(0x822f26c) at 0x8184a60 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x8184b14 SV = PVAV(0x8186c54) at 0x8184b14 REFCNT = 2 FLAGS = (PADBUSY,PADMY) IV = 0 NV = 0 ARRAY = 0x0 FILL = -1 MAX = -1 ARYLEN = 0x0 FLAGS = (REAL)
        with 5.10
        sankaran@invest5.hyd:/u/sankaran/base/src/perl/Deshaw/PDL> /usr/local +/bin/perl5.10 -MDevel::Size=total_size -MDevel::Peek -e 'my @a; my $ +s = total_size(\@a);print "\nSIZE : $s\n"; Dump \@a, 1' SIZE : 176 SV = RV(0x417520) at 0x417510 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x4b1ba8 SV = PVAV(0x41a388) at 0x4b1ba8 REFCNT = 2 FLAGS = (PADMY,RMG) MAGIC = 0x57dd10 MG_VIRTUAL = &PL_vtbl_arylen_p MG_TYPE = PERL_MAGIC_arylen_p(@) MG_FLAGS = 0x02 REFCOUNTED ARRAY = 0x0 FILL = -1 MAX = -1 ARYLEN = 0x0 FLAGS = (REAL)

        Again, I ask: which version of Devel::Size are you using?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.