FAQ
Hello,
I have a case where an object has a few variables, a couple of which
are objects which hold references back to this object.

So very simply something like

class A {
var B
}

class B {
var A
}

When doing something like so

$obj = new A(); in a loop the object's never released from memory
because of the internal references it holds. __destruct is not called
until the end of script execution etc. Now normally in a web request
situation this isn't a big deal. However I'm using it in a batch and
loop over 22 000 records. Since re-assigning the single object I use
isn't freeing any memory it consumes quite a bit. I have gotten around
it by creating a special function in the object to manually release the
recursive references. However it would be nice if there were some sort
of 'magic' function that could be called on an object so that it knows
it is being deleted, sort of like a __preDestruct or something like
that... Is that a bad idea? Or I guess better able to handle recursive
references in objects would be the other way to solve it I guess.

Any feedback is welcome.

--
Nathanael D. Noblet

Search Discussions

  • Arnold Daniels at Aug 11, 2007 at 6:28 am
    Hi Nathanael,

    If you've been watching the list you could have seen that David Wang
    (god bless his soul) is working on this at the moment. Have a look at
    http://marc.info/?l=php-dev&m=118418663902191&w=2.

    You'll have to stick with your custom solution for now though.

    Best regards,
    Arnold


    Nathanael D. Noblet wrote:
    Hello,
    I have a case where an object has a few variables, a couple of which
    are objects which hold references back to this object.

    So very simply something like

    class A {
    var B
    }

    class B {
    var A
    }

    When doing something like so

    $obj = new A(); in a loop the object's never released from memory
    because of the internal references it holds. __destruct is not called
    until the end of script execution etc. Now normally in a web request
    situation this isn't a big deal. However I'm using it in a batch and
    loop over 22 000 records. Since re-assigning the single object I use
    isn't freeing any memory it consumes quite a bit. I have gotten around
    it by creating a special function in the object to manually release
    the recursive references. However it would be nice if there were some
    sort of 'magic' function that could be called on an object so that it
    knows it is being deleted, sort of like a __preDestruct or something
    like that... Is that a bad idea? Or I guess better able to handle
    recursive references in objects would be the other way to solve it I
    guess.

    Any feedback is welcome.
  • Nathanael D. Noblet at Aug 11, 2007 at 5:34 pm

    Arnold Daniels wrote:
    Hi Nathanael,

    If you've been watching the list you could have seen that David Wang
    (god bless his soul) is working on this at the moment. Have a look at
    http://marc.info/?l=php-dev&m=118418663902191&w=2.

    Wonderful! I just joined a few moments before so hadn't seen any traffic
    on this list. I guess I should start testing out his changes.

    Thanks,
    --
    Nathanael D. Noblet
  • Richard Lynch at Aug 12, 2007 at 5:42 am
    On Fri, August 10, 2007 6:01 pm, Nathanael D. Noblet wrote:

    I'm not a PHP OOP expert, so this could be 100% wrong, but...
    $obj = new A(); in a loop the object's never released from memory
    because of the internal references it holds. __destruct is not called
    until the end of script execution etc. Now normally in a web request
    If __destruct isn't being called until the end of the script, then PHP
    thinks you still have some way of accessing that object, and it can't
    free it.
    situation this isn't a big deal. However I'm using it in a batch and
    loop over 22 000 records. Since re-assigning the single object I use
    isn't freeing any memory it consumes quite a bit.
    Perhaps set some things to NULL or unset them explicitly to convince
    PHP that you are done with them.
    I have gotten around
    it by creating a special function in the object to manually release
    the
    recursive references.
    Well, there you go.

    Release the references so PHP knows you are done with them.
    However it would be nice if there were some sort
    of 'magic' function that could be called on an object so that it knows
    it is being deleted, sort of like a __preDestruct or something like
    that... Is that a bad idea? Or I guess better able to handle recursive
    references in objects would be the other way to solve it I guess.
    __destruct is called right before PHP destroys the object.

    There really isn't any event of any significance for a __perDesctruct...

    Either you're done with it, or you're not...

    --
    Some people have a "gift" link here.
    Know what I want?
    I want you to buy a CD from some indie artist.
    http://cdbaby.com/browse/from/lynch
    Yeah, I get a buck. So?
  • Arnold Daniels at Aug 12, 2007 at 7:57 pm
    Hi Richard,

    class A {
    public $bs = array();
    function addB() {
    $this->bs[] = new B($this);
    }
    }

    class B {
    public $parent;
    function __construct($parent) {
    $this->parent = $parent;
    }
    }

    function doSomething() {
    $a = new A();
    $a->addB();
    $a->addB();
    }

    See the problem? The object $a isn't available from global space, but
    because of the circular reference, A and all its Bs won't be picked up
    by the garbage collector. Have this 22 000 times and you run out of memory.

    Best regards,
    Arnold



    Richard Lynch wrote:
    On Fri, August 10, 2007 6:01 pm, Nathanael D. Noblet wrote:

    I'm not a PHP OOP expert, so this could be 100% wrong, but...

    $obj = new A(); in a loop the object's never released from memory
    because of the internal references it holds. __destruct is not called
    until the end of script execution etc. Now normally in a web request
    If __destruct isn't being called until the end of the script, then PHP
    thinks you still have some way of accessing that object, and it can't
    free it.

    situation this isn't a big deal. However I'm using it in a batch and
    loop over 22 000 records. Since re-assigning the single object I use
    isn't freeing any memory it consumes quite a bit.
    Perhaps set some things to NULL or unset them explicitly to convince
    PHP that you are done with them.

    I have gotten around
    it by creating a special function in the object to manually release
    the
    recursive references.
    Well, there you go.

    Release the references so PHP knows you are done with them.

    However it would be nice if there were some sort
    of 'magic' function that could be called on an object so that it knows
    it is being deleted, sort of like a __preDestruct or something like
    that... Is that a bad idea? Or I guess better able to handle recursive
    references in objects would be the other way to solve it I guess.
    __destruct is called right before PHP destroys the object.

    There really isn't any event of any significance for a __perDesctruct...

    Either you're done with it, or you're not...

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-internals @
categoriesphp
postedAug 10, '07 at 11:01p
activeAug 12, '07 at 7:57p
posts5
users3
websitephp.net

People

Translate

site design / logo © 2022 Grokbase