简体   繁体   中英

A Daemon on the Rocks

I am writing a daemon in PHP. I did not take a OS class in college. So, I'm wondering, what are the server/other statistics that I need to be looking at to make sure my Daemon is not consuming too much system resources and will be able to scale when there are more mysql records. Basically, my daemon is processing a bunch of mysql table rows.

For example, I understand I need to see how long the daemon is taking to process a certain number of rows, and the amount of memory it is using. But, how do I determine if it is leaking memory? Also, what other system parameters should I be judging the daemon by?

But, how do I determine if it is leaking memory?

The stuff you're asking about here has little to do with the operating system. You're right to be concerned about memory usage. A proper answer to this question goes way beyond the scope of a post here but you might want to start by looking at how reference counting works for memory management, and make sure you've got the circular reference checker configured in your PHP installation. The plot thickens when you discover that the mysql client blocks PHP while it is running and ignores PHP's memory limits - so if you fetch too large a result set, you won't know about it until mysql_query returns and your code falls over: always use LIMIT in queries (or PK selection) and for preference run the daemon under a watchdog. Test using varying memory limits lower than you intend to use in production.

Note that PHP will only start making more memory available to itself via garbage collection when it thinks it's running out of memory.

Write lots of stuff to log files!

Depending on how you are going to execute the Daemon fire up top in linux and then have it process a lot of rows (100k+, or something that would take about 30 seconds to execute) of what you anticipate. Look to see how fast memory usage increases: with small tasks it happens too fast, you need the running process.

Then be sure that you unset($objectOrString), close all files and connections to the database as soon as you are done using them: this will help.

Again, depending on what this file will be doing you may want to let it terminate and use a cron job to start it up agian so that PHP can run its garbage collection for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM