{"id":8,"date":"2014-12-01T12:02:30","date_gmt":"2014-12-01T12:02:30","guid":{"rendered":"http:\/\/dracony.org\/?p=8"},"modified":"2014-12-01T17:47:55","modified_gmt":"2014-12-01T17:47:55","slug":"php-memory-leaks-and-garbage-collection","status":"publish","type":"post","link":"https:\/\/dracony.org\/php-memory-leaks-and-garbage-collection\/","title":{"rendered":"PHP memory leaks and garbage collection"},"content":{"rendered":"

Historically memory leaks have not been a huge concern for PHP development. Since PHP processes exit after processing a request all used memory is deallocated. But that doesn’t mean we are free to not care about memory management altogether. If your application is processing several requests at the sime time, the total amount of wasted memory may add up to a hefty number.<\/p>\n

With projects like ReactPHP become more popular and widely used PHP is entering the domain of permanently running processes and we all look forward to using those to speed up our applications. But for that to become a reality our applications and frameworks have to be carefully rewritten to prevent memory leaks, since even the smallest one can grow to eat all available memory in a span of a month.<\/p>\n

PHP utilizes reference counting to decide when a object can be removed. Eachtime you assign an object to a new variable its refcount is increased by one<\/p>\n

\r\n\r\n$a = new stdClass;\r\n$c = $b = $a;\r\nxdebug_debug_zval('a'); \/\/refcount = 3\r\nunset($b, $c);\r\nxdebug_debug_zval('a'); \/\/refcount = 1\r\n\r\n<\/pre>\n

When a refcount reaches 0 the memory is deallocated. This is the first thing you have to think about when instantiating multiple objects, e.g. your domain entities. You have to make sure that the references to them don’t hang around after they are no longer needed. One good rule of thumb for doing so is avoiding global state as much as possible. If all your object references are inside some local scope, it’s much easier to keep track on whats happening to them than when they are scattered all over the place.<\/p>\n

Now let’s see wht happens here:<\/p>\n

\r\n\r\n$a = new stdClass;\r\n$b = new stdClass;\r\n\r\n$a->b = $b;\r\n$b->a = $a;\r\n\r\nxdebug_debug_zval('a'); \/\/refcount = 2\r\nxdebug_debug_zval('b'); \/\/refcount = 2\r\n\r\nunset($a, $b);\r\n\r\n<\/pre>\n

After we unset both $a and $b variables we have no way of accessing those objects from our application anymore, but their refcount is not 0. Since what was $a still references $b and vice versa both their refcounts are 1, and they won’t be removed from memory. And that is what a memory leak is. In a perfect world we would try to avoid this as much as possible, but this might be hard to achieve. PHP tries to detect such unreachable objects using it’s garbage collector. When it’s called it looks through the memory for lopps in object references and deallocates them, thus saving us memory. But this process is not free, running it too often may slow down the execution time. The PHP manual states the impact is about 7% which is quite considerable.<\/p>\n

I have found a nice trick that works for me to both have gc collection and get an almost insignificant performance impact: to run the garbage collection only after the end of each request. You can do it like so:<\/p>\n

\r\n\r\ngc_enable();\r\ngc_collect_cycles();\r\ngc_disable();\r\n\r\n<\/pre>\n

Basically we are enabling garbage collection, running it once and disabling it again. This way we ensure that it runs only a single time per request.<\/p>\n","protected":false},"excerpt":{"rendered":"

Historically memory leaks have not been a huge concern for PHP development. Since PHP processes exit after processing a request all used memory is deallocated. But that doesn’t mean we are free to not care about memory management altogether. If your application is processing several requests at the sime time, the total amount of wasted […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/posts\/8"}],"collection":[{"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/comments?post=8"}],"version-history":[{"count":4,"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/posts\/8\/revisions"}],"predecessor-version":[{"id":15,"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/posts\/8\/revisions\/15"}],"wp:attachment":[{"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/media?parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/categories?post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dracony.org\/wp-json\/wp\/v2\/tags?post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}