in reply to memory "release" with $#=-1

$#a=-1 will free up all the elements of @a, allowing their memory to be reused. In addition, undef @a frees up the array's AvARRAY structure, which uses typically (4 x maximum array index ever used) bytes. Neither will free up the actual array itself; you need to ensure the variable goes out of scope for that.

Dave.

Replies are listed 'Best First'.
Re^2: memory "release" with $#=-1
by ISAI student (Scribe) on Jun 28, 2005 at 11:00 UTC
    I am sorry for being obtuse. Will it improve the memory usage or not, in the same scope?
      my $a = []; $a->[$_] = $_ for 0..999; $#$a = 1; # frees up approx 16000 bytes undef @$a; # frees a further 4000 bytes undef $a; # frees a further 52 bytes
      Note that this frees memory to the perl interpter, not to the OS. ie perl can reuse that memory when creating further data, but other processes can't

      update: to clarify: just doing the undef $a on its own would free up all 20052 bytes too.

      Dave.

        OK. That's great, I'll use it to free up memory. Thanks for all your help. -ISAI student
        One note though, if you create the array peicemail over time, then you may have smaller portions of memory allocated for the array that are not contiguous. In that case freeing the array and making a new array may not use the freed memory if the new array has larger malloc needs then the freed memory has contiguous memory segments.


        -Waswas