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?

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

1 Answer 1

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,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.