I want to remove the entire array. Currently I do @array=();
Does it remove the elements and clears the memory , garbage collected? If not do i need to use Splice?.
-
3I believe `undef @array` is idiomatic, but what you have works fine too. – Nemo Jul 30 '13 at 00:45
-
possible duplicate of [What is the best way to delete a value from an array in Perl?](http://stackoverflow.com/questions/174292/what-is-the-best-way-to-delete-a-value-from-an-array-in-perl) – Alexej Magura Jul 30 '13 at 00:50
-
Doesn't seem to be related to removing a single value given he's trying to undef the entire array. – Joe Jul 30 '13 at 02:03
-
Yes, `@array = ();` is the best way to clear an array. – shawnhcorey Jul 30 '13 at 12:18
5 Answers
It's very very odd that you need to do this. Proper use of
my
means it's very rare that one needs to empty an array.@array = ();
will free the elements and call any destructors as necessary. In other words, the elements will be garbage collected (before the operation even ends) if they are not used elsewhere, as desired.@array = ();
does not free the underlying array buffer. This is a good thing.undef @array;
would force the memory to be deallocated, which will force numerous allocations when you start putting more elements in the array.
So,
If you want free an array because you'll never use it again, limit its scope to where you need it by placing the
my @array;
at the correct location.{ my @array; ... } # Elements of @array garbage collected here.
If you want to empty an array you will reuse, use
@array = ();
.my @array; while (...) { if (...) { push @array, ...; } else { ... use @array ... @array = (); # Elements of @array garbage collected here. } }
Don't use
undef @array;
.
You can use splice
if it's convenient.
say for @array;
@array = ();
could be written as
say for splice(@array);

- 367,544
- 15
- 269
- 518
-
Thanks a lot for the info. i am doing the while loop way that you posted reusing the array and clearing it up undtil the end of loop. I was concerned that if it's not garbage collected and memory is not deallocated i am going to ranout of memory in the while loop. you had mentioned memory is not deallocated when i do @array=(). Does the perl interpreter marks it for garbage collection alone? When I reuse the array does it overwrite on the earlier memory allocated and used? – Arav Jul 30 '13 at 01:20
-
*Does the perl interpreter marks it for garbage collection alone?* The array can't be GC'ed since it's still in use even if empty. *When I reuse the array does it overwrite on the earlier memory allocated and used?* Yes. Changing an array changes the memory uses by that array. – ikegami Jul 30 '13 at 13:38
-
Maybe you think `()` creates an array? It doesn't. That doesn't create anything at all. It's just a placeholder to signify nothing at all. `my @array;` creates an array. `[]` also creates an array (and returns a reference to it). – ikegami Jul 30 '13 at 13:40
@array = ();
is fine, you can also use
undef @array;
Note that this is wrong:
@array = undef;
it will have a value of undef

- 119,891
- 44
- 235
- 294
-
Another option would be `$#array = -1`, which truncates the array. But I wouldn't recommend that. – amon Jul 30 '13 at 00:57
If your goal is to release memory back to the OS you are probably out of luck. If your goal is to make the memory available to your perl program to use again then the other answers are all good.
For some more details check out the following links
http://www.perlmonks.org/?node_id=243025
In Perl, how can I release memory to the operating system?

- 1
- 1

- 66
- 3
All these recipes didn't help me, but I found a new one:
my @tmp_arr = qw();
@array = @tmp_arr;

- 53
- 7