In programming languages where memory is allocated dynamically (that applies to most of the languages we teach), there has to be a mechanism to gather up memory that is no longer used as the program is running so that it can be re-allocated later within the same program, and that mechanism is usually an automated routine called the "Garbage Collector" that runs from time to time. How and when the garbage collector is scheduled, whether it runs thorougly each time it's called, or has "cheap and cheerful" runs most of the time with occasional more thorough passes, and whether it gathers up long standing variables into a single area to make future collection more efficient, comes down to the language itself, and it's something that only occasionally needs to be considered by the programmer.
How does a garbage collector know WHAT is garbage? Most garbage collectors use a "mark and sweep" mechanism, where they look at each variable name that currently exists in the
symbol table and follow a pointer to the memory address at which the associated data is kept, marking the data at that point in the storage area (usually known as 'on the
heap) as "in use / still referenced". For variables that include deeper data (objects, collections such as lists, etc), this marking process involves descending into the structure of the variable, very much in the way that a
deepcopy or
clone method descends into an object. The process (thus far) described is the
mark. Having done a complete mark, the garbage collector then goes sequentially through the data ares, doing a
sweep during which it builds up an internal table of memory areas that are not marked, and are therefore free for reuse.
You may see the term
weak references used sometimes - in particular (within the languages we teach) in Lua.
A weak reference is one in which the mark cycle of the garbage collector is instructed NOT to follow the mechanism that descends a particular tree or set of trees into an object to retain the data, but rather allows the data that's on that part of the tree to be released and the memory reused,
but only if that data is ONLY accessible via such weak references.
Why on earth do we want to do something like this? Doesn't it break the whole garbage colection mechanism?
Usually it does and you want to use the default
strong references, but there are some circumstances in which you might want to use a weak key in a table (so that the key may be lost) pointing to a strong value. This allows you to name values within a single master table, but have those names lost when the underlying values that's been named is lost.
For example, if I could have a table of places that I'll be giving courses, with each value itself being a table of information about the course, venue, etc ... and another table of days that I'm running the courses, pointing at the same data.
When I run a course and delete it from one table as completed, I can release the memory from the other table too if I use weak references. You can see an example in source code
[here].
Pertinent parts of that code:
1. Setting up the two tables, with weak values in the weekday table:
weekday = {}; b = { __mode = "v" }
setmetatable(weekday, b) -- weekday has weak values
daily = {} -- daily has strong values
daily["Leeds"] = {"Python", "The Wool Warehouse"}
daily["Grimsby"] = {"Perl", "The Old Fish Market"}
weekday["Wednesday"] = daily["Grimbsy"]
weekday["Thursday"] = daily["Leeds"]
Indicating that the course at Grimsby has been completed by removing it from the daily table:
daily["Grimsby"] = nil
Here's the result of running the complete code so far:
-------------
Wednesday Perl The Old Fish Market
Thursday Python The Wool Warehouse
Tuesday PHP Central Conference Center
Monday Lua The Adelphi
-------------
Clearing up (ALL) items that need garbage collection, including anything that's only accessible by weak references:
collectgarbage()
And when we then rerun the code to list the data out, the weak referenced data has been cleared away:
-------------
Thursday Python The Wool Warehouse
Tuesday PHP Central Conference Center
Monday Lua The Adelphi
-------------
Note: Lua objects in the global environment are ignored during garbage collection, and will never be collected, even if they aren't in use. (written 2012-04-04, updated 2012-04-07)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
U198 - New in Lua 5.2 [3686] The goto statement in Lua - (2012-04-06)
[3687] Binary / bitwise operations in Lua with the standard bit32 library - (2012-04-06)
[3690] Changes in Lua 5.2 - Garbage Collection - (2012-04-08)
U107 - Object Orientation - the Lua way [1692] Towards Object Oriented Programming in Lua - (2008-06-30)
[1699] If you are learning Lua, here are some more examples - (2008-07-06)
[1743] First class functions in Lua lead to powerful OO facilities - (2008-08-07)
[1819] Calling base class constructors - (2008-10-03)
[2318] For Lua Programmers AND for Town Planners - (2009-08-02)
[2359] A fresh example - objects the Lua way - (2009-08-13)
[2455] Lua examples - coroutines, error handling, objects, etc - (2009-10-15)
[2701] Is Lua an Object Oriented language? - (2010-04-01)
[2703] Lua Metatables - (2010-04-02)
[2710] __index and __newindex in Lua - metatable methods - (2010-04-05)
[3142] Private and Public - and things between - (2011-01-22)
[3396] Tables as Objects in Lua - a gentle introduction to data driven programming - (2011-08-17)
[3524] Metaclasses (Python) and Metatables (Lua) - (2011-11-17)
[3694] Special __ methods you can use in Lua metatables - (2012-04-12)
[3727] Using Lua tables as objects - (2012-05-11)
[3730] What is a metatable? How do I set one up? How do I use them? Lua - (2012-05-12)
[4117] Is Lua an Object Oriented language? - (2013-06-15)
[4248] Metatables, Metamethods, classes and objects in Lua - (2014-03-18)
[4273] Dot or Colon separator between table name and member in Lua - what is the difference? - (2014-05-06)
[4572] Tables with values and code in Lua - looks like an object? - (2015-11-05)
[4573] Classic style OO code - in Lua - (2015-11-05)
[4753] Lua, Tcl, Python, C and C++ courses - at our Melksham HQ or on your site - forward from July 2017 - (2017-07-02)
Some other Articles
Programming Standards in LuaOnce upon a Maundy ThursdayWeak references in Lua - what are they, and why use them?Melksham Business NewsreelKicking up a stink, the Victorian way?How can I run multiple web servers behind a single IP address? Setting up your Linux system as a firewall using iptablesPotteries and Staffordshire in the Sunshine