Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Weak references in Lua - what are they, and why use them?

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:


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)

Back to
Melksham Business Newsreel
Previous and next
Horse's mouth home
Forward to
Once upon a Maundy Thursday
Some other Articles
Programming Standards in Lua
Once upon a Maundy Thursday
Weak references in Lua - what are they, and why use them?
Melksham Business Newsreel
Kicking 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 iptables
Potteries and Staffordshire in the Sunshine
4759 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, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

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., 2022: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3683_Wea ... them-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb