Search code examples
garbage-collectionlanguage-featurescompiler-theory

Why is this GC system bad?


I have thought of a GC which I can't see major flaws with, and I am wondering why it isn't used more prevalently, or why I haven't heard of its use.

The system is:

  • All objects have a 4 byte unsigned int counter attached to them. (Could be 2 bytes, I don't know.
  • Whenever an object is constructed, its counter is started at 1.
  • When an object is sent to a function as a parameter, its counter is incremented.
  • When an object reaches the point where it is no longer used within a function (Could be the very end of scope) its counter is decremented.
  • When the object's counter reaches zero, it is deleted, as at no position in code is it referenced.

Is there a fringe case in which the counter becomes faulty? What are the disadvantages, and the advantages?

Thank you in advance for your assistance.


Solution

  • Its called reference counting. I would suggest reading the wikipedia article as it covers the advantages and disadvantages.