Nice Post. Would the following achieve the same thing without needing "undef"?
# Iterate through all of the genes on a clone foreach my $gene ( @{ $first_clone->get_all_Genes() } ) { my $temp_obj_handle = $gene; print $temp_obj_handle->stable_id(), "\n"; }
Update: Again kudos to BrowserUk.

Although $temp_obj_handle gets re-cycled and it does "go out of scope", it points to the same place as $gene within the loop and $gene does not go "out of scope". Therefore, the $gene reference count is not, "at the end of the day" decremented. End result: The above code does not save memory within the foreach() loop, although the code from BrowserUk does.

At the end of day, does this memory optimization within a for() loop matter at all? I think that it usually does not.

Perl is excellent about re-cycling memory that it has used before. A typical Perl program reaches a max memory usage and then just stays there (provided of course that you don't have memory leaks :-)). There are not any "garbage collection" calls like in Java or C#. In short, this is fine:

foreach my $gene ( @{ $first_clone->get_all_Genes() } ) { print $gene->stable_id(), "\n"; }
I would not worry until there are thousands of new objects being created. A few hundred? => no.

Update again:

Well as with many things in programming, judgment is required. 5 objects might very well consume 1,483 MB of memory. I have no idea of how much memory that a particular $gene->stable_id() will consume... it might be a lot. On other hand, it might not "be much". This is very application specific. I think this thread has pointed out how the memory allocation works and the OP can decide what to do in a particular situation. I personally would use the most simple loop unless there is a reason not to do so. In other words, make things more complicated when it is necessary to do so. "necessary" is application specific.


In reply to Re^2: lazy loading and memory usage by Marshall
in thread lazy loading and memory usage by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.