+ /**
+ * Check runtime memory usage, possibly triggering a graceful shutdown
+ * and thread respawn if we've crossed the soft limit.
+ *
+ * @param boolean $respawn if false we'll shut down instead of respawning
+ */
+ protected function checkMemory($respawn=true)
+ {
+ $memoryLimit = $this->softMemoryLimit();
+ if ($memoryLimit > 0) {
+ $usage = memory_get_usage();
+ if ($usage > $memoryLimit) {
+ common_log(LOG_INFO, "Queue thread hit soft memory limit ($usage > $memoryLimit); gracefully restarting.");
+ if ($respawn) {
+ $this->requestRestart();
+ } else {
+ $this->requestShutdown();
+ }
+ } else if (common_config('queue', 'debug_memory')) {
+ $fmt = number_format($usage);
+ common_log(LOG_DEBUG, "Memory usage $fmt");
+ }
+ }
+ }
+