LoginSignup
9
8

More than 5 years have passed since last update.

Cakephp3 メモリリミットエラー

Last updated at Posted at 2016-07-12

直面した問題

CakePHP3を使ったシステムのバッチ処理でメモリーリミットエラーが発生

Fatal error: Allowed memory size of ****** bytes exhausted

原因

数十万件のレコードを更新するバッチ処理で
かつ、開発環境にてデバッグモードで実行していたので
SQLのクエリログが大量に残り、メモリを圧迫していた

解決策

設定ファイルでクエリログを無効にする

config/app.php
[
    'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'localhost',
            'username' => 'root',
            'password' => 'admin',
            'database' => 'databasename',
            'encoding' => 'utf8',
            'timezone' => '',
            'cacheMetadata' => true,
            'log' => false,// ← ここ!!
            'quoteIdentifiers' => true,
        ],
]

または、Shellの時だけ無効にする

※自分はこちらで解決しました

Shell/AppShell.php
class AppShell extends Shell
{

    public function initialize()
    {
        parent::initialize();
        ConnectionManager::get('default')->logQueries(false);
    }
}

結果

Before

2016-07-12 12:18:46 Info: 0件目:メモリ使用量 36.333709716797MB
2016-07-12 12:19:06 Info: 10000件目:メモリ使用量 61.611282348633MB
2016-07-12 12:19:30 Info: 20000件目:メモリ使用量 90.64225769043MB
2016-07-12 12:19:50 Info: 28000件目:メモリ使用量 113.06938171387MB

After

2016-07-12 12:31:50 Info: 0件目:メモリ使用量 35.958770751953MB
2016-07-12 12:32:16 Info: 10000件目:メモリ使用量 35.958770751953MB
2016-07-12 12:32:41 Info: 20000件目:メモリ使用量 35.958770751953MB
2016-07-12 12:33:01 Info: 28000件目:メモリ使用量 35.958770751953MB

1MBも増えなくなりました

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8