LoginSignup
6
2

More than 5 years have passed since last update.

CircleCI2.0でphp.iniのmemory_limitを変更しても「Allowed memory size of xxx bytes exhausted (tried to allocate xxx bytes)」エラーが出てしまうときの解決法

Posted at

CircleCI2.0でのPHPアプリケーションのビルドで、あるとき「Allowed memory size of xxx bytes exhausted (tried to allocate xxx bytes)」というエラーがPHPUnitのテストで発生するようになりました。
そのときかなりハマったので備忘録として残します。

TL;DR

MySQLのmax_allowed_packetが足りていないのが原因で、MySQLのDockerイメージに引数でmax_allowed_packetの設定値を渡すとエラーが解消しました。

試したけど効果なかったもの

この手のエラーが出たときは、大体PHPのmemory_limitの値が低いことが原因なので、php.iniのmemory_limitを上書きするようにしました。
まずはiniの場所がわからないのでrun: php -i でphpinfoを出力しました。

省略...

Configuration File (php.ini) Path => /usr/local/etc/php
Loaded Configuration File => (none)
Scan this dir for additional .ini files => /usr/local/etc/php/conf.d

省略...

とあるので、/usr/local/etc/php/conf.dにファイルを追加します。

    - run: sudo sh -c "echo \"memory_limit = 2048M\" > /usr/local/etc/php/conf.d/memory.ini"
    - run: php -i | grep memory_limit

これでmemory_limitが2GBになりましたが、同じエラーが出ます。
プログラムがmemory_limitを書き換えているのかと思い、処理の直前にini_set('memory_limit', '2048M') を入れましたがメッセージは変わりません。

Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 806912 bytes) ...

と出ます。

memory_limitを変更したらAllowed memory size of xxx bytes exhaustedのxxxの値が変わるはずなのに変わらないのはおかしいと思い、他にこのような問題がないか調べてみると、stackoverflow

I had the same problem. No matter how much I was increasing memory_limit (even tried 4GB) I was getting the same error, until I figured out it was because of wrong database credentials setted up in .env file

というのがありました。
DBの接続情報が悪かったようでそこを直すと動作したそうです。
ここでMySQLの設定が原因なのかと疑いはじめました。

あらためてエラーが出るようになったコミットを見てみるととても長い文字列をDBに登録しており、MySQLが受け付けることができるサイズを超えたのではと思い、以下の方法でmax_allowed_packetを変更してみました。

config.yml
version: 2
jobs:
  build:
    docker:
      # specify the version you desire here
      - image: circleci/php:7.2-apache-node-browsers
      - image: circleci/mysql:5.7
        command: [--max_allowed_packet=32M]

めでたくエラーが解消されてビルドが通るようになりました!
エラーメッセージが適切でなかったためだいぶハマってしまったのでこの経験が誰かの役に立てば幸いです。

6
2
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
6
2