I currently have a PHP CLI script using Zend Framework extensively which seems to be using a ever larger amount of memory as it runs. It loops through a large set of models retrieved from a database in batches of 1000. Calls to memory_get_usage()
show that the memory usage of the script is always increasing.
我目前有一个广泛使用Zend Framework的PHP CLI脚本,它似乎在运行时使用了更大量的内存。它循环遍历从数据库中检索的大量模型,批量为1000.调用memory_get_usage()表明脚本的内存使用量总是在增加。
This is despite making sure that I'm unsetting the model after every iteration and actually using array_shift()
to reduce the size of the array of models on every iteration.
尽管确保我在每次迭代后都没有设置模型,并且实际上使用array_shift()来减少每次迭代时模型数组的大小。
My question is, in PHP is there a way of discovering the size-in-memory of a variable so I can track what's growing?
我的问题是,在PHP中是否有一种方法可以发现变量的内存大小,以便跟踪正在增长的内容?
4 个解决方案
#1
3
i don't have a solution to check the size of every variable, but if you use doctrine its probably the reason
我没有一个解决方案来检查每个变量的大小,但如果你使用doctrine它可能是原因
you need to use
你需要使用
$Elem->free(true);
another thing is to upgrade to 5.3 (if you dont do it yet), the garbage collector of 5.3 is better
另一件事是升级到5.3(如果你还没有这样做),5.3的垃圾收集器更好
#2
3
No. You are likely looking for memory that is not freed, e.g. you unlinked a variable or deleted a reference and the garbage collector did not yet release the associated block in memory.
不。您可能正在寻找未被释放的内存,例如你取消了一个变量或删除了一个引用,垃圾收集器还没有在内存中释放相关的块。
You could try Zend Server 5 (you need the commercial version though) to mem-profile your application. It has code tracing. I dont know if this would allow you to spot memory leaks though.
你可以尝试Zend Server 5(你需要商业版)来mem-profile你的应用程序。它有代码跟踪。我不知道这是否会让你发现内存泄漏。
Also see:
- What's new in PHP V5.2, Part 1: Using the new memory manager
- Memtrack (PECL)
PHP V5.2中的新功能,第1部分:使用新的内存管理器
#3
0
I don't know how accurate it is, but I got a number by using apc_add('variable_name', $var);
. I then go to my apc.php
under user cache entries and look at the size column.
我不知道它有多准确,但是我使用apc_add('variable_name',$ var);得到了一个数字。然后我转到用户缓存条目下的apc.php并查看size列。
Of course for this to work you need to have APC installed and running. :-)
当然,要实现这一点,您需要安装并运行APC。 :-)
#4
-2
Here is a code snippet I found at weberdev
这是我在weberdev找到的代码片段
<?php
function array_size($a){
$size = 0;
while(list($k, $v) = each($a)){
$size += is_array($v) ? array_size($v) : strlen($v);
}
return $size;
}
?>
It gets the size of the given array in bytes. Is this what you meant?
它以字节为单位获取给定数组的大小。这是你的意思吗?
#1
3
i don't have a solution to check the size of every variable, but if you use doctrine its probably the reason
我没有一个解决方案来检查每个变量的大小,但如果你使用doctrine它可能是原因
you need to use
你需要使用
$Elem->free(true);
another thing is to upgrade to 5.3 (if you dont do it yet), the garbage collector of 5.3 is better
另一件事是升级到5.3(如果你还没有这样做),5.3的垃圾收集器更好
#2
3
No. You are likely looking for memory that is not freed, e.g. you unlinked a variable or deleted a reference and the garbage collector did not yet release the associated block in memory.
不。您可能正在寻找未被释放的内存,例如你取消了一个变量或删除了一个引用,垃圾收集器还没有在内存中释放相关的块。
You could try Zend Server 5 (you need the commercial version though) to mem-profile your application. It has code tracing. I dont know if this would allow you to spot memory leaks though.
你可以尝试Zend Server 5(你需要商业版)来mem-profile你的应用程序。它有代码跟踪。我不知道这是否会让你发现内存泄漏。
Also see:
- What's new in PHP V5.2, Part 1: Using the new memory manager
- Memtrack (PECL)
PHP V5.2中的新功能,第1部分:使用新的内存管理器
#3
0
I don't know how accurate it is, but I got a number by using apc_add('variable_name', $var);
. I then go to my apc.php
under user cache entries and look at the size column.
我不知道它有多准确,但是我使用apc_add('variable_name',$ var);得到了一个数字。然后我转到用户缓存条目下的apc.php并查看size列。
Of course for this to work you need to have APC installed and running. :-)
当然,要实现这一点,您需要安装并运行APC。 :-)
#4
-2
Here is a code snippet I found at weberdev
这是我在weberdev找到的代码片段
<?php
function array_size($a){
$size = 0;
while(list($k, $v) = each($a)){
$size += is_array($v) ? array_size($v) : strlen($v);
}
return $size;
}
?>
It gets the size of the given array in bytes. Is this what you meant?
它以字节为单位获取给定数组的大小。这是你的意思吗?