0xdeadbeef
0xdeadbeef

Reputation: 4140

Does allocated memory for PHP Anonymous objects get freed?

For example:

I have a object created with $object = new stdClass(); it's passed onto a function.

My question is whether the memory allocated for this object is ever freed, if so when does it get freed.

Also how will one check for these changes to memory.

I manually unset($object); right now, just to be safe.

Upvotes: 0

Views: 258

Answers (2)

Scott M.
Scott M.

Reputation: 7347

php is an episodic event. every time the web page is loaded, the code runs from scratch (on a normal setup, I'm leaving out things like memcached and facebook's solution). This means that the memory gets allocated, the page gets sent, then the memory is freed. unsetting an object does basically nothing for you because the program will end very soon anyway.

Upvotes: 2

kayahr
kayahr

Reputation: 22020

Objects are destroyed on the end of the script execution. You can try it out:

<?php

class Test  
{
    function __construct()
    {
        echo "Construct"; 
    }

    function __destruct()
    {
        echo "Destruct";
    }
}

$test = new Test();

?>

When calling this script in the browser you get the Construct and Destruct output which proves that the object was successfully destroyed on the end of script execution.

Upvotes: 2

Related Questions