开发者

Memory leak in transition from PHP 4 to 5 - any hints?

I am porting a large web application to a shared web hosting environment. The app was written in PHP 4. The new environment has PHP 5.2.

For some reason, the application is leaking tremendous amounts of memory when running in PHP 5.2. I can literally watch memory usage going through the roof using memory_get_usage(), until it reaches the limit of 64 MB (which is very generous in a shared environment, the site used to run with 12 or 16 MB on PHP 4).

I suspect that this is a side-effect of something that has changed in PHP 5, most likely the handling of object references in arguments. However, the search for the particular behaviour(s) that cause(s) the leak is extremely difficult as the application is very complex, and partly not very well written (yes, it is mine).

As the app is installed on a shared webspace, I can not use the classical debugging facilities (as far as I know). I would very, very much like to avoid recreating the environment locally, as I am working on the project literally all day for some time.

So my questions are:

  • Does anybody know typical PHP4 => 5 transition pitfalls, especially when dealing with large amounts of objects, that might be the cause for my leak(s)

  • Does anybody know some kind of debugger-inde开发者_开发知识库pendent, script-only "graphical scope dump" tool for the variables used by PHP that might help me find out which variables are eating up these enormous amounts of memory?


To solve this issue I would:

  1. Profile the script with xdebug+wincachegrind (or some other profiler) and/or firephp.
  2. Turn on the STRICT mode to get all the php error + notices.

Doing that, you will be able to:

  • Optimize your code. (Removing the errors/warning/notices can speed up your code)
  • Clean your code. (Using the proper php5 object syntax can only be good for the performance).

The object model totally changed from PHP4 to PHP5. For sure, the STRICT mode will tell you that you should not use an explicit "passed by reference".


One common one I found in one release of 5.2 was that strtotime leaked without memory_get_usage catching it. Changing to strptime fixed the issue.

There is a bug report for this for 5.2.8 - http://bugs.php.net/bug.php?id=46889.


Without debugger you can open a script that you think use many memory and put memory_get_usage function in critical locals.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜