1

Imagine I have a referece that points to an array that contains many anonimous arrays. Ex:

my @main_array = ( [1,2,3], [3,4,5], ['a','b','c'] );
my $reference = \@main_array

If later on I'm done using the data from that array and I only have a reference to it, what is the best method to delete that array and free the memory? I usually do the following to free the memory used by data in a simple array:

undef @array

but because I only have a reference to it I thought about doing this

undef @{$reference}

If I do that, wouldn't I just be deleting the references to the anonymous arrays stored in the array (main_array) and not the actual content of the anonymous arrays?

I guess my question can be simplify as this: Does deleting a reference makes Perl free the memory used by the array, hash or scalar referred by the reference?

Thank you

simbabque
  • 53,749
  • 8
  • 73
  • 136
JohnnyLoo
  • 877
  • 1
  • 11
  • 22
  • 1
    Related: [How to free memory in Perl?](http://stackoverflow.com/q/8924142), [In Perl, how can I release memory to the operating system?](http://stackoverflow.com/q/1242832), [How to free memory in the Perl](http://stackoverflow.com/q/27565121), [How can I remove perl object from memory](http://stackoverflow.com/q/40574671), [Why does Perl not garbage collect memory when a large array is deallocated?](http://stackoverflow.com/q/14159258) – ThisSuitIsBlackNot Mar 22 '17 at 14:55
  • 2
    *"I usually do the following to free the memory used by data in a simple array: `undef @array`"* Please don't do this. It is generally pointless and unnecessary. – Borodin Mar 22 '17 at 14:59

2 Answers2

7

Yes, undef @{$reference} (or undef @$reference) will do what undef @array did. It will free almost all memory used by the array to be reusable by the program.

But there is very rarely any good reason to do this. When your lexical $reference goes out of scope, the same thing will happen. Explicitly calling undef on it first will just make your code minutely slower.

ysth
  • 96,171
  • 6
  • 121
  • 214
  • I should've mentioned that my array have a very large number of items and it lives for the entire time that the script is running. This is because the script needs to collect data from different sources and all that data needs to be processed at the very end of the script, so I have to keep the data in memory. There are some sections when I have to create sub arrays and those sub arrays are the ones I try to delete to free their memory and avoid having duplicate data.. Thank you – JohnnyLoo Mar 22 '17 at 15:15
  • 6
    @JohnnyLoo: If you have temporary data then you should limit its lifetime by declaring it with `my` and limiting its scope so that it is discarded automatically. Explicit use of `undef` is usually a sign of poor design. If your data is so big then you may be better using an in-memory SQLite database. – Borodin Mar 22 '17 at 15:27
1

If later on I'm done using the data from that array and I only have a reference to it, what is the best method to delete that array and free the memory?

Ideally, just let $reference go out of scope. Otherwise, you can use $reference = undef;.

ikegami
  • 367,544
  • 15
  • 269
  • 518