tracegc.lua 681 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. -- track collections
  2. local M = {}
  3. -- import list
  4. local setmetatable, stderr, collectgarbage =
  5. setmetatable, io.stderr, collectgarbage
  6. global none
  7. local active = false
  8. -- each time a table is collected, remark it for finalization on next
  9. -- cycle
  10. local mt = {}
  11. function mt.__gc (o)
  12. stderr:write'.' -- mark progress
  13. if active then
  14. setmetatable(o, mt) -- remark object for finalization
  15. end
  16. end
  17. function M.start ()
  18. if not active then
  19. active = true
  20. setmetatable({}, mt) -- create initial object
  21. end
  22. end
  23. function M.stop ()
  24. if active then
  25. active = false
  26. collectgarbage() -- call finalizer for the last time
  27. end
  28. end
  29. return M