0

I'm having a hard time finding clear information on PHP's garbage collection as it relates to objects referencing other objects. Specifically, I'm trying to understand what happens when I have a chain of objects that reference each other, and I destroy or unset() the first object in the chain. Will PHP know to GC the remaining objects in the chain or do I have to destroy them all individually to prevent a memory leak?

For example, Say I have a class BinaryTree with a variable rootNode and a Node class that extends BinaryTree and has 3 variables, a parent Node, a leftChild Node, and a rightChild Node. The rootNode will be the Node with a parent that is null, but every other Node created will reference either the rootNode or a subsequent child Node before it as the parent and any Nodes after it as either leftChild or rightChild. The rootNode, therefore, is the only way to reference any of the child nodes.

If I unset() the rootNode, will all other Nodes remain in memory? Would I need to iterate through the entire tree unsetting each Node individually to prevent them from just existing without a way to reference them anymore, or will PHP know that those objects can no longer be referenced and GC them?

So far what I'm reading leads me to believe I would need to iterate the entire list of objects to destroy them individually, but I'm wondering if anyone has any experience with this and can offer some clarity. TIA.

3
  • So far what I'm reading leads me to believe I would need to iterate the entire list of objects - would be interesting to see what you have been reading. Commented Feb 11, 2023 at 10:53
  • As long as a variable holds a reference, it will be kept in memory and the GC can not remove it. 3v4l.org/UsTpq Unsetting the root holding objects not stored in extra variables has no reference and can be removed by the GC. 3v4l.org/tHBlG Commented Feb 11, 2023 at 10:57
  • @NigelRen I guess I should have worded it "what I'm understanding." I read the page that has the answer and it just didn't seem to click in my brain. I guess I just needed someone to explain it to me like I'm 5. Makes perfect sense in hindsight. Commented Feb 12, 2023 at 11:10

1 Answer 1

4

The PHP manual has a section on this:

...a zval container also has an internal reference counting mechanism to optimize memory usage. This second piece of additional information, called "refcount", contains how many variable names (also called symbols) point to this one zval container.

[...]

...when the "refcount" reaches zero, the variable container is removed from memory

[...]

...unsetting a variable removes the symbol, and the reference count of the variable container it points to is decreased by one.

And so you get a cascade effect. Initially all the nodes of your binary tree node have a reference count of 1, but by unsetting the rootNode, the reference count of the root object becomes 0, and the memory used for the root is freed. This involves unsetting the references it has to other objects, whose reference counters are therefore also reduced, ...etc.

So, no, you do not have to unlink the nodes or destroy them yourself. The reference count mechanism will trigger a cascade of memory being freed.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.