如何查找哪个PHP脚本正在泄漏内存? [英] How to find which PHP script is leaking memory?

查看:354
本文介绍了如何查找哪个PHP脚本正在泄漏内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的专用服务器具有32GB RAM,并且内存在不断增加,现在我必须每天重新启动它.这使我付出了客户和金钱的代价.

My dedicated server has 32GB RAM and the memory just goes up and up constantly and I have to reboot it daily now. This is costing me customers and money.

我很难找到内存泄漏的位置.我只能在网上找到有人说"Use xdebug",但我还找不到任何有关发现内存泄漏的xdebug教程.我曾在函数调用之前和之后尝试打印memory_get_usage,但这是正确的方法吗?

I am having a hard time finding where the memory leak is. All I can find online is people say "Use xdebug" but I haven't been able to find any xdebug tutorials on finding memory leaks. I have tried printing memory_get_usage before and after function calls but is that the right way to do it?

我正在运行许多php脚本-有些来自访客,有些来自cron作业-我需要找出其中的哪一个正在泄漏内存并尽快修复它,但我什至不知道如何确定给定功能是否正在泄漏内存.

I have MANY php scripts running - some from visitors, others from cron jobs - and I need to find which one(s) of them is leaking memory and fix it ASAP but I don't even know how to determine if a given function is leaking memory or not.

我曾尝试在函数调用之前和之后打印memory_get_usage,但它会上升,但是如果我多次调用该函数,它将不再上升.有人可以解释一下,告诉我如何简单,轻松地判断PHP函数是否存在内存泄漏吗?

I have tried printing memory_get_usage before a function call and after, and it goes up, but then if I call the function more than once, it doesn't go up anymore. Can someone please explain this and tell me how I can simply and easily tell if a PHP function has a memory leak?

推荐答案

您可以做很多事情,但是首先,您应该首先避免产生内存泄漏.

You could do various things, but first you should try to avoid the creation of memory leaks in the first place.

让我澄清一下:PHP是一种脚本语言,它不是为长时间运行的脚本而设计的,因此它的内存管理并不是市场上最好的.但是为什么会这样呢?目的是在请求级别上调用它​​,以使其运行范围很小(不超过2-3秒).其他所有内容都应该放在后台.

Let me clarify: PHP is a scripting language and it is not designed for long running scripts, so it's memory management is not the best on the market. But why should it be? It's purpose is to be called on a request level so its running scope is quite small (not more than 2 - 3 seconds). Everything else should be put in the background.

我该如何应对内存泄漏?

What can I do against memory leaks?

  1. 如果您的版本低于5.4,则需要注意圆形参考,因为这些参考不是垃圾收集.

  1. If you are at a version below 5.4 you need to take care of circle references, since those are not garbage collected.

如果需要连续运行脚本,则可以考虑使用其他方法.尝试使用while(true)实现,但是将supervisor( http://supervisord.org )包装起来,并在之后调用它它结束了.这样一来,您可以100%确保不会出现内存泄漏.

If you need a script to be run continuously, you might think about a different approach. Do try a while(true) implementation, but wrap supervisor (http://supervisord.org) around your script, and let it be called after it ends. That way you make 100% sure you never get memory leaks.

您可以使用xdebug逐个概要分析脚本,并找出消耗大量内存的位置.

You could use xdebug to profile your scripts one by one and find out, where a lot of memory is consumed.

如果不再需要该类,则可以实现一个析构函数以取消引用的所有内容.

You could implement a destructor to unset all you references if the class is not of any need anymore.

public function __destruct(){
    $this->cleanup();
}

public function cleanup() {
    //cleanup everything from attributes
    foreach (get_class_vars(__CLASS__) as $clsVar => $_) {
        unset($this->$clsVar);
    }

    //cleanup all objects inside data array
    if (is_array($this->_data)) {
        foreach ($this->_data as $value) {
            if (is_object($value) && method_exists($value, 'cleanUp')) {
                $value->cleanUp();
            }
        }
    }
}

  • 通读有关垃圾收集的PHP文档 http://us3.php.net/manual/zh-CN/features.gc.php

    避免使用全局变量,因为它们永远不会被垃圾回收,而需要显式地unset.如果您使用的是ZF或Symfony之类的框架,则可能无法实现,因为这样做会破坏功能.

    Avoid global variables, because those are never garbage collected and need to be unset explicitly. If you are using a Framework like ZF or Symfony that might not be possible, since you would break functionality if you do.

    最后但并非最不重要的一点是,我想再次强调,PHP不适合长时间运行的脚本!如果您有事要做,那需要连续运行,那么您不应该因为PHP的内存泄漏而崩溃,而应该花时间学习更复杂的语言,例如JAVA或C#.

    Last but not least I want to emphasize once again, PHP is not suited for long running scripts! If you have things to do, that need to run continuously you should not crumble your head with memory leaks in PHP, but take the time to learn a more sophisticated language like JAVA or C#.

    这篇关于如何查找哪个PHP脚本正在泄漏内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆