That suggests that when get_all_Genes() is called, the values in the returned array reference are handles to as-yet-unpopulated objects. That is, the anonymous array returned is filled with handles to objects that are, at the point of return, empty. They do not get populated until you call the first method call upon them.

Therefore, if you iterate that array in a for loop, each one gets populated when you call its stable_id() method. So by the end of the for loop, all the genes will have been populated, and as their object handles are still held in the array, all the memory required by all of them will still be in-use.

Conversely, in the while loop, each gene is again populated by the call to the stable_id() method, but because the object handle was shifted off the array, when the loop iterates, that object handle will go out of scope, thereby allowing it and all the memory required to holds its contents to be released.

With the while-shift method, only one gene from the array is ever populated at any given time, so the total memory usage is reduced.

You could achieve the same thing--arguably more clearly--but using undef in conjunction with the for loop:

# Iterate through all of the genes on a clone foreach my $gene ( @{ $first_clone->get_all_Genes() } ) { print $gene->stable_id(), "\n"; undef $gene; ## Free the gene object and the memory it uses. }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: lazy loading and memory usage by BrowserUk
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.