incognito2
incognito2

Reputation: 1106

Memory leakage in php unrelated to GC?

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.

Upvotes: 1

Views: 481

Answers (3)

preinheimer
preinheimer

Reputation: 3722

Two options:

  • Farm out the work through gearman or the like to a script that will die completely. Generally I'll run my workers through a certain number of jobs, then have them die. They'll be restarted by supervisor in my setup so it's not a problem. The death after N requests just avoids memory issues.
  • As of 5.4 this might help: https://www.php.net/manual/en/function.apache-child-terminate.php

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.

Upvotes: 1

Alasdair
Alasdair

Reputation: 14151

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.

Upvotes: 0

Cybercartel
Cybercartel

Reputation: 12592

A good start to check for memory leaks is valgrind.

Upvotes: 0

Related Questions