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)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y102 - Python - Fundamentals [328] Making programs easy for any user to start - (2005-05-29)
[633] Copying a reference, or cloning - (2006-03-05)
[956] Python security - trouble with input - (2006-11-30)
[1430] Integer v float - Python - (2007-11-12)
[1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
[1461] Python - input v raw input - (2007-12-06)
[1878] Pascals Triangle in Python and Java - (2008-11-10)
[2368] Python - fresh examples of all the fundamentals - (2009-08-20)
[2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
[2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
[3083] Python - fresh examples from recent courses - (2010-12-11)
[3181] Beware - a=a+b and a+=b are different - Python - (2011-02-23)
[3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
[3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
[3886] Formatting output - why we need to, and first Python example - (2012-10-09)
[3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
[4324] Learning to program - variables and constants - (2014-11-22)
[4442] Mutable v Immuatble objects in Python, and the implication - (2015-02-24)
[4712] A reminder of the key issues to consider in moving from Python 2 to Python 3 - (2016-10-30)
T214 - Tcl/Tk - Other Facilities in Tcl [239] What and why for the epoch - (2005-03-08)
[364] pu daily and p hourly - (2005-06-30)
[366] Error handling in Tcl through catch - (2005-07-02)
[461] Shortened interactive commands - (2005-10-11)
[782] Converting between Hex and Decimal in Tcl - (2006-06-28)
[1277] AgtInvoke - a command to drive Agilent Tcl software extensions - (2007-07-26)
[1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
[1338] Handling Binary data in Tcl (with a note on C) - (2007-09-09)
[2467] Tcl - catching an error before your program crashes - (2009-10-22)
[3287] Exceptions - Tcl style - (2011-05-12)
[3570] Trapping errors in Tcl - the safety net that catch provides - (2012-01-06)
[3583] Expanding a list of parameters in Tcl - {*} and eval - (2012-01-17)
[4207] Exception handling in Tcl - (2013-11-14)
[4523] Catching failed commands and not crashing the program in Tcl - (2015-10-10)
[4525] What does Tcl do if you try to run a command that is not defined? - (2015-10-10)
[4762] Coverage map in Tcl - how many times has each proc been called? - (2017-09-28)
P203 - More about the Perl Environment [743] How to debug a Perl program - (2006-06-04)
[1865] Debugging and Data::Dumper in Perl - (2008-11-02)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
P202 - Perl Fundamentals [184] MTBF of coffee machines - (2005-01-20)
[1312] Some one line Perl tips and techniques - (2007-08-21)
[1726] Hot Courses - Perl - (2008-07-28)
[1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
[1946] Variable Types in Perl - (2008-12-15)
[2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
[3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
[3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
[3329] Perl from basics - (2011-06-20)
[3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
[3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
[3574] Perl functions such as chop change their input parameters - (2012-01-10)
H103 - PHP - Variables, Operators and Expressions [483] Double Dollars in PHP - (2005-11-02)
[2215] If nothing, make it nothing. - (2009-06-02)
[3916] PHP variables - dynamically typed. What does that mean? - (2012-11-08)
[4642] A small teaching program - demonstration of principles only - (2016-02-08)
Some other Articles
Over zealous police activity?Want to be a technical trainer in the UK?Almost everyone losesCottage industry or production line data handling methodsGetting rid of variables after you have finished with themThe Fag Packet Design MethodologyDomain Listing Center and Domain Registry of AmericaPython modules. The distribution, The Cheese Shop and the Vaults of Parnassus.We can offer a room, but we can't operate on a dog