php内存泄漏与GC无关?
我有一个 php 脚本,它获取图像,处理它,然后将新图像写入文件。我正在使用 imagick/imagemagick 和 php 5.3.8 以及 fastcgi。阅读完周围内容后,我认为垃圾收集功能可能会有所帮助,但它并没有阻止 TOP 中 php 的内存使用量增长到三位数。我曾经在 cron 中运行这个脚本。
<?php
var_dump(gc_enabled()); // true
var_dump(gc_collect_cycles()); // number comes out to 0
?>
不知道该怎么办。到目前为止,唯一有助于控制 php 的方法是每小时左右执行一次“service php-fpm reload”。使用 imagick 作为共享扩展而不是静态编译会有帮助吗?任何建议或见解将不胜感激。
I have a php script which takes an image, processes it and then writes the new image to file. I'm using imagick/imagemagick with php 5.3.8 with fastcgi. After reading around I thought maybe the garbage collecting function might help but it hasn't stopped php's memory usage in TOP from growing to triple digits. I used to run this script in cron.
<?php
var_dump(gc_enabled()); // true
var_dump(gc_collect_cycles()); // number comes out to 0
?>
Not sure what to do. So far the only thing that helps keep php in check is by doing a 'service php-fpm reload' every hour or so. Would using imagick as a shared ext instead of statically compiled one help? Any suggestions or insight is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两种选择:
关于内置库与外部库的说明。我还没有玩过图像魔法的这方面,但我在 GD 中看到过它。当您使用外部库时,您从 PHP 函数获得的内存值要低得多,但实际内存使用量几乎相同。
Two options:
A note about built in vs external libraries. I haven't played with this aspect of image magick, but I saw it with GD. You get a much lower memory value from the PHP functions when you're using the external library, but the actual memory usage is nearly equal.
检查内存泄漏的一个好的开始是 valgrind。
A good start to check for memory leaks is valgrind.
如果 PHP 有大量可用内存可供使用,那么它不会费心去擦除内存,因为它认为不需要。当它使用更多内存时,或者如果其他应用程序开始使用更多内存,那么它将清除它可以清除的内存。
您可以通过将变量设置为 NULL 来强制清除变量的内存,但建议使用 unset(),因为您不需要强制它使用更少的内存,因为 PHP 会自行清理。
但否则,需要一段代码来回答您的问题。
If PHP has lots of available memory to use then it doesn't bother to wipe the memory since it doesn't think it needs to. As it uses more, or if other applications start to use more memory, then it will clear the memory of what it can.
You can force the memory to be cleared for a variable by setting it to NULL, but unset() is recommended because you shouldn't need to force it to use less memory as PHP will clean up by itself.
But otherwise, a snippet of your code is required to answer your question.