1

I think that this is my first post, i'm not sure.

I've got three files: queue.class.php, node.class.php and test.php, look like this:

queue.class.php:

<?php

include('node.class.php');

class queue
{
    public $head;
    public $tail;
    private $size;

    //some methods (e.g __construct)

    public function create_node($value){
        $data = new Node($value);
        return $data;
    }

    public function push($value){
        //here I call create_node
    }

    //some more methods..

    public function __destruct(){
        if(!$this->queue_is_empty())
        {
            $node = $this->head->next;
            while(!empty($node))
            {
                unset($this->head) // Here, doesn't free the memory.
                $this->head = $node;
                $node = $this->head->next;
            }
            unset($node);
        }
        unset($this->head, $this->tail, $this->size);
    }
}

?>

node.class.php:

<?php

class Node
{
    public $next;
    public $previous;
    private $data;

    //some methods..

    public function __destruct(){
        unset($this->next, $this->previous, $this->data);
    }

}

?>

And in test.php supose that I have the following:

<?php

include('queue.class.php');

$cola = new queue();

for($i = 0; $i < 1000; $i++){
    $cola->push($i);
}

unset($cola) //free all the memory;

?>

My doubt is the next: Why when I call unset(node) inside the "while" of __destruct (in queue.class.php) doesn't free the memory taken by the node? But if I create a new method inside node.class.php call it, e.g: delete_node() (with the same code that __destruct) and replace every unset(node) with: node->delete_node() in queue.class.php yes, it works.

I've seen similar themes but I don't understand completely

I hope that understand my explanation and anyone can help me. Thanks

PD: I'm new, i don't know how to do markdown

singe batteur
  • 401
  • 2
  • 14
Adoc
  • 63
  • 1
  • 6
  • Possible duplicate of [What's better at freeing memory with PHP: unset() or $var = null](http://stackoverflow.com/questions/584960/whats-better-at-freeing-memory-with-php-unset-or-var-null) – Charlotte Dunois Aug 13 '16 at 16:36
  • Not exactly what I was looking but helped me alot, thanks – Adoc Aug 13 '16 at 18:48

0 Answers0