Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: memory usage/leaks

by thraxil (Prior)
on Dec 10, 2001 at 00:02 UTC ( [id://130555]=note: print w/replies, xml ) Need Help??


in reply to memory usage/leaks

perl doesn't have "real" garbage collection. instead it uses refcounts. the idea is pretty simple. every variable/array/hash has a counter that is used to keep track of how many references point to it. when this reaches 0, the memory is freed. not as robust as real garbage collection but very efficient.

most memory leaks i've seen have been from arrays just getting constantly added to and not reset. if you have a loop somewhere in your code that does $i++ and then $stuff$i = "something else" take a close look and see if the array ever goes out of scope or is explicitly reset; if not, there's your problem. the quick and dirty refcount method is efficient but has a problem with self-referential data structures. if you have a hash with one of the elements being a reference to the hash itself, the refcount will never hit 0 by itself and the memory won't be freed unless you delete it manually. but i've never found self-referential data structures to be very common for basic apps so i'd look for ever-growing arrays first.

anders pearson

Replies are listed 'Best First'.
Re: Re: memory usage/leaks
by dws (Chancellor) on Dec 10, 2001 at 02:33 UTC
    <ahem>
    Perl most certainly does have real garbage collection. Reference counting is one of several GC techniques.
    </ahem>

    One of the shortcomings of reference counting as a memory management technique is the problem of circular references. If you manage to get a Perl data structure to point to itself, all elements will continue to have non-zero reference counts even if nobody else points to the data structure. The structure hangs around in memory until the program terminates.

    There are two other likely causes of memory leaks in Perl programs. First, there are known memory leaks in some version of Perl with eval.

    A second source of leaks are with modules that use XS modules, which are linked into the application at runtime. If C code in the XS module doesn't correctly clean up after itself, external resources (e.g., resources not visible directly to Perl) can leak. Tk uses XS. You might be running into a leak in whichever version you're using.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://130555]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-23 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found