memory leaks in perl

Rick Delaney rick-h4KjNK7Mzas at public.gmane.org
Sun May 2 03:34:30 UTC 2004


On Sat, May 01, 2004 at 09:58:27PM -0400, Stewart C. Russell wrote:
> Rick Delaney wrote:
> >
> >undef($scalar) frees the memory used by $scalar.
> 
> Yes, but UTFM ;-)

Unread?  IDKWTHTSTM. :-b

> >$scalar = undef will leave $scalar using lots of memory if it was
> >previously using lots of memory.
> 
> True, but that memory won't be made available to the system, only to the 
> program's heap. Try this with top running in another window:
> 
> $ perl -wle '$|=1; $a=undef; print "I am small"; sleep 10; $a = "foo" x 
> 10000000; print "I am large"; sleep 10; $a=undef; print "I am small 
> again"; sleep 10;'
> 
> On my machine, my free memory drops by about 58MB when it prints "I am 
> large", and doesn't return that until the interpreter exits.

Of course, because you are still doing 

   $a = undef;

instead of 

   undef $a;

so you are not even returning memory to the heap.

Try it again, but add

   print "undef $a; Now I am truly small";<>";

to the end.  On many systems this may still make no difference to the
free system memory, but this is Linux so you should get a fair chunk of
the memory back.  Cool, eh?

> I don't think I've ever deleted a hash entry to save memory. Perl's 

Me neither.

> memory manglement is best left to its own devices, unless you really 
> know that you really know what you're doing.

Agreed.

-- 
Rick Delaney
rick-h4KjNK7Mzas at public.gmane.org
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list