# Node-RED and memory

**URL:** <https://discourse.nodered.org/t/node-red-and-memory/4530>\
**Category:** General\
**Created:** [3 November 2018 15:47 UTC](https://discourse.nodered.org/t/node-red-and-memory/4530 "2018-11-03T15:47:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmckinnon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rmckinnon/32/3536_2.png) [@rmckinnon](https://discourse.nodered.org/u/rmckinnon)\
**Post date:** [3 November 2018 15:47 UTC](https://discourse.nodered.org/t/node-red-and-memory/4530/1 "2018-11-03T15:47:04Z")

</div>

Is there a way to increase the amount of memory Node-RED uses? I am processing rather large number of files and I am getting a "JavaScript heap out of memory" error.

---

<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 November 2018 16:23 UTC](https://discourse.nodered.org/t/node-red-and-memory/4530/2 "2018-11-03T16:23:35Z")

</div>

This is a Node.JS issue rather than Node-RED as such.

This may help if you have sufficient RAM in your device.

> <https://stackoverflow.com/questions/38558989/node-js-heap-out-of-memory>

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 November 2018 16:36 UTC](https://discourse.nodered.org/t/node-red-and-memory/4530/3 "2018-11-03T16:36:23Z")

</div>

What hardware are you running on?

---

<div class="post-metadata">

**Author:** ![rmckinnon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rmckinnon/32/3536_2.png) [@rmckinnon](https://discourse.nodered.org/u/rmckinnon)\
**Post date:** [4 November 2018 10:03 UTC](https://discourse.nodered.org/t/node-red-and-memory/4530/5 "2018-11-04T10:03:08Z")

</div>

I saw similar information in other blogs and I tried the following with no luck:

```auto
node-red --max-old-space-size=4096

```

Any ideas on how would I apply this when starting up Node-RED?

> [@Colin](#):
>
> What hardware are you running on?

Mac Book Pro

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 November 2018 10:15 UTC](https://discourse.nodered.org/t/node-red-and-memory/4530/6 "2018-11-04T10:15:35Z")

</div>

How do you start node red on your system? Does it start automatically on boot?  
What OS are you running, or is that a silly question on a Mac Book, I don't know.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [4 November 2018 10:32 UTC](https://discourse.nodered.org/t/node-red-and-memory/4530/7 "2018-11-04T10:32:08Z")

</div>

Hi @rmckinnon

By default, node.js does not start garbage collecting unused memory until it hits 2gb. The maxoldspace setting allows you to set when gc should kick in. It is not a hard limit.

If you are running out of memory in a Mac, then you will almost certainly have over 2gb free memory so the node GC will have already locked in. Changing the point at which it kicks in is unlikely to have any meaningful effect.

This suggests your flows do have a genuine memory leak in them. They can be hard to track down. In general terms, what is your flow doing? Is it handling large messages?

---

<div class="post-metadata">

**Author:** ![rmckinnon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rmckinnon/32/3536_2.png) [@rmckinnon](https://discourse.nodered.org/u/rmckinnon)\
**Post date:** [4 November 2018 13:24 UTC](https://discourse.nodered.org/t/node-red-and-memory/4530/8 "2018-11-04T13:24:09Z")

</div>

@knolleary to help understand this issue I wrote a simple node I call "memory" that outputs the nodeJS process memory stats. [Process | Node.js v21.5.0 Documentation](https://nodejs.org/api/process.html#process_process_memoryusage). When I run a simple sequence "inject -\> memory -\> debug", I am seeing a total size memory of about 54 Megs

 ![Node-RED_memory](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2dd60c298213b9583048be6b0203652f2c3753e1.png)

In my sequence where I am trying to process thousands of files using nodes from the "node-red-contrib-fs-ops" pallet is where I get the "JavaScript out of heap memory" issue. My thought is that if I could raise the 54M of memory to 4G, I should be good.

@Colin

> [@Colin](#):
>
> How do you start node red on your system? Does it start automatically on boot?

I start it from the command line using the node-red script. This script is created when you install node-Red using the npm tool with nodeJS.

> [@Colin](#):
>
> What OS are you running, or is that a silly question on a Mac Book, I don't know.

It runs OSX High Sierra

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [4 November 2018 13:28 UTC](https://discourse.nodered.org/t/node-red-and-memory/4530/9 "2018-11-04T13:28:31Z")

</div>

> [@rmckinnon](#):
>
> My thought is that if I could raise the 54M of memory to 4G, I should be good.

You don't allocate memory to node. Node asks the OS for memory when it needs it. You hit OutOfMemory when the OS is unable to allocate any more memory.

If you are trying to process thousands of files using NR then it sounds like you're trying to do too much at once. How does the flow operate? Have you built any throttling in or is your flow trying to handle the thousands of files in one go?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 November 2018 13:51 UTC](https://discourse.nodered.org/t/node-red-and-memory/4530/10 "2018-11-04T13:51:14Z")

</div>

It may not be relevant to the problem here, but I recently ran into a problem when I was using the exec node to carry out operations on a bunch of files. I passed a sequence of messages to the exec node, not realising that each time it received a new message it started the command running even if the previous one had not completed, even though I was running in exec not spawn mode. The result was I had thousands of processes all running at once. I didn't run out of memory but it certainly clogged up my system for a while. I solved it by using node-red-contrib-semaphore to ensure that it would only run one at a time.

---

<div class="post-metadata">

**Author:** ![rmckinnon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rmckinnon/32/3536_2.png) [@rmckinnon](https://discourse.nodered.org/u/rmckinnon)\
**Post date:** [4 November 2018 14:51 UTC](https://discourse.nodered.org/t/node-red-and-memory/4530/11 "2018-11-04T14:51:38Z")

</div>

> [@knolleary](#):
>
> You don't allocate memory to node. Node asks the OS for memory when it needs it. You hit OutOfMemory when the OS is unable to allocate any more memory.

I have a JAVA background so I was assuming it worked similar.

> [@knolleary](#):
>
> If you are trying to process thousands of files using NR then it sounds like you're trying to do too much at once. How does the flow operate? Have you built any throttling in or is your flow trying to handle the thousands of files in one go?

For clarity I am adding a picture of the sequence:

 ![Node-RED_many_files](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/9ed3a3d02bfbeb8d07a3441f6325dbf60a7b8dc8.png)

I think I am hearing that Node-RED depends on the OS to allocate it's memory. I am assuming that if I am able to give the OS more memory then, theoretically, Node-RED should be able to ask for more memory.

As far as the issue with my sequence: I am sure I can solve it by breaking things up but I was looking to see if we could brute-force a solution.

@Colin - Thanks for the tip, I will check out node-red-contrib-semaphore.

---

<div class="post-metadata">

**Author:** ![rmckinnon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rmckinnon/32/3536_2.png) [@rmckinnon](https://discourse.nodered.org/u/rmckinnon)\
**Post date:** [4 November 2018 18:06 UTC](https://discourse.nodered.org/t/node-red-and-memory/4530/12 "2018-11-04T18:06:57Z")

</div>

I made some lead-way with the brute-force approach. When I started node-red with the following command I was able to complete the sequence without a "JavaScript heap out of memory" error.

`node --max_old_space_size=8192 /Users/rob/.nvm/versions/node/v10.11.0/bin/node-red`
