Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
This is a question about proper memory management. I want to make sure that I am not creating a memory leak and that I am using memory as efficiently as possible.
The task that I am working on is one where I need to do a lot of similar things in different scripts. To facilitate this I want to make a file with all the key tasks in it as subroutines and call them when needed from the other scripts. These tasks will use objects from various Perl packages. Since I will only need maybe 10 subroutines, I don't really want to do anything more complex than a file with a collection of subroutines in the same folder as the folder that my scripts are in. I want to keep it simple.
What I have done so far is to create a subrouting file called mynetutils.pl and put the following code in it.
use WWW::Mechanize; sub getfile { my($url, $file) = @_; my $mech = WWW::Mechanize->new(); $mech->get("$url", ':content_file' => "$file"); #delete $mech and test to see if it is deleted #$mech->die; #$mech->delete; #$mech->giberish; $mech->get("$url", ':content_file' => ".\\afterdel.jpg"); } 1;
The code to call it is:
require ".\\mynetutils.pl"; #This downloads a nice pic of a sunset for testing the subroutine &getfile("http://artwall.us/scenic/tropical/images/sunset.jpg",".\\tes +t.jpg");
If you have not noticed yet, this is written for windows and it does work. My concern is that creating the $mech object each time the subroutine is called will will cause a build up of $mech objects in memory. Possible soutions that I have considered are as followes:
1. delete the object after use. The code that I used to test this is in the subroutine file. The attempts to delete the file are all commented out. If you uncomment them (one at a time) and run the code you will see that the second download does not happen. However when I look at the command prompt there is an error:
C:\>test.pl Can't locate object method "delete" via package "WWW::Mechanize" at .\ +mynetutils .pl line 11.
I get the same message and no second download for all three lines when they are uncommented. Since gibberish should not be a defined subroutine, I don't think I am actually deleting the object with either die or delete. So is there a way to kill the object?
2. My second thought is that if I can create a single $mech object and reuse it throughout the execution of the primary script, I could be sure of not cluttering up memory and save time by not recreating the object each run. If I place the my $mech...(declaration and construction line) outside the subroutine, will the object be created and available for all the subroutines to use? Thereby, saving time and memory.
So there is two questions, one last question is can I return this $mech object back to the primary script and manipulate it there and not mess up the memory management. Basically have a reference to the object from the subroutine file and from the primary script at the same time. Sorry if reference is the wrong word here I learned OOP using Java and in Java this would work. I could create the object once in the subroutine folder when the script is first run, when a subroutine is called, return the object after 'loading' it with a download, manipulate it in the primary script, and once finished with that object, call a subroutine to 'reload' the object with another download. So my last question is, "Is this possible"?
Thanks for you time in advance, Chris
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Memory Managment with Subroutines and Objects
by jethro (Monsignor) on Dec 28, 2010 at 19:49 UTC | |
by Anonymous Monk on Dec 28, 2010 at 23:36 UTC | |
|
Re: Memory Managment with Subroutines and Objects
by Corion (Patriarch) on Dec 28, 2010 at 18:11 UTC | |
by Anonymous Monk on Dec 28, 2010 at 23:28 UTC | |
by Corion (Patriarch) on Dec 28, 2010 at 23:38 UTC | |
by Anonymous Monk on Dec 29, 2010 at 02:39 UTC | |
by Corion (Patriarch) on Dec 29, 2010 at 08:14 UTC | |
|
Re: Memory Managment with Subroutines and Objects
by locked_user sundialsvc4 (Abbot) on Dec 29, 2010 at 13:30 UTC | |
by locked_user sundialsvc4 (Abbot) on Dec 30, 2010 at 14:08 UTC |