Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Why Thrash Memory?

by chromatic (Archbishop)
on Apr 01, 2000 at 01:03 UTC ( #6615=note: print w/replies, xml ) Need Help??


in reply to Why Thrash Memory?

For the most part, Perl's garbage collection will handle discarding variables and cleaning up for you automatically. As you suggest, if you're mindful of the imminenet death of a variable scope, don't worry about calling undef.

With that in mind, there are a couple of places where you might want to massage things by hand.

  • When you have a circular data structure, like a doubly linked list or some other monstrosity. As the gc works by reference counting, if you have two objects that only point to each other, they'll still stick around. Break the links or undefine one or both objects.
  • When you won't be leaving the scope for a while, but you have an item using lots of memory. One example would be performing some sort of transformation on a large data file. If you must do it all in memory (for speed reasons) but you copy it from one scalar to another, undef the first scalar as soon as possible.
The first is the only one I'd really worry about -- with some thoughtful programming, the second may never come up. I really don't worry about it much, as wise coding tends to minimize memory usage anyway.

Replies are listed 'Best First'.
RE: Re: Why Thrash Memory?
by btrott (Parson) on Apr 01, 2000 at 01:16 UTC
    Re: #1--if you're interested, Perl 5.6 has (experimental) support for weak references, meaning that you can use circular references (as one example) and have the standard garbage collection work for you. A weak reference doesn't increment the reference count of the thing to which it refers.

    You'll need the WeakRef module.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2022-12-03 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?