Jump to:

23483 Posts in 18976 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Memory Management Issue & __destruct

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1298 Views
  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Memory Management Issue & __destruct Link to this post

    Hi,

    I have a memory management issue - calling

    $vd->customise($arrdata)->renderWith('MyTemplate');

    over and over in a loop - and it is failing with memory limit. I've tried adding...

    unset($vd);
    unset($arrdata);

    but this frees a tiny amount of memory and my reading of the php doc page is that it is only freeing the object, not the memory allocated by the object. To free that it would expect me to call it's decontructor first... it's "__destruct" method... I searched the code and couldn't find one... so...

    what is the best way to manage memory for this in silverstripe?

  • Nivanka
    Avatar
    Community Member
    395 Posts

    Re: Memory Management Issue & __destruct Link to this post

    hi,

    I think this can sometimes help.

    $vars = get_object_vars($vd);

    foreach($vars as $val){
    if(isset($vd->$val)){
    unset($vd->$val);
    }
    }

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Memory Management Issue & __destruct Link to this post

    Massive help, thanks!

    I've now got this batch process working, I've changed the code a bit and it means that now when the memory limit is reached php does is garbage collection and everything works out fine! (instead of previously where none of the memory was able to be collected!). Note this version uses recursion to free all of the objects in it's object graph.

    function dounset($var) {
       $type=gettype($var);
       if ($type=='object') {
          $vars = get_object_vars($var);
          if (count($vars)) foreach($vars as $val) {
             $this->dounset($val);
          }
       }
       else {
          unset($var);
       }
    }

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Memory Management Issue & __destruct Link to this post

    Quick note - this only works on php 5.3.x as you need the zend.enable_gc in the php.ini

    1298 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.