Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

reducing memory usage

by batkins (Chaplain)
on Dec 17, 2002 at 03:11 UTC ( #220431=perlquestion: print w/replies, xml ) Need Help??

batkins has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a program that uses Net::OSCAR and Tk. The program works fine, but it uses 13MB of RAM on Win32. I don't know why this is happening. The script and all its dependent modules are no larger than one or two megabytes. I don't load any files except two 60KB images (with Tk::Photo).

Can anyone explain why the script would use so much memory or explain how I can diagnose its problem? The code is several pages long and I have decided not to include it here.

Replies are listed 'Best First'.
Re: reducing memory usage
by chromatic (Archbishop) on Dec 17, 2002 at 03:44 UTC

    Sorry, it's hard to diagnose this without seeing code. You do have a few options though.

    First, make sure that your variables are scoped properly. Don't keep them around longer than you need them. If you have global hashes or arrays, make sure they're not continually growing. Leave your program running for a while and make sure its memory usage doesn't keep growing. Use the Devel::Size module to check the size of suspicious data structures.

      Here's the strange thing: my memory usage actually decreases over time. It starts at around 13 or 14 meg and then in maybe ten minutes, it drops to 8 meg.
        Are you sure it's total memory used and not just in-core memory? You might be seeing some of your unused pages swapped out to disk. If so, that's usually a sign you've got garbage that's not getting cleaned up, either because of circular data structures, or you (or perl) isn't disposing of something used earlier on.

        If you really are seeing a footprint shrink like that, it's likely you've got something being cached somewhere that gets reused after a while and shrinks on that reuse. (Perl does some aggressive caching of array structures in lexicals, for example)

        Finally, it's possible you're running afoul of an almost-but-not-quite empty memory segment. Perl's asked for a 5 meg chunk at some point, uses it and then frees up all but a few bytes right at the beginning, so it has to hold on to the chunk until the last thing using a part of it dies or goes somewhere else. (And there's not much you can do about that, unfortunately)

Re: reducing memory usage
by pg (Canon) on Dec 17, 2002 at 04:20 UTC
    Two things might quickly increase memory usage:
    • Thread

      If your application is threaded, you would usually end up with using much more memory, as there are resources you would replicate for each thread. Especially for 5.8 thread, there are lots of memory leaks.

      To help measure threaded application, you may want download Benchmark::Thread::Size to help. This module is from CPAN. It reports memory usage for pieces of source code in a threaded application.
    • Circular ref and recursive call

      Recursive calls can increase memory usage very quickly.

      There is also something called circular ref, which causes memory leak in old versions of Perl. In old versions, circular ref can easily escape first phase of garbage collection. A sample would be:
      for (1..10000) { $var = \$var; }
      It seems that each instance of $var would be freed up at the end of each loop, but in old perl, that's not the case. All 10000 $var would exist up to the end of the execution.
Re: reducing memory usage
by elusion (Curate) on Dec 17, 2002 at 03:39 UTC
    Source code size has little to do with the how much memory your program uses. It's possible to write short programs that use large amounts of memory, and vice versa.

    Things that determine how much memory you use are how many variables you have (and how many references too), what types of variables you use, and the stack-depth of your subroutines. Also, the perl interpreter takes up space, and the Tk module and the GUI will take up space as well.

    Perl is known to use fair amounts of memory. If memory or speed are ever real concerns and fine-tuning your algorithm doesn't give you the boost you need, there's always C.

    elusion :

Re: reducing memory usage
by derby (Abbot) on Dec 17, 2002 at 13:48 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://220431]
Approved by graff
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2020-08-14 05:14 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (75 votes). Check out past polls.