开发者

Php memory leak question

开发者 https://www.devze.com 2023-02-01 07:06 出处:网络
I\'m having a terrible amount of problems with an XML parsing script leaking some memory in PHP. I\'ve made a solution by rewriteing my whole OOP code to non OOP, which was mostly database checks and

I'm having a terrible amount of problems with an XML parsing script leaking some memory in PHP.

I've made a solution by rewriteing my whole OOP code to non OOP, which was mostly database checks and inserts, and that seemed to plug the hole, but I'm curious as to what caused it? I'开发者_StackOverflow中文版m using Zend Framework and once I removed all of the model stuff, there are no leaks.

Just to give you and idea how bad it was: I'm running through some 30k items on the same number of files. So, one per file. It started out by using 5mb!!! or RAM, when the file itself was only about 20kb big.

Could it be those referencing functions that I've read about? I thought that that bug was fixed?!

EDIT

I found out, that the leak was due to using Zend Framework database classes. Is there a way to call a shutdown function after each iteration, so that it would clear the resources?


Its pretty dificult to answer this as we have no code to work with.

Revert back to the OOP version of your sources and create a small class like so:

abstract class MemoryLeakLogger
{
     public static $_logs = array();

     public function Start($id,$action)
     {
          self::$_logs[$id] = array(
              'action' => $action,
              'start_ts' => microtime(),
              'memory_start' => memory_get_usage()
          );
     }

     public function End($id)
     {
          self::$_logs[$id]['end_ts'] = microtime();
          self::$_logs[$id]['memory_end'] = memory_get_usage();
     }

     public static function GetInformation(){return self::$_logs;}
}

and then within your application do the following:

MemoryLeakLogger::Start(":xml_parse_links_set_2", "parsing set to of links");
/*
    * Here you would do the relative code
*/
MemoryLeakLogger::End(":xml_parse_links_set_2");

And so forth throughout your application, you will need to create calculations to gather the offsets for memory usages and time taken per action, once your script is completed just debug the information by printing it in a readable fashion and look for peaks

You can also use xdebug to trace your application.

Hope this helps

0

精彩评论

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

关注公众号