PHP在单个请求中分配的内存是否总是在最后释放? [英] Is the memory allocated by PHP in a single request always released at the end?

查看:215
本文介绍了PHP在单个请求中分配的内存是否总是在最后释放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP的内存泄漏有些困惑.

I'm a bit confused about the memory leaks in PHP.

由于Zend Memory Manager,我已经了解到PHP会自动释放每个请求中使用的内存: http://www.webreference.com/programming/php_mem/2.html

I've read that PHP is releasing automatically the memory used in each request thanks to the Zend Memory Manager: http://www.webreference.com/programming/php_mem/2.html

但是我看到很多人和话题(甚至是SO方面的话题)都与PHP和内存泄漏有关.

But I see a lot of people and topics (even here in SO) concerned about PHP and memory leaks.

所以我觉得我正在失去一些东西.

So I feel that I'm losing something.

是否可能在不同请求之间的PHP中发生内存泄漏?

推荐答案

在不同请求之间(使用默认Apache配置时),PHP脚本不可能发生内存泄漏,因为在一个请求中使用的变量和代码是在该请求结束时释放,PHP的内存分配器针对下一个请求重新开始.但是,PHP解释器或扩展中的错误可能会单独泄漏内存.

It is not possible to have memory leaks from PHP scripts between different requests (when using the default Apache configuration), as the variables and code used in one request are released at the end of that request and PHP's memory allocator starts afresh for the next request. Bugs in the PHP interpreter or extensions could leak memory separately, however.

更大的问题是Apache子进程内部具有PHP的内存空间.他们膨胀以分配PHP脚本的峰值内存使用量,然后维持此内存分配,直到子进程被杀死(一旦某个进程要求内核分配一部分内存,该内存消失,直到该进程终止,该内存才被释放). ).有关为何会出现问题以及如何解决此问题的更详细说明,请参见

A much greater problem is that Apache child processes have PHP's memory space inside them. They swell to allocate the peak memory usage of a PHP script and then maintain this memory allocation until the child process is killed (once a process has asked the kernel to allocate a portion of memory, that memory won't be released until the process dies). For a more detailed explaination of why this is a problem and how to combat it, see my answer on Server Fault.

脚本中的内存泄漏非常罕见-脚本中的内存泄漏没有设置变量并且PHP垃圾收集器发生故障-大多数PHP脚本运行几百毫秒,而且通常这还不足以显示严重的内存泄漏

Memory leaks in a script, where variables are not unset and the PHP garbage collector fails, are very rare - most PHP scripts run for a few hundred milliseconds, and this is not generally enough time for even a serious memory leak to manifest.

您可以通过 memory_get_usage() memory_get_peak_usage() -在 PHP手册.

本文中详细介绍了PHP的内存管理.

PHP's memory management is explained in detail in this article.

编辑:您可以使用httpd -l确定将模块编译到Apache中-默认值因操作系统分发和存储库配置而异.有很多方法可以将PHP连接到Apache-最详细的此处

edit: You can determine the compiled in modules to Apache with httpd -l - defaults vary by OS distribution and repository configuration. There are plenty of ways to interface PHP to Apache - most detailed here.

这篇关于PHP在单个请求中分配的内存是否总是在最后释放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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