诊断内存泄漏 - 已用完 # 字节的允许内存大小

2021-12-30 00:00:00 memory-leaks php

我遇到了可怕的错误消息,可能是经过艰苦的努力,PHP 内存不足:

I've encountered the dreaded error-message, possibly through-painstaking effort, PHP has run out of memory:

第 123 行的 file.php 中 #### 字节的允许内存大小耗尽(尝试分配 #### 字节)

Allowed memory size of #### bytes exhausted (tried to allocate #### bytes) in file.php on line 123

增加限制

如果您知道自己在做什么并想增加限制,请参阅memory_limit:

ini_set('memory_limit', '16M');
ini_set('memory_limit', -1); // no limit

当心!您可能只是在解决症状,而不是问题!

Beware! You may only be solving the symptom and not the problem!

错误消息指向我认为正在泄漏或不必要地累积内存的带有循环的行.我在每次迭代结束时打印了 memory_get_usage() 语句,可以看到数字缓慢增长直到达到限制:

The error message points to a line withing a loop that I believe to be leaking, or needlessly-accumulating, memory. I've printed memory_get_usage() statements at the end of each iteration and can see the number slowly grow until it reaches the limit:

foreach ($users as $user) {
    $task = new Task;
    $task->run($user);
    unset($task); // Free the variable in an attempt to recover memory
    print memory_get_usage(true); // increases over time
}

出于这个问题的目的,让我们假设可以想象的最糟糕的意大利面条式代码隐藏在全局范围内的$userTask 中.

For the purposes of this question let's assume the worst spaghetti code imaginable is hiding in global-scope somewhere in $user or Task.

哪些工具、PHP 技巧或调试伏都教可以帮助我找到并解决问题?

推荐答案

PHP 没有垃圾收集器.它使用引用计数来管理内存.因此,最常见的内存泄漏源是循环引用和全局变量.如果您使用一个框架,恐怕您将有很多代码需要搜索才能找到它.最简单的方法是有选择地调用 memory_get_usage 并将其缩小到代码泄漏的位置.您还可以使用 xdebug 来创建代码跟踪.使用 执行跟踪 和 show_mem_delta 运行代码.

PHP doesn't have a garbage collector. It uses reference counting to manage memory. Thus, the most common source of memory leaks are cyclic references and global variables. If you use a framework, you'll have a lot of code to trawl through to find it, I'm afraid. The simplest instrument is to selectively place calls to memory_get_usage and narrow it down to where the code leaks. You can also use xdebug to create a trace of the code. Run the code with execution traces and show_mem_delta.

相关文章