php laravel 内存限制错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29875730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:26:46  来源:igfitidea点击:

Php laravel memory limit error

phpmysqllaraveltransactionsout-of-memory

提问by Serghei

I am making currently migration from one database to another, project is on laravel so I am creating laravel command for this. I have one table with about 700000 records. I have created function with LIMIT and transactions to optimize query but still getting out of memory error from PHP. Here is my code:

我目前正在从一个数据库迁移到另一个数据库,项目在 laravel 上,所以我为此创建了 laravel 命令。我有一张大约有 700000 条记录的表。我已经使用 LIMIT 和事务创建了函数来优化查询,但仍然从 PHP 中获取内存不足错误。这是我的代码:

ini_set('memory_limit', '750M');  // at beginning of file

$circuit_c = DB::connection('legacy')->select('SELECT COUNT(*) FROM tbl_info');
$count = (array) $circuit_c[0];
$counc = $count['COUNT(*)'];
$max =  1000;
$pages = ceil($counc / $max);

    for ($i = 1; $i < ($pages + 1); $i++) {
        $offset = (($i - 1) * $max);
        $start = ($offset == 0 ? 0 : ($offset + 1));
        $infos = DB::connection('legacy')->select('SELECT * from tbl_info LIMIT ' . $offset . ', ' . $max);
        DB::connection('mysql')->transaction(function() use ($infos) {
            foreach ($infos as $info) {
                $validator = Validator::make($data = (array) $info, Info::$rules);
                if ($validator->passes()) {
                    if ($info->record_type == 'C') {
                        $b_user_new = Info::create($data);
                        unset($b_user_new);

                    }
                }
                unset($info);
                unset($validator);
            }
        });
        unset($infos);
    }

Error is this:

错误是这样的:

user@lenovo /var/www/info $ php artisan migratedata
PHP Fatal error:  Allowed memory size of 786432000 bytes exhausted (tried to allocate 32 bytes) in /var/www/info/vendor/laravel/framework/src/Illuminate/Database/Grammar.php on line 75

Error is show after importing about 50000 records.

导入大约 50000 条记录后显示错误。

回答by Margus Pala

There is kind of a "memory leak" in here. You need to find out which of the variables is hogging all of this memory. Try this function to debug and see which variable keep on growing constantly

这里有一种“内存泄漏”。您需要找出哪些变量占用了所有这些内存。试试这个功能调试看看哪个变量不断增长

function sizeofvar($var) {
  $start_memory = memory_get_usage();
  $tmp = unserialize(serialize($var));
  return memory_get_usage() - $start_memory;
}

Once you know what variable is taking all the memory then you can start implementíng appropriate measures.

一旦您知道哪个变量占用了所有内存,您就可以开始实施适当的措施。

回答by Serghei

Found the answer, laravel caches all queries, so just: DB::connection()->disableQueryLog();

找到了答案,laravel 缓存了所有查询,所以只需: DB::connection()->disableQueryLog();