The specific scenario is this: a server running beanstalkd, using the PHP libary Pheanstalk. I have a PHP worker running as a service, so it theoretically never stops running.
The initial code for the worker is just a function call made to do the work. I wanted to shift that over to OOP. This would instantiate an object for every job that was processed. I imagine that without proper clean up, this would create a memory leak.
What would be the proper way to create/manage/destroy objects for use in this kind of scenario without causing memory leaks or excess system usage?
I've run hundreds of millions of jobs through PHP workers, with SES, and Beanstalkd as the queue systems. I didn't worry about trying to keep things running forever. If you find that after a job, memory use is getting high, restart the worker. Likewise, if you've just completed your 100th or 1000th job with that worker, restart from scratch, just to clean up.
It's easy to run more workers, and it's fast to start a new one. Use it. If, while developing, plug in enough debugging so if you do find a memory leak, there's enough information there to figure out where, and then deal with it.
This is the shell script I have to keep the PHP worker going. When I
exit(98);
from the script, it recognises this and immediately restarts. I'll usually add others for planned pauses and to exit the script. Start this with whatever init-style system you may have (upstart, supervisord, etc) and the script will keep going, and restarting at will, until you decide otherwise.