I am not experienced enough to know most of the inner details, but it's fair to say that perl does a lot of work internally when the items are first accessed. If you change to Devel::Peek, you can see how the structure changes after access:

use strict; use warnings; use Devel::Peek qw(Dump); my $s = 'AAAAAAAAAAAAAAA'; my %hash = map {$s++ => 1} 1 .. 2; print "before:\n"; Dump \%hash; open my $fh, '>', 'j1.txt' or die $!; for my $key (keys %hash) { print $fh "$key $hash{$key}\n"; } print "after:\n"; Dump \%hash;

Output:

before: SV = IV(0x1e5cca8) at 0x1e5ccb8 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x1e88420 SV = PVHV(0x1e63ef0) at 0x1e88420 REFCNT = 2 FLAGS = (PADMY,SHAREKEYS) ARRAY = 0x1ed7880 (0:6, 1:2) hash quality = 125.0% KEYS = 2 FILL = 2 MAX = 7 Elt "AAAAAAAAAAAAAAA" HASH = 0xa39c9065 SV = IV(0x1e7a9a8) at 0x1e7a9b8 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 1 Elt "AAAAAAAAAAAAAAB" HASH = 0x7747aa6e SV = IV(0x1e7a990) at 0x1e7a9a0 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 1 after: SV = IV(0x1e883e0) at 0x1e883f0 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x1e88420 SV = PVHV(0x1e63ef0) at 0x1e88420 REFCNT = 2 FLAGS = (PADMY,OOK,SHAREKEYS) ARRAY = 0x1e75110 (0:6, 1:2) hash quality = 125.0% KEYS = 2 FILL = 2 MAX = 7 RITER = -1 EITER = 0x0 RAND = 0x69863a90 Elt "AAAAAAAAAAAAAAA" HASH = 0xa39c9065 SV = PVIV(0x1e7f860) at 0x1e7a9b8 REFCNT = 1 FLAGS = (IOK,POK,pIOK,pPOK) IV = 1 PV = 0x1ed7810 "1"\0 CUR = 1 LEN = 16 Elt "AAAAAAAAAAAAAAB" HASH = 0x7747aa6e SV = PVIV(0x1e7f878) at 0x1e7a9a0 REFCNT = 1 FLAGS = (IOK,POK,pIOK,pPOK) IV = 1 PV = 0x1e98b90 "1"\0 CUR = 1 LEN = 16

Here's a brief on memory usage.


In reply to Re: Devel::Size reports different size after hash access by stevieb
in thread Devel::Size reports different size after hash access by Cristoforo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.