php5.2.0的内存管理做了较大的改进,某些情况下内存不能释放的问题不存在了。
测试php脚本(mem.php),我用echo N>>和sleep来控制脚本在某个阶段暂停以便检测状态。
复制代码 代码如下:
echo '1>>';
sleep(5);
$o = array();
for ($i=0;$i $o[]='aaaaaaaaaaaaaaaaaaaaa';
}
echo '2>>';
sleep(5);
unset($o);
echo '3>>';
while (true) {
echo '..';
sleep(10);
}
?>
while true;do clear;ps au|grep -v "\(vi\|grep\)"|grep "\(mem\|RSS\)";sleep 2;done;
以下是$/usr/local/bin/php mem.php这个进程在三种状态(数组创建前,数组创建后,数组销毁后)下,用5.1.6和5.2.0的PHP(我使用了一样的configure参数)测试的RSS(内存使用值,单位KB)结果。
php5.1.6:
3164
18076
17572
PHP5.2.0:
4088
14400
4424
大家可以看到5.1.6这个版本,unset数组之后,内存并没有从进程里释放出来,虽然它是可以继续被这个php进程再利用,但是却无法被系统的其他进程所使用。而5.2.0则真正释放出了内存。
大家可能还注意到,在最开始,5.2.0的内存使用比5.1.6多了几个kb,这是由于5.2.0增加了一些新的东西,这个是很正常的。
另外php5.2.0的内存分配也进行了大的改进,官方的说法是对memory_limit的检测由每次呼叫emalloc()的时候进行改为直接检测从系统请求的内存数据块(blocks)。需要更进一步了解的朋友可以自己去研究代码。而由于内存分配的实现的更改,内存控制可以更精确地被控制在memory_limit之下,也就是说以前php代码里,如果有超过了memory_limit的内存使用而没有出错的,在php5.2.0里可能会报错。为了平衡这次改进,PHP5.2.0的默认的memory_limit由以前的8MB改为了16MB.搜索源代码可以看到这个修改(find . -name \*c -type f |xargs cat |grep memory_limit)。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 English version
Recommended: Win version, supports code prompts!
