angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

ng serve uses a lot of CPU %

Open areijngoudt opened this issue 10 years ago • 29 comments

Mac OSX El Capitan

Versions.

angular-cli: 1.0.0-beta.17 node: 5.7.1 os: darwin x64

angular-cli uses about 85% CPU when I only use ng serve during development, also when I'n not changing code but just use the webapp in a browser.

areijngoudt avatar Oct 17 '16 14:10 areijngoudt

I experienced the same thing here. Sitting idle, with nothing changing, approximately one CPU core burns 100% in ng serve. Latest node, latest OSX, CLI beta 17.

kylecordes avatar Oct 17 '16 20:10 kylecordes

npm i fsevents should solve this.

mseemann avatar Oct 17 '16 21:10 mseemann

Not working on Windows.

masaanli avatar Oct 18 '16 10:10 masaanli

installing fsevents solves this issue on mac. Thanks @mseemann

areijngoudt avatar Oct 31 '16 07:10 areijngoudt

I don't think this issue should be closed. Manually installing fsevents can not be the longterm solution.

rryter avatar Oct 31 '16 08:10 rryter

Does it make sense to add fsevents to the dependencies of @angular/cli?

benyaminshoham avatar Mar 01 '17 10:03 benyaminshoham

@mseemann Thanks a lot!

msnatu avatar Apr 04 '17 18:04 msnatu

fsevents only work for mac, but what about windows and linux, in my case what is literally eating is my ram, ng process consumes 1.2GB which i consider a lot for a process like that one

jlberrocal avatar Jun 07 '17 14:06 jlberrocal

Jose, please keep in mind that node is GC'd so it will happily use available memory and won't collect aggressively until pressured by the OS which doesn't happen if you have a lot of RAM ;-) So unless ng is eating all your memory and your OS starts paging other stuff you should be fine.

On Wed, Jun 7, 2017 at 5:38 PM, Jose Berrocal [email protected] wrote:

fsevents only work for mac, but what about windows and linux, in my case what is literally eating is my ram, ng process consumes 1.2GB which i consider a lot for a process like that one

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/2748#issuecomment-306815246, or mute the thread https://github.com/notifications/unsubscribe-auth/AAH8N0Lg5SAAoyQVRdvVPBVsaZNKX8wiks5sBrX2gaJpZM4KYq8B .

JohannesRudolph avatar Jun 07 '17 16:06 JohannesRudolph

have you tried using task manager to lower the affinity / priority of the process?

Almenon avatar Sep 22 '17 03:09 Almenon

@Almenon 's solution to lower the affinity/priority of the process has been working for me. However, it is kinda tedious to do every time I run ng serve.

If I don't manually lower the affinity/priority of the process, my CPU usage is at 99%.

Running on windows 8

chestercharles avatar Nov 15 '17 17:11 chestercharles

@rryter also, if I add it to my package.json every build not happening under Mac OS will fail:

npm ERR! notsup Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

trumbitta avatar Nov 27 '17 11:11 trumbitta

I ended up adding the following little script to my postinstall:

const isMac = /^darwin/.test(process.platform);

if (isMac === true) {
  const process = require('child_process');
  process.execSync('npm install fsevents --no-save');
}

trumbitta avatar Nov 27 '17 14:11 trumbitta

Running under debian, using the ng serve --poll command causes my cpu to spike to %300 (3coresx100)

rightisleft avatar Dec 07 '17 00:12 rightisleft

this is very useful

smallg avatar Mar 29 '18 13:03 smallg

Can somebody explain why fsevents solves this issue? Posting a solution without an explanation isn't helpful.

azcdev avatar May 12 '18 18:05 azcdev

What is solution on linux ?

sonjaerock avatar May 31 '18 07:05 sonjaerock

@alanchavez88 fsevents allows the process to subscribe to File System EVENTS.

I can only assume angular-cli has logic built in that makes use of this if it's available.

This means that angular-cli no longer has to poll the actual files to see if changes occurred, it simply waits for OSX to notify it of a file change.

EDIT: Just to confirm, fsevents calmed down my CPU as well. No more hand-warmer.

PyroSA avatar Jun 05 '18 15:06 PyroSA

@alanchavez88 further to @PyroSA reply: it might have to do with chokidar, which is used by karma and the @angular/compiler-cli for watching files for changes. Chokidar is a wrapper around different ways to watch for files and fsevents is one of those. Chokidar has fsevents as an optional dependency - if it's present it will use it otherwise it has fallbacks. So if nothing else installs fsevents, chokidar goes with one of the alternatives and that eats up CPU because of polling.

https://www.npmjs.com/package/chokidar#performance

You can add "fsevents" to "optionalDependencies" in package.json to persist this change.

electrichead avatar Jun 09 '18 16:06 electrichead

For anyone running on Linux, I've been running my build and serve with a lower priority. It's really helped with ng hogging the CPU.

You can try: $ nice -10 ng <build, search> --watch true or poll N which appears to do the trick.

This should also work on other *NIX OS's

pattersongp avatar Jul 06 '18 14:07 pattersongp