in reply to deleteing references

But of course, if you have a variable referencing one of these internal hash-refs then the garbage collector cannot free that memory. How much of the overlying variable structure remains alive, I cannot guess. Other Monks, more conversant with Perl's internals might be able to help you.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: deleteing references
by ikegami (Patriarch) on Jun 09, 2008 at 05:51 UTC

    You mean like

    my $array_ref = []; for(1 .. 2) { my $hash_ref = { foo => foo, bar => bar }; push(@{$array_ref}, $hash_ref); } my $hash_ref = $array_ref->[0]; $array_ref = [];

    You'll be left with the hash ref, the referenced hash, it's keys and values.

    Here's what happens, illustrated!

    Initial State: $array_ref $hash_ref +--------------+ +--------------+ | array ref | | hash ref | +--------------+ +--------------+ | refcount = 1 | | refcount = 1 | +--------------+ +--------------------+ +--------------+ | o=====> | array | | o | +--------------+ +--------------------+ +---------- : -+ | refcount = 1 | : +--------------------+ : | element 0 | : | +--------------+ | : | | hash ref | | : | +--------------+ | : | | refcount = 1 | | V | +--------------+ | +--------------+ | | o========> | hash | | +--------------+ | +--------------+ +--------------------+ | refcount = 2 | | element 1 | +--------------+ | +--------------+ | | keys & vals | | | hash ref | | +--------------+ | +--------------+ | | | refcount = 1 | | | +--------------+ | +--------------+ | | o========> | hash | | +--------------+ | +--------------+ +--------------------+ | refcount = 1 | +--------------+ | keys & vals | +--------------+ ##### After the array reference is overwritten: +--------------+ +--------------+ | array ref | | hash ref | +--------------+ +--------------+ | refcount = 1 | | refcount = 1 | +--------------+ +--------------------+ +--------------+ | o | | array | | o | +---------- : -+ +--------------------+ +---------- : -+ : | refcount = 0 | : : +--------------------+ : V | element 0 | : +--------------+ | +--------------+ | : | array | | | hash ref | | : +--------------+ | +--------------+ | : | refcount = 1 | | | refcount = 1 | | V +--------------+ | +--------------+ | +--------------+ | no elements | | | o========> | hash | +--------------+ | +--------------+ | +--------------+ +--------------------+ | refcount = 2 | | element 1 | +--------------+ | +--------------+ | | keys & vals | | | hash ref | | +--------------+ | +--------------+ | | | refcount = 1 | | | +--------------+ | +--------------+ | | o========> | hash | | +--------------+ | +--------------+ +--------------------+ | refcount = 1 | +--------------+ | keys & vals | +--------------+ ##### Automatically, the original array is freed since its refcount == 0. +--------------+ +--------------+ | array ref | | hash ref | +--------------+ +--------------+ | refcount = 1 | | refcount = 1 | +--------------+ +--------------+ | o | | o | +---------- : -+ +---------- : -+ : : : : V : +--------------+ +--------------+ : | array | | hash ref | : +--------------+ +--------------+ : | refcount = 1 | | refcount = 0 | V +--------------+ +--------------+ +--------------+ | no elements | | o========> | hash | +--------------+ +--------------+ +--------------+ | refcount = 2 | +--------------+ +--------------+ | keys & vals | | hash ref | +--------------+ +--------------+ | refcount = 0 | +--------------+ +--------------+ | o========> | hash | +--------------+ +--------------+ | refcount = 1 | +--------------+ | keys & vals | +--------------+ ##### Automatically, the hash refs with refcount == 0 are freed. +--------------+ +--------------+ | array ref | | hash ref | +--------------+ +--------------+ | refcount = 1 | | refcount = 1 | +--------------+ +--------------+ | o | | o | +---------- : -+ +---------- : -+ : : : : V : +--------------+ : | array | : +--------------+ : | refcount = 1 | V +--------------+ +--------------+ | no elements | | hash | +--------------+ +--------------+ | refcount = 1 | +--------------+ | keys & vals | +--------------+ +--------------+ | hash | +--------------+ | refcount = 0 | +--------------+ | keys & vals | +--------------+ ##### Automatically, the hash with refcount == 0 is freed. +--------------+ +--------------+ | array ref | | hash ref | +--------------+ +--------------+ | refcount = 1 | | refcount = 1 | +--------------+ +--------------+ | o | | o | +---------- : -+ +---------- : -+ : : : : V : +--------------+ : | array | : +--------------+ : | refcount = 1 | V +--------------+ +--------------+ | no elements | | hash | +--------------+ +--------------+ | refcount = 1 | +--------------+ | keys & vals | +--------------+
      Thanks for your replies guys... It's been of much help :]