Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Is it a good way to read in arr_ref? (Updated)

by Athanasius (Archbishop)
on Jan 31, 2016 at 07:51 UTC ( [id://1154110]=note: print w/replies, xml ) Need Help??


in reply to Is it a good way to read in arr_ref?

Hello jakito, and welcome to the Monastery!

First, your code has an error: in sub read_file () the prototype specifies no arguments, but the function is then twice called with one argument. In general, prototypes should be avoided anyway unless you have a good reason to use them. See Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen for the gory details.

Now to the main issue: why do say that the contents of $file_data will not be released until the program ends? That variable is reference-counted like any other, so when its reference count falls to zero it becomes eligible for garbage collection. It may not actually be garbage collected until a later time, but that applies to any variable, including $file_data->[0]. So, why do you expect the second approach to be more memory-efficient than the first?

From your reference to Devel::Peek it seems you’ve already conducted further experiments not outlined in your post. Please supply details, and specify the platform (OS and Perl version) you’re working on. So far, my very limited experiments (using Strawberry Perl 5.22.1 on Windows 8.1 64-bit1) have not revealed any difference in memory usage between the two approaches.

1Substituting tasklist for top.

Update (Feb 1, 2016): I see you’ve made significant changes to your original post. The reference to Devel::Peek has gone, the system call in sub show_size now includes an awk command, and output has been added. While this new information is useful, you should clearly mark the updates as such, so that other monks coming to the thread won’t be confused by the fact that my answer relates to a question which has since changed. Or just add the new information in a new post.

I still don’t understand why you think $file_data is immune from garbage collection?

Cheers,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1154110]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-26 00:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found