I've seen quite often that Perl was reusing reference addresses after garbage collection had them released before.

I was curious as to how often this address reusing happens:

#!/bin/bash for i in {1..15}; do perl -Mstrict -wle' use strict; use warnings; my %addr; my $count = 0; my $tries = 2**$ARGV[0]; for my $try (1..$tries) { my %h; for (reverse 1..1000) { $h{$_} = { value => $_}; $count++; } $addr{$_}++ for values %h; } my $reused = scalar grep $addr{$_}>1, keys %addr; my $addrs = scalar keys %addr; print "Address of reference reused $reused times, all addresses $addrs +, in $count tries";' $i done
Address of reference reused 1 times, all addresses 1999, in 2000 tries Address of reference reused 1001 times, all addresses 2999, in 4000 tr +ies Address of reference reused 2995 times, all addresses 3007, in 8000 tr +ies Address of reference reused 3011 times, all addresses 3023, in 16000 t +ries Address of reference reused 3043 times, all addresses 3055, in 32000 t +ries Address of reference reused 3107 times, all addresses 3119, in 64000 t +ries Address of reference reused 3235 times, all addresses 3247, in 128000 +tries Address of reference reused 3491 times, all addresses 3503, in 256000 +tries Address of reference reused 4003 times, all addresses 4015, in 512000 +tries Address of reference reused 5027 times, all addresses 5039, in 1024000 + tries Address of reference reused 7075 times, all addresses 7087, in 2048000 + tries Address of reference reused 11171 times, all addresses 11183, in 40960 +00 tries Address of reference reused 19363 times, all addresses 19375, in 81920 +00 tries Address of reference reused 35747 times, all addresses 35759, in 16384 +000 tries Address of reference reused 68515 times, all addresses 68527, in 32768 +000 tries

These numbers seem to be stable on my machine across multiple runs.

The bottom line is that address reusing happens all the time.


In reply to Re^2: Hashes do preserve insertion order after all by kikuchiyo
in thread Hashes do preserve insertion order after all by kikuchiyo

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.