1

Do I need to worry about memory leaks with PHP? In particular, I have the following code that is being called from a browser. When the call finishes, is everything cleaned up properly, or, do I need to clear the memory created by the first array that was created?

class SomeClass
{
  var $someArray = array();

  function someMethod()
  {
     $this->someArray[1] = "Some Value 1";
     $this->someArray[2] = "Some Value 2";
     $this->someArray[3] = "Some Value 3";
     $this->someArray = array();
     $this->someArray[1] = "Some other Value";
     $this->someArray[2] = "Some other Value";
     $this->someArray[3] = "Some other Value";
  }
}

someMethod();

Thanks, Scott

1
  • 2
    Every script invocation starts a new PHP process. All memory is lost between those calls. Commented Jun 22, 2012 at 1:10

3 Answers 3

3

Do I need to worry about memory leaks with PHP?

It's possible to have a cyclic reference in PHP where the refcount of the zval never drops to 0. This will cause a memory leak (GC won't clean up objects that have a reference to them). This has been fixed in >= PHP 5.3.

In particular, I have the following code that is being called from a browser. When the call finishes, is everything cleaned up properly, or, do I need to clear the memory created by the first array that was created?

PHP scripts have a request lifecycle (run application, return response, close application), so it shouldn't be a worry. All memory used by your application should be marked as free'd when your application finishes, ready to be overwritten on the next request.

Sign up to request clarification or add additional context in comments.

2 Comments

I believe that as of PHP 5.3 the cyclic reference problem has been fixed.
@Corbin You're right, just found it mentioned in some slides. I'll make an edit.
1

If you're super paranoid, you can always unset things, however, PHP is a garbage collected language meaning that unless there is a bug in the core or in an extension, there is never going to be a memory leak.

More information


On a side note, you should use the newer PHP 5 OOP syntax. And, someMethod would be an error. It would need to be $obj->someMethod() where $obj is an instance of the class.

Comments

1

There actually do exist memory problems if you run mod_php through Apache with the mpm_prefork behavior. The problem is that memory consumed by PHP is not released back to the operating system. The same Apache process can reuse the memory for subsequent requests, but it can't be used by other programs (not even other Apache processes).

One solution is to restart the processes from time to time, for example by setting the MaxRequestsPerChild setting to something rather low (100 or so, maybe lower for lightly loaded servers). The best solution is to not use mod_php at all but instead run PHP through FastCGI.

This is a sysadmin issue though, not a programmer issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.