in reply to lazy loading and memory usage
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. }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: lazy loading and memory usage
by Marshall (Canon) on Dec 19, 2010 at 00:53 UTC | |
by BrowserUk (Patriarch) on Dec 19, 2010 at 01:16 UTC | |
by BrowserUk (Patriarch) on Dec 19, 2010 at 04:59 UTC |