我正在使用laravel 5.1并使用supervisor来监视队列作业。队列驱动程序是数据库。
[program:queue]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work database --sleep=3 --tries=1 --daemon
autostart=true
autorestart=true
user=root
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/html/storage/logs/supervisord.log
Queue Listener使用的RAM在处理完每个作业后会增加,最高可达150-200 MB。所有全局变量都指定为null。
namespace App\Jobs;
use App\Jobs\Job;
use App\Compatibility;
use App\Like;
use App\Message;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
class CalculateInteractionLike extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $userAId;
protected $userBId;
protected $gender;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($userAId, $userBId, $gender)
{
$this->userAId = $userAId;
$this->userBId = $userBId;
$this->gender = $gender;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
echo 'start CalculateInteractionLike '. memory_get_usage() . "\n";
$userArray = array();
Compatibility::where('userAId', $this->userBId)->where('userBId', $this->userAId)->update(['interactionAB'=>0.0001]);
$profiles = Compatibility::where('userAId', $this->userBId)->where('interactionAB', '>',0)->orderBy('compatibilityAB', 'desc')->take(4)->get(['compatibilityAB']);
$compatible = array();
for($i=0; $i<sizeof($profiles);$i++){
$compatible[] = $profiles[$i]->compatibilityAB;
}
$std = sizeof($compatible)>1 ? $this->_calculateStd($compatible) : 0;
$messagedProfile = Message::where('userBId', $this->userBId)->where('type', '1')->get(['userAId']);
for($i=0;$i<sizeof($messagedProfile);$i++){
Compatibility::where('userAId',$this->userBId)->where('userBId', $messagedProfile[$i]->userAId)->update(array('interactionAB' => $std));
}
$this->userAId = null;
$this->userBId = null;
$this->gender = null;
$userArray = null;
$profiles = null;
$compatible = null;
$std = null;
$messagedProfile = null;
}
private function _calculateStd($compatible){
return sqrt(array_sum(array_map([$this,"_stdSquare"], $compatible, array_fill(0,count($compatible), (array_sum($compatible) / count($compatible))))) / (count($compatible)-1));
}
private static function _stdSquare($x, $mean){
return pow($x - $mean, 2);
}
public function __destruct(){
$this->cleanup();
echo 'end CalculateInteractionLike '. memory_get_usage() . "\n";
}
public function cleanup() {
//cleanup everything from attributes
foreach (get_class_vars(__CLASS__) as $clsVar => $_) {
unset($this->$clsVar);
}
}
}
如果处理上述作业,则每次RAM增加一些。有什么想法吗?
答案 0 :(得分:1)
作为一种解决方法 - 如果你找不到内存泄漏的来源 - 你可以切换到queue:listen
而没有任何守护进程标志。
这将在每个作业之后“重启”框架,通过PHP释放所有内存。
这与Supervisor兼容,这将确保始终重新启动queue:listen
。
答案 1 :(得分:0)
你没有清理记忆的唯一地方是()
for($i=0;$i<sizeof($messagedProfile);$i++){
Compatibility::where('userAId',$this->userBId)->where('userBId', $messagedProfile[$i]->userAId)->update(array('interactionAB' => $std));
}
第一个是static(:: used),但是内部对象已经创建并开始使用 - &gt;调用函数。您是否尝试检查是否存在desctuctors并手动调用垃圾收集器?
for($i=0;$i<sizeof($messagedProfile);$i++){
Compatibility::where('userAId',$this->userBId)->where('userBId', $messagedProfile[$i]->userAId)->update(array('interactionAB' => $std));
gc_collect_cycles();
}
where(),update()等第二件事可以返回对象的引用,如果是,你想尝试:
for($i=0;$i<sizeof($messagedProfile);$i++){
$tmp = Compatibility::where('userAId',$this->userBId)->where('userBId', $messagedProfile[$i]->userAId)->update(array('interactionAB' => $std));
$tmp = null;
}
请注意php&lt; 7.0不会自动破坏在上层类中创建的类的对象。 检查一下:https://codereview.stackexchange.com/questions/57847/case-of-the-hidden-memory-leak