# CPU Usage spikes - how to identify problems

**URL:** <https://discourse.nodered.org/t/cpu-usage-spikes-how-to-identify-problems/16272>\
**Category:** General\
**Created:** [3 October 2019 19:10 UTC](https://discourse.nodered.org/t/cpu-usage-spikes-how-to-identify-problems/16272 "2019-10-03T19:10:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![deme](https://avatars.discourse-cdn.com/v4/letter/d/7993a0/32.png) [@deme](https://discourse.nodered.org/u/deme)\
**Post date:** [3 October 2019 19:10 UTC](https://discourse.nodered.org/t/cpu-usage-spikes-how-to-identify-problems/16272/1 "2019-10-03T19:10:42Z")

</div>

Hello,

my node-red sees huge (100%+) CPU usage spikes from time to time. I do have 100+ individual function nodes and \> 25 flows, however none of those are complex or justify the cpu usage.

Do you have any hints how to identify the function node or the flow responsible for those?

Regards,  
Dennis

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [3 October 2019 19:59 UTC](https://discourse.nodered.org/t/cpu-usage-spikes-how-to-identify-problems/16272/2 "2019-10-03T19:59:18Z")

</div>

If you are running on Linux try htop, which will show you which processes are using resources.

---

<div class="post-metadata">

**Author:** ![deme](https://avatars.discourse-cdn.com/v4/letter/d/7993a0/32.png) [@deme](https://discourse.nodered.org/u/deme)\
**Post date:** [3 October 2019 20:01 UTC](https://discourse.nodered.org/t/cpu-usage-spikes-how-to-identify-problems/16272/3 "2019-10-03T20:01:55Z")

</div>

it is the node-red process - question beeing, how can identify the function within node-red, which is responsible for the high load.

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [3 October 2019 20:06 UTC](https://discourse.nodered.org/t/cpu-usage-spikes-how-to-identify-problems/16272/4 "2019-10-03T20:06:51Z")

</div>

I don't think you can directly identify which function is problematic. Try eliminating functions systematically!

---

<div class="post-metadata">

**Author:** ![ryanrdetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ryanrdetzel/32/5286_2.png) [@ryanrdetzel](https://discourse.nodered.org/u/ryanrdetzel)\
**Post date:** [3 October 2019 20:19 UTC](https://discourse.nodered.org/t/cpu-usage-spikes-how-to-identify-problems/16272/5 "2019-10-03T20:19:06Z")

</div>

Yeah, I'm in the process of that too, just trial and error. I'm actually moving flows over to a new instance since I can't figure out why my old one hangs all the time.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [3 October 2019 20:37 UTC](https://discourse.nodered.org/t/cpu-usage-spikes-how-to-identify-problems/16272/6 "2019-10-03T20:37:02Z")

</div>

Hey @deme,

Very interesting question!  
Would be nice if you could **measure** the cpu of each Node-RED block, instead of having to use _trial-and-error._ But I'm not sure whether you can do that, because I 'think' it works like this:

- A Node-RED block will execute some code (synchronous) =\> I think you could (somehow) measure the CPU usage required to execute this code.
- But in that code other code could be called asynchronous =\> this code will be executed afterwards by the event loop =\> You would have to measure this CPU usage also, since it has been triggered originally by the Node-RED node.
- And that previous asynchronous code, can again call another code snippet asynchronously.
- And so on ...

So you would have to keep track somehow of all code that has been triggered by the Node-RED node, and then sum all those cpu usages to calculate the cpu usage of that Node-RED node. And this process needs to be repeated for all nodes in your flow. Then you would have a nice overview of how much cpu each of your Node-RED nodes uses.

But no idea how to measure that 🥴  
I assume you need a **CPU profiler**. For example if we have a look at the output of this (obsolete!!!) [async-profile](https://github.com/ConradIrwin/async-profile) module:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/aa3dfc23b5ba8b818668c6c4caa9bf933cfc93ff.png)

It seems to me that it keeps track of all asynchronous calls, and at the end all cpu usages are aggregated. But I have never digged into NodeJs cpu profilers...

Bart

---

<div class="post-metadata">

**Author:** ![deme](https://avatars.discourse-cdn.com/v4/letter/d/7993a0/32.png) [@deme](https://discourse.nodered.org/u/deme)\
**Post date:** [3 October 2019 21:19 UTC](https://discourse.nodered.org/t/cpu-usage-spikes-how-to-identify-problems/16272/7 "2019-10-03T21:19:04Z")

</div>

Digging a bit deeper, apparently, this topic had been discussed / worked on before:

- [Easy way to remove lag?](https://discourse.nodered.org/t/easy-way-to-remove-lag/10025/26)
- [https://github.com/node-red/node-red.github.io/wiki/Analysing-Performance](https://github.com/node-red/node-red.github.io/wiki/Analysing-Performance)

Has someone worked with one of these tools before?

- [https://nodejs.org/en/docs/guides/simple-profiling/](https://nodejs.org/en/docs/guides/simple-profiling/)
- [https://nodejs.org/en/docs/guides/diagnostics-flamegraph/](https://nodejs.org/en/docs/guides/diagnostics-flamegraph/)
- [https://github.com/RuntimeTools/appmetrics](https://github.com/RuntimeTools/appmetrics)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [3 October 2019 22:12 UTC](https://discourse.nodered.org/t/cpu-usage-spikes-how-to-identify-problems/16272/8 "2019-10-03T22:12:57Z")

</div>

Don't forget this is all running in node.js... When it gets above a certain memory limit (--max-old-space) the garbage cleaner will kick in and try to reclaim the memory. On a small processor with other things going on, this can be quite aggressive and cause spikes. Possibly... or maybe something else.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [3 October 2019 22:29 UTC](https://discourse.nodered.org/t/cpu-usage-spikes-how-to-identify-problems/16272/9 "2019-10-03T22:29:14Z")

</div>

Swapping can also cause big spikes when running off an sd-card. So you should check and get rid of anything not really necessary. Obviously, you won't want to be running a desktop so make sure you stop any desktop related services.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [19 March 2022 08:00 UTC](https://discourse.nodered.org/t/cpu-usage-spikes-how-to-identify-problems/16272/10 "2022-03-19T08:00:49Z")

</div>

This topic was automatically closed after 5 days. New replies are no longer allowed.
