小编Bri*_*son的帖子

Laravel/Eloquent内存泄漏重复检索相同的记录

我正在尝试编写一个laravel函数,它从一个数据库中获取大量记录(100,000+)并将其放入另一个数据库中.为此,我需要查询我的数据库,看看用户是否已经存在.我反复调用这段代码:

$users = User::where('id', '=', 2)->first();
Run Code Online (Sandbox Code Playgroud)

然后在那之后发生几百次,我的内存耗尽.所以,我做了一个极简主义的例子,它耗尽了所有可用的内存,它看起来像这样:

<?php

use Illuminate\Console\Command;

class memoryleak extends Command
{
    protected $name = 'command:memoryleak';
    protected $description = 'Demonstrates memory leak.';

    public function fire()
    {
        ini_set("memory_limit","12M");

        for ($i = 0; $i < 100000; $i++)
        {
            var_dump(memory_get_usage());
            $this->external_function();
        }
    }

    function external_function()
    {
        // Next line causes memory leak - comment out to compare to normal behavior
        $users = User::where('id', '=', 2)->first();

        unset($users);
        // User goes out of scope at the end of this function
    }
} …
Run Code Online (Sandbox Code Playgroud)

laravel laravel-4

18
推荐指数
1
解决办法
1万
查看次数

标签 统计

laravel ×1

laravel-4 ×1