4
2

More than 1 year has passed since last update.

rails g または rails c を実行するとCPU使用率が100%(熱くなる)になる

Last updated at Posted at 2021-10-02

事象

railsコマンドを使っているとPCが非常に熱くなりました。
再起動を行うと解消されますが、railsコマンドを使うと再び熱くなりました。
熱くなるコマンドは以下でした(後で記載しているGemfileでわかりますが、developmentに関わるコマンドが影響あるようです)。

* rails c
* rails g ~~
* bundle exec spec ~~

熱くなったのでPCの状態を次のようにして確認しました。

tarminl
% ps aux | head -n 2
USER               PID  %CPU %MEM      VSZ    RSS   TT  STAT STARTED      TIME COMMAND
username   68581 100.0  2.4  9070972 397040   ??  Rs    2:29PM   5:51.09 ruby -I /Users/username/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib -I /Users/username/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/spring-2.1.1/lib -e require 'spring/application/boot'

対象

現象が発生するのは限定的なようです。ポイントはM1プロセッサのようです。

Machine : MacBook Air(M1プロセッサ)
OS      : macOS Big Sur version 11.6
ruby    : ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin20]
rbenv   : rbenv 1.1.2
rails   : Rails 6.0.4.1

原因

springかlistenというrailsのライブラリに関わる問題だと報告があるようです。
詳細は、参考サイトのページで確認してください。

簡易的対策(コマンド)

① アプリケーションのフォルダへ移動する(アプリ名がappの場合)

% cd app

② springを停止させる

% spring stop
Spring stopped.

簡易的対策(Gem)

① Gemfile の gem 'spring' と gem 'spring-watcher-listen', '~> 2.0.0' をコメントアウトする

Gemfile
group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  # gem 'spring'
  # gem 'spring-watcher-listen', '~> 2.0.0'
end

② bundle install コマンドを実行する

恒久的な対策

① Gemfile の gem 'listen', '>= 3.0.5', '< 3.2' gem 'listen', '>=3.0.5', '<4.0' に変更する

Gemfile
group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
    gem 'listen', '>=3.0.5', '<4.0'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

② bundle install コマンドを実行する

参考サイト

Github - rails/spring | Pegging CPU core on Apple M1 processor #636

その他

私は、どうしても簡易的対策しか打てない環境でした。
そのため、気づくとどうしてもPCが熱くなります。
そのため、CPU使用率が高くなっていることを通知するソースコードを製作してみました。

ryohei-takasugi/rails-cpu-checker

10秒周期でPSコマンドを実行しています。 PSコマンドのCPU使用率をチェックして、60%以上になっているプロセスを見つけます。 また、それのプロセスが「spring/application/boot」である場合に次の動作をします。

  • 現在日時を表示します。
  • PSコマンドの取得内容を表示します。
  • 音声で確認を促します。

追記 

2021/10/6

Railsを同じく勉強している方から「Spring」って何?と聞かれました。
そういえば、調べていたのに書いていなかったため、追記したいと思います。
GitHubの説明を読む限り、Railsコマンドを高速に起動するため、必要となるファイルを事前に読むこんでおくライブラリのようです。
そのため、導入しなくても動作は変わらず、起動が遅くなるだけで済みます。

Github - rails/spring

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