5

In php is all the memory allocated to run my script released at the end of my page request or do I need to worry about memory leaks building up over time?

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
  • PHP frees all memory once the request ends. It will not leak memory. You _can_ however reach the `memory_limit` in your script, but that's another story. – mishmash Jan 21 '13 at 09:06

5 Answers5

4

No. You do not need to manually free (call unset()) resources. PHP will do this automatically. Everything gets free'd at the end of the request lifetime. So no, you do not need to bother with this. If you do this:

<?php
$resource = allocate_heavy_resource();
?>

The $resource will get freed at the end of the request and so this will not leak memory. If it does than that means there is a serious bug in PHP and any discussion of normal operation goes through the window anyway.

EDIT: There are exceptions, of course. Like persistent database connections. But those get handled eventually, so its not really a memory leak.

mishmash
  • 4,422
  • 3
  • 34
  • 56
1

Yes, all memory is released after your script terminates. However, memory leaks can occur during your script based on some algorithms. You can use unset() to free up the variable, but contrary to vanneto's point it will not actually free up the memory.

Community
  • 1
  • 1
NobleUplift
  • 5,631
  • 8
  • 45
  • 87
  • How is using too much memory a memory leak? – mishmash Jan 21 '13 at 09:00
  • What about internal caches used by PHP? Are they always released after every request? – ta.speot.is Jan 21 '13 at 09:03
  • @ta.speot.is - what internal caches were you thinkng of? The whole point of caches like APC are to preserve compiled code and data between requests – Mark Baker Jan 21 '13 at 09:05
  • @MarkBaker Any cache that PHP uses internally that might persist between requests. – ta.speot.is Jan 21 '13 at 09:07
  • the only memory leaks that can exist are the one that have in PHP. latest 5.3, 5.4 and 5.5 has less mem leaks than before. – artragis Jan 21 '13 at 09:09
  • @ta.speot.is - in that case, no.... if you have a cache that is designed to persist between requests, then it isn't released after each request.... session files and APC being obvious examples of this, or persistent database connections are also left "open" – Mark Baker Jan 21 '13 at 09:09
  • @MarkBaker That was my point. This answer's statement that "All memory is released..." is a simplification. And even things a script doesn't do can cause memory to be held after a request, PHP's implementation is free to cache implementation details across requests. – ta.speot.is Jan 21 '13 at 09:12
  • Yes, but creating a cache is not creating a memory leak now is it? – mishmash Jan 21 '13 at 09:13
  • @artragis Memory leaks can be caused by certain algorithms, [for example](http://paul-m-jones.com/archives/262), but I believe this has been patched or can be fixed by gc_enable() like user1914292 said. – NobleUplift Jan 21 '13 at 09:16
  • 1
    @ta.speot.is Memory != cache. – NobleUplift Jan 21 '13 at 09:20
  • @NobleUplift his question was `In php is all the memory allocated to run my script released **at the end** of my page request...?` so that bug does not apply. – mishmash Jan 21 '13 at 09:20
  • that bug has been corrected by the improvement of the internal gc_collect_cycles. – artragis Jan 21 '13 at 09:47
  • @vanneto I wasn't applying that bug to the question or it would be in my answer. It was in response to artragis' comment on memory leaks. – NobleUplift Jan 21 '13 at 09:48
  • @artragis As I said, I knew it was fixed by now but it was the best example of a memory leak algorithmic in nature. – NobleUplift Jan 21 '13 at 09:49
1

PHP does release memory that you claim by building objects etc. Still there are scenarios where memory is NOT released. For this the principle of garbage collection was introduced in version 5.3.

You can use the gc_enable() function to execute it. Garbage collection in PHP is NOT active by default.

user1914292
  • 1,586
  • 13
  • 38
  • You mean there exists a scenario where I allocate a resource in one request and it will persist to a second request? I'd like to see an example of this please. – mishmash Jan 21 '13 at 09:08
  • I didn't see you're going to be able to use the memory in another request. I said it was not always being freed and certainly not immediately. In case you need memory to be shared you should use other tools (for caching I recommend APC for example). – user1914292 Jan 21 '13 at 09:31
1

Kind of, Memory leaks occur when things are cached. So if you have memory leaks in your php script your apache processes will increase over time, You can restart them after so many requests. Check the your Multi Processing Module (MPM) usually prefork or worker.

Most cases though this wont effect you much unless your doing a lot of processing with PHP

exussum
  • 18,275
  • 8
  • 32
  • 65
0

PHP uses garbace collector. It frees all variables to which there are no references left.

http://v1.srcnix.com/2010/02/10/7-tips-to-prevent-php-running-out-of-memory/

I am Pretty sure GC does it automatically for you. (it even closes open mysql connections)

Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90