1

I have a Perl script, fetch url like http://1.1.1.1/1.jpg from MySQL using DBI, and download this jpg file using LWP::Simple. It's a infinite loop.

while (1) {
    my $url=&fetch_url_from_mysql;
    if ($url){        
        &download_jpg($url);
    } else {
        sleep 1;
    }
}

Plain simple. I suppose the memory usage would be stay in certain amount. But after one month of continuous running of this script. The memory usage is 7.5G!

How can I profile it?

everbox
  • 1,189
  • 3
  • 16
  • 25
  • Is fetch_url_from_mysql opening a new connection every time, and not closing it? – xxpor Mar 30 '12 at 05:56
  • Yes. I call `DBI::connect` before `while loop`, in `fetch_url_from_mysql` is `prepare,execute,fetch and return $url` – everbox Mar 30 '12 at 06:05
  • 6
    In washing your code down to a clean example you washed away the parts that actually use memory. – DavidO Mar 30 '12 at 06:12

1 Answers1

1

For profiling, set an explitict exit. Create a counter, and exit from your program if your iteration is equal or bigger than this.

For profiling, use NYTprof:

perl -d:NYTProf script.pl nytprofhtml

But you are dealing with a memory leak here.

Read this to find a memory leak: How can I find memory leaks in long-running Perl program?

Most probably you have a variable that will never be freed. Perl frees memory if a variable goes out of scope, but one of your variables never goes out of scope.

Use $variable=undef to free up the memory.

If you port your whole script maybe we could find a leak in it.

regards,

Community
  • 1
  • 1
user1126070
  • 5,059
  • 1
  • 16
  • 15