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

2023-07-16php开发问题
1

本文介绍了诊断内存泄漏 - 已用完 # 字节的允许内存大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我遇到了可怕的错误消息,可能是经过艰苦的努力,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.

这篇关于诊断内存泄漏 - 已用完 # 字节的允许内存大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17