develooper Front page | perl.perlfaq.workers | Postings from January 2005

perlfaq3: How can I free an array or hash so my program shrinks?

From:
_brian_d_foy
Date:
January 26, 2005 12:09
Subject:
perlfaq3: How can I free an array or hash so my program shrinks?
Message ID:
260120051408547099%comdog@panix.com
* Michael Carmen rewrote this answer to clarify some of the memory
management issues.  I usually don't pay attention to this level of
Perl and I'm not an OS guru, so an internals-type person may want
to verify the change.


Index: perlfaq3.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq3.pod,v
retrieving revision 1.44
diff -u -d -r1.44 perlfaq3.pod
--- perlfaq3.pod  21 Jan 2005 12:08:04 -0000 1.44
+++ perlfaq3.pod  26 Jan 2005 20:06:53 -0000
@@ -649,23 +649,25 @@
 
 =head2 How can I free an array or hash so my program shrinks?
 
-You usually can't. On most operating systems, memory
-allocated to a program can never be returned to the system.
-That's why long-running programs sometimes re-exec
-themselves. Some operating systems (notably, systems that
-use mmap(2) for allocating large chunks of memory) can
-reclaim memory that is no longer used, but on such systems,
-perl must be configured and compiled to use the OS's malloc,
-not perl's.
+(contributed by Michael Carman)
+
+You usually can't. Memory allocated to lexicals (i.e. my() variables)
+cannot be reclaimed or reused even if they go out of scope. It is
+reserved in case the variables come back into scope. Memory allocated
+to global variables can be reused (within your program) by using
+undef()ing and/or delete().
+
+On most operating systems, memory allocated to a program can never be
+returned to the system. That's why long-running programs sometimes re-
+exec themselves. Some operating systems (notably, systems that use
+mmap(2) for allocating large chunks of memory) can reclaim memory that
+is no longer used, but on such systems, perl must be configured and
+compiled to use the OS's malloc, not perl's.
 
-However, judicious use of my() on your variables will help make sure
-that they go out of scope so that Perl can free up that space for
-use in other parts of your program.  A global variable, of course,
never
-goes out of scope, so you can't get its space automatically reclaimed,
-although undef()ing and/or delete()ing it will achieve the same effect.
 In general, memory allocation and de-allocation isn't something you can
-or should be worrying about much in Perl, but even this capability
-(preallocation of data types) is in the works.
+or should be worrying about much in Perl.
+
+See also "How can I make my Perl program take less memory?"
 
 =head2 How can I make my CGI script more efficient?

-- 
brian d foy, comdog@panix.com



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About