Programming Search⌘K

See Garbage collection run

Garbage collection frees the memory of objects that the program can no longer reach. The program allocates objects but never frees them, and the runtime finds the dead ones. Use the buttons or the keys: Space plays and pauses, the arrow keys step, Home and End jump.

  1. DROP(x) ▷ reference counting: one reference to x goes away
  2. 1count(x) ← count(x) − 1
  3. 2if count(x) = 0 then
  4. 3for each reference from x to an object y do DROP(y)
  5. 4free x
  6. COLLECT() ▷ tracing: mark from the roots, then sweep
  7. 5colour each object of the heap white
  8. 6for each root r do
  9. 7if r refers to a white object y then colour y grey
  10. 8while some object is grey do
  11. 9take a grey object x
  12. 10for each reference from x to an object y do
  13. 11if y is white then colour y grey
  14. 12colour x black ▷ no black object refers to a white object
  15. 13for each object x of the heap do ▷ the sweep
  16. 14if x is white then free x
  17. 15else colour x white ▷ ready for the next collection
0 / 0
Use your own input

Type the input as JSON, in the same form as a preset.

Open the full player

Read the full page about Garbage collection. To put this player on your site, use <iframe src="https://programming.tamnd.com/embed/garbage-collection/" width="100%" height="520"></iframe>.