2a8e Getting rid of variables after you have finished with them
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Getting rid of variables after you have finished with them

If you've finished with a variable in your program, you can usually just leave it "in situe" and it will be destroyed and the memory it occupied will be released when you exit from your program. In many languages, variables within named blocks of code have an even shorter "shelf life" - by default, a PHP or Python variable used within a function will be lost once you leave the function, and in Perl any variable described as a "my" variable will be lost at the end of the block in which it is declared. There *are* exceptions - if a variable reference is copied (Perl or Python) and / or if a PHP variable is declared as being static to name just a couple of examples.

But what if I've got a long-running program and I want to release the memory that a variable occupies once I've finished with it? Most languages support TWO ways of getting rid of variables:

a) You can set the variable to being empty. In this case, the variable name still exists but it contains no data, so what might previously have been a memory hog is shrunk to virtually nothing

b) You can actually remove the variable name itself from the symbol table, so that the very name ceases to exist.

If you would a metaphor - if you consider that a variable is like a cage that contains animals, then setting the variable to empty is like getting rid of the animals, and removing the variable is like destroying the whole structure of the cage.

Python

To set a variable to empty, assign None to it:
queue = None

To destroy a variable, use the del keyword:
del queue

Perl

To set a variable to empty, undef it:
undef @queue;

To destroy a variable, you may be able to use delete (but this applies only to hash members and as from Perl 5.6 to list members too:
delete $queue{"X72"};

PHP

To set a variable to empty, assign 'nothing' to it, for example:
$?queue = NULL;
or $queue = "";

To destroy a variable, use unset:
unset($queue);

Tcl, Tcl/Tk, Expect

To set a variable to empty, assign an empty string to it:
set queue ""

To destroy a variable, use unset:
unset queue

Please note that under most operating systems, memory released by emptying or destroying variables will be available for re-use by the current process, but will NOT be released back to the operating system until the process terminates. This applies to all languages, not just the ones I've highlighted in this article, as most operating systems aren't built to handle memory returned to them by shrinking processes.
(written 2006-06-06, updated 2006-06-09) 302a

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H103 - PHP - Variables, Operators and Expressions
  [3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
  [3916] PHP variables - dynamically typed. What does that mean? - (2012-11-08)
  [3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
  [2215] If nothing, make it nothing. - (2009-06-02)
  [483] Double Dollars in PHP - (2005-11-02)

P202 - Perl Fundamentals
  [3574] Perl functions such as chop change their input parameters - (2012-01-10)
  [3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
  [3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
  [3329] Perl from basics - (2011-06-20)
  [3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
  [3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
  [2876] Different perl examples - some corners I rarely explore - (2010-07-18)
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
  [2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
  [1946] Variable Types in Perl - (2008-12-15)
  [1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
  [1726] Hot Courses - Perl - (2008-07-28)
  [1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
  [1312] Some one line Perl tips and techniques - (2007-08-21)
  [184] MTBF of coffee machines - (2005-01-20)

P203 - More about the Perl Environment
  [1865] Debugging and Data::Dumper in Perl - (2008-11-02)
  [743] How to debug a Perl program - (2006-06-04)
  [328] Making programs easy for any user to start - (2005-05-29)

T214 - Tcl/Tk - Other Facilities in Tcl
  [3583] Expanding a list of parameters in Tcl - {*} and eval - (2012-01-17)
  [3570] Trapping errors in Tcl - the safety net that catch provides - (2012-01-06)
  [3287] Exceptions - Tcl style - (2011-05-12)
  [2467] Tcl - catching an error before your program crashes - (2009-10-22)
  [1338] Handling Binary data in Tcl (with a note on C) - (2007-09-09)
  [1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
  [1277] AgtInvoke - a command to drive Agilent Tcl software extensions - (2007-07-26)
  [782] Converting between Hex and Decimal in Tcl - (2006-06-28)
  [461] Shortened interactive commands - (2005-10-11)
  [366] Error handling in Tcl through catch - (2005-07-02)
  [364] pu daily and p hourly - (2005-06-30)
  [239] What and why for the epoch - (2005-03-08)

Y102 - Python - Fundamentals
  [3886] Formatting output - why we need to, and first Python example - (2012-10-09)
  [3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
  [3181] Beware - a=a+b and a+=b are different - Python - (2011-02-23)
  [3083] Python - fresh examples from recent courses - (2010-12-11)
  [2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
  [2368] Python - fresh examples of all the fundamentals - (2009-08-20)
  [1878] Pascals Triangle in Python and Java - (2008-11-10)
  [1461] Python - input v raw input - (2007-12-06)
  [1430] Integer v float - Python - (2007-11-12)
  [956] Python security - trouble with input - (2006-11-30)
  [633] Copying a reference, or cloning - (2006-03-05)


Back to
The Fag Packet Design Methodology
Previous and next
or
Horse's mouth home
Forward to
Cottage industry or production line data handling methods
Some other Articles
Over zealous police activity?
Want to be a technical trainer in the UK?
Almost everyone loses
Cottage industry or production line data handling methods
Getting rid of variables after you have finished with them
The Fag Packet Design Methodology
Domain Listing Center and Domain Registry of America
Python modules. The distribution, The Cheese Shop and the Vaults of Parnassus.
We can offer a room, but we can't operate on a dog
4106 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83 at 50 posts per page

4337
This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2013: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/748_Gett ... -them.html • PAGE BUILT: Mon May 27 06:13:46 2013 • BUILD SYSTEM: wizard
0