Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7408795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:58:30+00:00 2026-05-29T05:58:30+00:00

Setting first and last node object identifier to NULL should result in instant automatic

  • 0

Setting first and last node object identifier to NULL should result in instant automatic garbage collection of all node object inside a linked list because there is no reference to all node objects including first and last node.

 $this->first = NULL
 $this->last = NULL

Do we need to iterate over the complete linked list and unset each of the node object identifier one by one?

My belief is that setting first and last to NULL is sufficient and PHP does garbage collection in background on our behalf.

Please correct me if i am wrong.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:58:30+00:00Added an answer on May 29, 2026 at 5:58 am

    If you are running PHP <5.3.0 and you are using a doubly linked list, none of the nodes will be freed. This is because earlier PHP versions only used reference counting [1], which is unable to recognize cyclic references. Even in a singly linked list, it is possible that it would take ‘n’ passes of the garbage collector to free the entire list, depending on the exact algorithm used (though I see this unlikely).

    To explain further, in a doubly linked list, every node points to the node both before and after it. Consider the ordered nodes A, B, C. This means that A is pointed to by, B, C is pointed to by B, and B is pointed to by A and C. Therefore, their references counts will always be non-zero unless you explicitly unset the nodes yourself.

    With the singly linked list, and the same nodes A, B, C, each node is pointed to by the one previous, except A, which is pointed to by no other node. Therefore, if you remove the reference to A, it will be garbage collected. Then, since B no longer is pointed to, it will be freed, and so-on down the list. However, say the garbage collector visits the list in reverse or random order (rather than the optimal left to right). Then, it may look at B first and conclude A still points to it, and therefore does not need freeing. Then the GC frees A and is done. Although, it is more likely that the GC algorithm continually collects memory with zero reference counts until there is nothing more to collect, which would avoid this problem.

    Thankfully, as of PHP 5.3.0, we don’t have to worry about the cyclic references [2]. This algorithm works by constructing a tree from the root memory node. Anything not included in the final tree must have been orphaned (and consequently is only being kept alive because of a cyclic reference), and therefore can be freed. So yes, as long as nothing else in your program points to any node in your list, the entire list will be freed by removing the references to the start and end.

    Note that the algorithm to free orphaned cyclic references is more expensive than the simple reference counting. Explicit freeing code may or may not be beneficial. Careful benchmarking will have to be done to find this.

    • [1] http://www.php.net/manual/en/features.gc.refcounting-basics.php

    • [2] http://www.php.net/manual/en/features.gc.collecting-cycles.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have First/Last/Previous/Next buttons that change the selected child node of a TreeViewItem. Setting
I am setting up a very small MySQL database that stores, first name, last
I'm setting up my first site using the asp authentication provider and using the
I'm setting up my first development Linux box - a netbook actually. I'm quite
I'm setting up my first symfony project, and am having trouble with the schema.
I am setting up an Oracle connection for NHibernate for the first time. I
I'm setting up a public site and the first thing on my mind is
First time poster here. A quick question about setting up a loop here. I
I've realised for the first time a couple of weeks ago that when setting
A random class definition: class ABC: x = 6 Setting some values, first for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.