简体   繁体   English

Angular CLI - ng serve - 节点进程的高 CPU 使用率

[英]Angular CLI - ng serve - high cpu usage from node process

I am struggling with an issue where on running any angular app locally the node process linked to ng serve is using well over 100% of my CPU core's.我正在努力解决一个问题,即在本地运行任何 angular 应用程序时,链接到ng serve的节点进程使用了我 100% 以上的 CPU 内核。

My current environment is:我目前的环境是:

Angular CLI: 7.3.10 Node: 11.15.0 OS: darwin x64 (Mac OS X Catalina 10.15.4) Angular: 7.2.16 Angular CLI:7.3.10 节点:11.15.0 操作系统:darwin x64 (Mac OS X Catalina 10.15.4) Angular:7.2.16

I have come across numerous posts and issues about this where the common solutions appear to be to install fsevents for Mac OS issues.我遇到过很多关于此的帖子和问题,其中常见的解决方案似乎是为 Mac OS 问题安装fsevents

Things I have tried:我尝试过的事情:

  • Installing XCode Command Line Tools for node-gyp to rebuild fsevents after its installed安装 XCode node-gyp gyp 的命令行工具以在安装后重建fsevents
  • Install the latest version of fsevents (2.1.3) as an older version is specified as an optional dependency with angular-cli (1.2.13)安装最新版本的fsevents (2.1.3),因为旧版本被指定为 angular-cli (1.2.13) 的可选依赖项
  • Using a different node version, tried 12.13.0 and deleting node_modules and doing a clean install.使用不同的节点版本,尝试 12.13.0 并删除 node_modules 并进行全新安装。 Also tried latest fevents version again还再次尝试了最新的 fevents 版本
    • Incase its relevant I use nvm also万一它相关我也使用nvm
  • Tried npm rebuild尝试过npm rebuild
  • Upgrading one of my apps from Angular 7 to 9, still same behaviour after clean install of dependencies, ensuring fsevents is installed and trying latest version too将我的一个应用程序从 Angular 7 升级到 9,全新安装依赖项后行为仍然相同,确保安装了 fsevents 并尝试最新版本

No matter what I try, I keep observing high cpu usage, I have seen it creep up too 300-400% at times.无论我尝试什么,我都会一直观察到高 CPU 使用率,我看到它有时会爬升 300-400%。

This is really hampering my development and I am hoping someone may have some bright ideas I could try out.这确实阻碍了我的发展,我希望有人可以提出一些我可以尝试的好主意。

Failing that, I am wondering if I should try uninstalling node completely, getting rid of nvm and starting from scratch.如果失败了,我想知道我是否应该尝试完全卸载node ,摆脱nvm并从头开始。

In Angular development context;在Angular开发上下文中; transpilation/compilation causes CPU spike.转译/编译会导致 CPU 峰值。 When this happens too frequent, your system is in trouble.当这种情况发生得太频繁时,您的系统就会出现问题。

There are certain ways to relieve the pain a little;有一些方法可以稍微减轻疼痛;

  • Turn off file change detection / live-reload / auto compilation entirely完全关闭文件更改检测/实时重新加载/自动编译

ng serve --live-reload false or ng serve --no-live-reload etc. depending on your Angular CLI version ng serve --live-reload falseng serve --no-live-reload等,具体取决于您的 Angular CLI 版本

  • Have a better change detection, react only when really needed有更好的变化检测,只有在真正需要时才做出反应

npm install fsevents

npm rebuild fsevents

npm serve

  • Forget change detection, check changes based on a time interval忘记更改检测,根据时间间隔检查更改

ng serve --poll [ms]

CPU management is tricky, there can be many reasons for this problem. CPU管理很棘手,这个问题可能有很多原因。 These are only a few possibilities closely related to Angular development.这些只是与 Angular 开发密切相关的几种可能性。 I hope this answer provides some options to try to the ones having the same trouble.我希望这个答案提供了一些选项来尝试那些有同样问题的人。

Maybe you have similar setup for you hot/live reloading on Docker such as ng serve --host 0.0.0.0 --poll 1 .也许您对Docker的热/实时重新加载有类似的设置,例如ng serve --host 0.0.0.0 --poll 1 The poll value is problematic, change it on something larger, for example 2000 , such that ng serve --host 0.0.0.0 --poll 2000 . poll值有问题,将其更改为更大的值,例如2000 ,这样ng serve --host 0.0.0.0 --poll 2000

Now, you'll loose (almost) real-time hot/live reloading, but you'll save your CPU/battery and having 2 seconds delay before your app will refresh the changed code.现在,您将失去(几乎)实时热/实时重新加载,但您将节省 CPU/电池并在您的应用程序刷新更改的代码之前有 2 秒的延迟。

it seems i managed to resolve the problem: delete the /node_module and install again看来我设法解决了问题:删除 /node_module 并重新安装

in my case that helped i found the advise here:https://gitmemory.com/issue/angular/angular-cli/14748/501608887就我而言,这有助于我在这里找到建议:https://gitmemory.com/issue/angular/angular-cli/14748/501608887

After a lot of digging, I found the below option which works well for me.经过大量挖掘,我发现以下选项对我来说很有效。 Before that every time I encounter with JavaScript heap out of memory issue.在此之前,我每次遇到 JavaScript 堆出 memory 问题。 Tried with custom NODE memory, but noting works for me rather than below one.尝试使用自定义节点 memory,但注意对我有用,而不是低于一个。

ng serve --source-map=false

and

ng build --source-map=false

Works like a charm.奇迹般有效。

If you use Yarn to install packages, its lockfile may have locked in an older version of fsevents which has known issues with node-pre-gyp on Apple Silicon.如果您使用 Yarn 安装软件包,它的锁定文件可能已锁定在旧版本的fsevents中,该版本已知在 Apple Silicon 上存在node-pre-gyp gyp 问题。

https://github.com/nodejs/node-gyp/issues/2296 https://github.com/nodejs/node-gyp/issues/2296

Simply deleting yarn.lock , and running yarn again, should attempt to install newer versions of the fsevents dependency, which does not have that issue.只需删除yarn.lock并再次运行yarn ,应该会尝试安装不存在该问题的fsevents依赖项的较新版本。

This worked with Angular 11.这适用于 Angular 11。

Setting the flag --watch=false is what reduced CPU usage of my angular process from 50% to 0%:设置标志--watch=false将我的 angular 进程的 CPU 使用率从 50% 降低到 0%:

ng serve --configuration=dev --watch=false

Try using below command尝试使用以下命令

node --max_old_space_size=8048 ./node_modules/@angular/cli/bin/ng serve

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM