PHP性能:内存密集型变量 [英] PHP performance: Memory-intensive variable
问题描述
假设一个多维关联数组,当使用print_r()
打印为文本时,将创建一个470 KiB文件.如果每个用户使用的变量不同,是否可以假设所讨论的变量每个实例占用服务器MiB的一半?因此,如果同时有1000位用户访问服务器,将消耗近一半的GiB内存?
Supposing a multidimensional associative array that, when printed as text with print_r()
, creates a 470 KiB file. Is it reasonable to assume that the variable in question takes up half a MiB of server memory per instance if it is different for each user? Therefore if 1000 users hit the server at the same time almost half a GiB of memory will be consumed?
谢谢.
推荐答案
IBM上有一篇关于该主题的出色文章: http://www.ibm.com/developerworks/opensource/library/os-php-v521/
There is an excellent article on this topic at IBM: http://www.ibm.com/developerworks/opensource/library/os-php-v521/
更新
原始页面已被删除,目前仍保留JP版本 https://www.ibm.com/developerworks/jp/opensource/library/os-php-v521/
The original page was taken down, for now the JP version is still there https://www.ibm.com/developerworks/jp/opensource/library/os-php-v521/
基本说明,您可以使用memory_get_usage()
检查脚本当前占用的内存量:
Basic takeaways form it are that you can use memory_get_usage()
to check how much memory your script currently occupies:
// This is only an example, the numbers below will differ depending on your system
echo memory_get_usage () "\ n";. // 36640
$ A = str_repeat ( "Hello", 4242);
echo memory_get_usage () "\ n";. // 57960
unset ($ a);
echo memory_get_usage () "\ n";. // 36744
此外,您还可以使用memory_get_peak_usage()
检查脚本的峰值内存使用情况.
Also, you can check the peak memory usage of your script with memory_get_peak_usage()
.
作为对您的问题的回答:print_r()
是数据的表示形式,其文本和格式过大.占用的内存本身将少于print_r()
的字符数.多少取决于数据.您应该像上面的示例一样检查它.
As an answer to your questions: print_r()
is a representation of data which is bloated with text and formatting. The occupied memory itself will be less than the number of characters of print_r()
. How much depends on the data. You should check it like in the example above.
无论您得到什么结果,它都是针对执行脚本的每个用户的,所以是的-如果同时有1000个用户请求该脚本,则将需要该内存.
Whatever result you get, it will be for each user executing the script, so yes - if 1000 users are requesting it at the same time, you will need that memory.
这篇关于PHP性能:内存密集型变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!