取消设置后释放PHP的可用内存 [英] PHP free memory after unset

查看:97
本文介绍了取消设置后释放PHP的可用内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于测试的小代码:

$strings = array('<big string here (2 Mb)');
$arr = array();

//--> memory usage here is 17.1Mb (checked by pmap)
echo memory_get_usage();//0.5Mb 
//(i know, that other 16.6Mb of memory used by process are php libraries)

for($i = 0; $i < 20; ++$i)
{
    $strings_local = array_merge($strings, array($i));
    $arr[$i] = $strings_local;
    unset($strings_local);
}

//--> memory usage here is 20.3Mb (checked by pmap)
echo memory_get_usage();//3.7Mb
//so, here its all ok, 17.1+3.2 = 20.3Mb

for($i = 0; $i < 20; ++$i)
{
    unset($arr[$i]);
}

//--> memory usage here is 20.3Mb (checked by pmap)
//BUT?? i UNSET this variables...
echo memory_get_usage();//0.5Mb

所以,即使您unset()您的变量,php似乎也不是空闲内存.取消设置后如何释放内存?

So, seems like php is not free memory, even if you unset() your variable. How can i free memory after unset?

推荐答案

PHP具有垃圾收集器,该垃圾收集器负责您的内存管理,以几种不同的方式影响(进程)的内存使用.

PHP has garbage collector which takes care of the memory management for you, which affects to memory usage (of the process) in several different ways.

首先,当检查进程外的某个进程的内存使用情况时,即使PHP看到某些内存已释放,也可能不会出于与内存分配相关的优化目的将其释放回OS.这样可以减少连续释放和分配产生的开销,这对于GC语言来说更容易发生,因为实际程序看不到分配过程.

First, when inspecting memory usage of a process outside of the process, even if PHP sees some memory to be freed, it may not be released back to the OS for optimization purposes related to memory allocation. This is reduce overhead from continuous frees and allocs, that happen more easily with GC’d languages, as allocation procedure is not visible to the actual program.

因此,即使有人手动调用gc_collect_cycles(),内存也可能根本不释放给OS,而是重新用于以后的分配.这导致PHP看到的内存使用量比实际使用的进程要少,这是由于某些早期的大保留从未将其释放回操作系统.

For that reason, even if one calls gc_collect_cycles() by hand, the memory may not be freed to the OS at all, but rather reused for future allocations. This causes PHP to see smaller memory usage than the process in reality uses, due to some early big reservation which never gets to freed back to the OS.

第二,由于垃圾回收的性质,在标记为程序未使用后,可能无法立即释放内存.调用gc_collect_cycles()将立即释放内存,但应将其视为不必要,并且如果脚本中存在逻辑(或PHP泄漏中的某些内容)内存泄漏,则该操作将无效.

Second, due to nature of garbage collection, the memory may not be immediately freed after marked unused by the program. Calling gc_collect_cycles() will make the memory freed immediately, but it should be seen unnecessary, and does not work if you have logical (or something in PHP leaks) memory leak in your script.

要知道发生了什么,逐行检查(例如,使用Xdebug的函数跟踪)将使您更好地了解PHP(或更确切地说,您的程序)如何看待内存使用情况.

For knowing what is going on, doing line by line inspection (for example with Xdebug’s function trace) would give you better insight about how PHP (or rather, your program) sees the memory usage.

将其与从进程外部进行逐行检查(例如,您的pmap命令)相结合,可以知道PHP在保留之后是否确实在任何时候释放了任何内存.

Combining that to line-by-line inspection from outside of the process (for example your pmap commands) would tell if the PHP actually is freeing any memory at any point after reserving it.

这篇关于取消设置后释放PHP的可用内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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