dsf323233323332 2016-10-30 04:18
浏览 137
已采纳

执行后,同一对象中的方法是否释放内存?

I'm trying to debug a PHP memory leak. According to https://stackoverflow.com/a/29398380/2724978, PHP frees memory after function ends.. well, is that also valid for object functions?

My object has the following method:

function process All(){

    $this->processA(); // intensive array processing - runs fine
    $this->processB(); // intensive array processing - runs fine
    $this->processC(); // intensive array processing - runs fine
    $this->processD(); // intensive array processing - memory leak

}

Then I change to this:

function process All(){

    $this->processD(); // intensive array processing - runs fine

}

So it appears that every function is piling up on the memory. Any ideas on how can I free it?

  • 写回答

1条回答 默认 最新

  • douqi2804 2016-10-30 05:10
    关注

    First, a clarification of terms (so that we're clear on what we mean when we say memory leak).

    • A memory leak is what happens when a program has allocated memory on the system that is no longer reachable by the program. This simply cannot happen as a direct result of your PHP code since PHP is solely responsible for managing your program's memory for you. Meaning that if a memory leak were to occur it would be a mistake made by PHP itself (the PHP memory manager) and not PHP code that you could have written. Only PHP extensions, which are written in C, and the PHP memory manager itself can leak memory. PHP code, cannot. I can assure you that the latter is exceedingly rare, if not impossible.

    So according to this information, it is not possible for your PHP code to leak memory (period).

    What you probably mean to say instead, is that you've either hit the user-defined PHP memory_limit or your code has a resulted in in OOM (Out of Memory) error. This is not what one would refer to as a memory leak, however. So it is crucially important that you include the actual OOM error along with your question in order to be able to decipher the problem with any degree of authority.

    Because the answer you point to has to do with freeing local function variables, and the code you present as the problem, provides absolutely no indication that any local function variables are being used, it remains entirely unclear where memory is being allocated in the first place.

    If I had to take a wild guess, I would say you're probably storing this array as an object property. Thus it remains in memory subject to any living reference of the object instance during that runtime.

    The difference between a local variable and an instance property is that a local variable is destroyed after the function is finished. An instance property, however, is not destroyed until the object instance to which it belongs is destroyed. This doesn't happen until the last reference to that object is destroyed or you explicitly do so yourself.

    class Foo {
    
        protected $arr = [];
    
        public function bar($baz) {
            $this->arr[] = $baz; // object instance property
            $quix = $baz; // local function variable
        }
    
    }
    
    $foo = new Foo;
    $foo->bar(1);
    $foo->bar(2);
    $foo->bar(3); // memory grows
    

    In the above scenario the local variable $quix is always destroyed after each method call (this doesn't change whether you use a function or method, they are the same to PHP). However, the instance property $this->arr belongs to the object instance. So PHP cannot remove that from memory by itself until that instance $foo is destroyed or the script ends (implicitly destroys all memory).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启