> The reason is that in the later case, the data is read into an internal mortal temporary scalar; and then copied from there to the named lexical, before the memory attached to the temp is freed. As the allocation is greater than (from memory) 1MB, (on windows at least) such huge allocations are allocated directly from the OS's virtual memory rather than from the process' heap; and then get released directly back to OS.
After having fixed the code, I ran your code with the following output:
6378 VM: RSS: VSZ VM: RSS: 17780 2000000000 VM: RSS: VSZ VM: 1970908 RSS: 1957712 VM: RSS: VSZ VM: RSS: 17780 2000000000 VM: RSS: VSZ VM: 4412316 RSS: 3910984 VM: RSS: VSZ VM: 1970908 RSS: 1957856
Wait, it seems the memory consumption is indeed doubled! I checked with my previous code to realize I was running it on a different machine with different Perl version. I tried with blead, too, and it seems memory is doubled on Linux in 5.18, but not in 5.20 and later.
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
In reply to Re^10: memory use array vs ref to array
by choroba
in thread memory use array vs ref to array
by dkhosla1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |