mseifert
mseifert

Reputation: 5670

Is this a php memory leak?

UPDATE I noticed that with a particular require_once('myobject.php') that the memory increase showed up. This require_once() was run conditionally. When I moved the require_once()statement out of the if statement so it loads all the time, the memory increase went away. Doesn't make sense to me, but the issue is solved. I'm not sure this is actually an "answer" so I will leave it unanswered.

I have memory_get_usage() in the footer of my page and with each refresh of the page, I watch it increase by about 100k each time. My page load creates many objects and destroys them when done . My parent objects each have __destruct() which uses unset() with all child objects. Child objects with a reference back to the parent, have __destruct() to unset() these references.

Inserting memory_get_usage() before and after processing different parts of my page only tells me how much of the total usage was added due to that part of the script. How do I go about determining what memory is lost and not recycled for garbage collection after the page finishes loading?

I have one global $_SESSION var containing objects storing user info, but have verified using strlen(serialize($object)) that this object is not growing in size.

I presume that what I am seeing is a memory leak and that php garbage collection should be in effect after the script ends. Any ideas how to debug this?

Upvotes: 0

Views: 740

Answers (1)

Matt
Matt

Reputation: 5567

Someone can correct me if I'm wrong, but if you have a standard Apache and PHP setup, the PHP process is destroyed after the request finishes, so it doesn't use memory between requests.

So unset(), __destruct(), etc. doesn't matter (across requests). When the PHP process ends, all memory is reclaimed.

But likely, there is no memory leak, but possibly some session file is increased in size so it is larger when it is loaded the next time.

Note: This could be different for php-fpm, I'm not sure.

Upvotes: 2

Related Questions