允许的内存大小为8589934592字节已用尽 [英] Allowed memory size of 8589934592 bytes exhausted

查看:139
本文介绍了允许的内存大小为8589934592字节已用尽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel的日程安排上遇到了问题.

I encountering issue on my schedule on Laravel.

我看到有关于此错误的重复问题,但这些并不能解决我的问题

I see that there is duplicate questions about this error but these don't fix my issue

root @ trafficshield:/var/www/vhosts/trafficshield.tools/httpdocs# /opt/plesk/php/7.1/bin/php工匠时间表:运行运行时间表 命令:Closure ^ [[15〜PHP致命错误:允许的内存大小为 8589934592字节已用尽(尝试分配4096字节) /var/www/vhosts/trafficshield.tools/httpdocs/vendor/laravel/framework/src/Illum inate/Database/Eloquent/Model.php,第279行PHP

root@trafficshield:/var/www/vhosts/trafficshield.tools/httpdocs# /opt/plesk/php/7.1/bin/php artisan schedule:run Running scheduled command: Closure ^[[15~PHP Fatal error: Allowed memory size of 8589934592 bytes exhausted (tried to allocate 4096 bytes) in /var/www/vhosts/trafficshield.tools/httpdocs/vendor/laravel/framework/src/Illum inate/Database/Eloquent/Model.php on line 279 PHP

此计划旨在统计我们所有服务人员的全部拜访次数.

This schedule aim to count all the visits of all campains on our service.

    $schedule->call(function () {
        $campaigns = Campaign::all();
        foreach ($campaigns as $campaign) {
            $campaign->denied_visits = $campaign->visitsDenied->count();
            $campaign->allowed_visits = $campaign->visitsAllowed->count();
            $campaign->save();
        }
    })->everyFiveMinutes();

如何避免发生此问题而更改PHP代码?

How can I change the PHP code the avoid this issue ?

配置:memory_limit : 8G

在此先感谢您的帮助.

推荐答案

Allowed memory size of 8589934592 bytes exhausted

这种错误是由内存中的大量数据引起的,因此解决此问题的方法是编写不太繁重的内存脚本.通过更改memory_limit,我们只能得到一个时间上的修正,因为当数据增长时,它会返回.

This kind of errors are caused by a big amount of data in memory, so the way to fix it is write a less heavy memory script. By changing memory_limit we only get a temporally fix because when our data grows it comes back.

    $campaigns = Campaign::all(); //at this point you are pulling the whole Campaigns table to memory.. and you pull some extra rows after that increasing even more the memory use

正如我说的,Elloquent执行此类任务的效率很低,所以让我们像mysqli一样逐个获取数据库行:

As I said, Elloquent is poor efficient doing this kind of task so let's fetch the database rows as mysqli used to do, one by one:

  $db = DB::connection()->getPdo(); //get a database connection instance

  $main_query_sql = "SELECT * FROM Campaigns"; //write our query 
  $main_query = $db->prepare($main_query_sql); //prepare it to get executed
  $main_query->execute(); //execute our query


  $visits_denied_sql = "SELECT COUNT(*) as total FROM VISITS WHERE DENIED = 1 AND CAMPAIGN_ID ="; //let's prepare our aggregate queries

  $visits_allowed_sql = "SELECT COUNT(*) as total FROM VISITS WHERE DENIED = 0 AND CAMPAIGN_ID ="; //I just assumed them so change it as you need

  $visits_denied = null;
  $visits_allowed = null;

  while($campaign = $main_query->fetch()){ //fetch our rows one by one
      //now we are getting an associative array from the db instead of a model so we cannot use it as a Laravel Model (we can't use ->save() method or eager loading)
      $visits_denied = $db->prepare($visits_denied_sql.$campaign['id']);
      $visits_denied = $visits_denied->execute();
      $denied_visits = $visits_denied->fetch();

      $visits_allowed= $db->prepare($visits_allowed_sql.$campaign['id']);
      $visits_allowed= $visits_allowed->execute();
      $allowed_visits = $visits_allowed->fetch();

      $campaign['denied_visits'] = $denied_visits['total'];
      $campaign['allowed_visits'] = $allowed_visits['total'] ;

      //edit with the correct update sentence:
      $insert_query_sql = "UPDATE CAMPAIGNS SET allowed_visits = :visits_allowed, denied_visits = :visits_denied WHERE id = :id";
        $insert_query = $db->prepare($insert_query_sql);
        $insert_query->bindParam(':visits_allowed', $campaign['allowed_visits']);
        $insert_query->bindParam(':visits_denied', $campaign['denied_visits']);
        $insert_query->bindParam(':id', $campaign['id']);
        $insert_query->execute();

  }

在您的日程安排中尝试一下,让我知道是否有效!

Try this inside your schedule and let me know if worked!

这篇关于允许的内存大小为8589934592字节已用尽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆