空的PHP脚本上的PHP memory_get_usage() [英] PHP memory_get_usage() on empty PHP script

查看:49
本文介绍了空的PHP脚本上的PHP memory_get_usage()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定看看为我的一些PHP脚本分配了多少内存,发现它的峰值约为130KiB.考虑到脚本中发生了什么,我认为还不错.

I decided to take a look at how much memory was being allocated to a few of my PHP scripts, and found it to be peaking at about 130KiB. Not bad, I thought, considering what was going on in the script.

然后,我决定看看脚本的开始位置.我期望32KiB左右.

Then, I decided to see what the script started at. I expected something around 32KiB.

我得到了121952个字节.之后,我尝试测试一个完全没有脚本的脚本:

I got 121952 bytes instead. After that, I tried testing a completely devoid script:

<?php 
echo memory_get_usage();

它也从分配了相同数量的内存开始.

It also started with the same amount of memory allocated.

现在,很明显,PHP将在运行脚本之前为该脚本分配一些内存,但这似乎有点多余.

Now, obviously, PHP is going to allocate some memory to the script before it is run, but this seems a bit excessive.

但是,根据当时系统可用的内存量来看,它似乎根本不是动态的.我尝试通过打开其他进程来消耗更多的系统内存,但是预分配的内存量保持相同的确切字节数.

However, it doesn't seem to be dynamic at all based on how much memory is available to the system at the time. I tried consuming more system memory by opening other processes, but the pre-allocated memory amount stayed the same exact number of bytes.

这是否可以在每个脚本的基础上进行配置,并且PHP如何确定它将分配给每个脚本的数量?

Is this at all configurable on a per script basis, and how does PHP determine how much it will allocate to each script?

使用PHP版本5.4.7

Using PHP Version 5.4.7

谢谢.

推荐答案

memory_get_usage函数直接查询PHP的内存分配器以获取此信息.它报告PHP本身使用了多少内存,而不报告整个过程甚至整个系统正在使用多少内存.

The memory_get_usage function directly queries PHP's memory allocator to get this information. It reports how much memory is used by PHP itself, not how much the whole process or even the system as a whole is using.

如果不传递其他true自变量,则返回的内容是代码使用的确切内存量;这永远不会超过memory_get_usage(true)的报告.

If you do not pass in an additional true argument what you get back is the exact amount of memory that your code uses; this will never be more than what memory_get_usage(true) reports.

如果您调用memory_get_usage(true),您将获得分配器已从系统保留的堆的大小,其中包括代码未实际使用但可直接用于代码的内存.

If you do call memory_get_usage(true) you will get back the size of the heap the allocator has reserved from the system, which includes memory that has not been actually used by your code but is directly available to your code.

当您的脚本需要的内存比分配器已可用的更多时,后者将在操作系统中保留另一个较大的块,您会看到memory_get_usage(true)急剧上升,而memory_get_usage()可能只会增加几个字节.

When your script needs more memory than what is already available to the allocator, the latter will reserve another big chunk from the OS and you will see memory_get_usage(true) jump up sharply while memory_get_usage() might only increase by a few bytes.

分配器用来决定何时分配多少内存以及在编译时将其分配给PHP的确切策略;您将必须编辑源代码并编译PHP才能更改此行为.

The exact strategy the allocator uses to decide when and how much memory to allocate is baked into PHP at compilation time; you would have to edit the source and compile PHP to change this behavior.

这篇关于空的PHP脚本上的PHP memory_get_usage()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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