Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Memory Management Issue & __destruct


Go to End


4 Posts   1995 Views

Avatar
swaiba

Forum Moderator, 1899 Posts

24 May 2011 at 1:58am

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?

Avatar
Nivanka

Community Member, 400 Posts

25 May 2011 at 5:21pm

hi,

I think this can sometimes help.

$vars = get_object_vars($vd);

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

Avatar
swaiba

Forum Moderator, 1899 Posts

25 May 2011 at 9:34pm

Edited: 26/05/2011 12:20am

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);
	}
}

Avatar
swaiba

Forum Moderator, 1899 Posts

27 May 2011 at 2:42am

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