64

I know that in PHP you don't have to free memory. Is it reached by garbage collector?

5 Answers 5

59

Yes there is, here's a nice article describing its pitfalls. In PHP > 5.3.0, there is also the gc_enable function.

3
  • 7
    One gotcha not included in that article is anonymous functions, created with create_function(). These are not subject to garbage collection. Commented Jun 29, 2011 at 13:59
  • @Dave Lancea Good observation. I would just point out however that your link points on a comment made on 2007, thus perhaps it is not like this anymore in 2017 (but maybe it is, I could not find more info on the subject).
    – Jamil Said
    Commented Oct 12, 2017 at 5:46
  • @DaveLancea, hopefully, there is no create_function() as of PHP 8.0.0. :) Commented Apr 3, 2022 at 20:55
11

PHP has a combination of garbage collection and reference counting. The latter is the main mode of managing memory, with the garbage collector picking up the pieces that the ref counter misses (circular references). Before 5.3, php only had ref-counting, and even in 5.3 it's the still how memory will usually be freed.

3

Yes. There is also session cleanup done by the garbage collector.

3

since 5.3.0 there is garbage collection support. please check this very informative article from php.net http://php.net/manual/en/features.gc.php

1

Since 5.3.0 you can force garbage collection by using gc_collect_cycles function.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.