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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |