Troubleshooting
Node-RED crash and memory growth: systemd restart recovery
Short answer
Set a Restart policy in the Service section of the Node-RED unit file so systemd brings the process back after a crash, and prove it with a controlled failure test. Then trend Node-RED and device memory side by side: a climb that ends in a crash after about 30 hours is flow-side growth, and no restart policy removes it.
On this page
A Node-RED gateway that crashes after about 30 hours of uptime needs two separate fixes. The crash is a service-management problem: add a restart policy to the [Service] section of the Node-RED unit — in the reported case the file /usr/lib/systemd/system/node-red.service — so systemd starts the process again on its own, and prove that with a controlled failure test. The memory climb is a different fault: reported use reached 1301.63 MB on a flow that polls Modbus, buffers to SQLite for web charts and publishes to MQTT, and no restart policy stops that growth. Fit the policy first so the data path survives a crash, then trend memory by process to find which component fills the RAM.
Separate what the report proves from what it suggests #
Sort the observations from the interpretation before changing anything — the two were mixed together in the original troubleshooting.
- Confirmed: Node-RED stopped after about 30 hours of uptime, with 1301.63 MB reported by the
devicestatsnode at the moment of the crash. That is the reading at which this unit died, not a documented limit for Node-RED or the device. - Confirmed: forcing garbage collection with the
gc-triggernode did not prevent the crash. An explicit clean-up trigger cannot reclaim allocations the flow still holds. - Confirmed: redeploying the flow released memory. That points the investigation at what the deployed nodes retain; it does not by itself identify a leak.
- Hypothesis, not measurement: roughly 700 MB of the reading may already belong to other software on the gateway. Treat it as a working number to verify, never as a measured base load.
Whether devicestats reads the whole device or the Node-RED process decides which series owns the climb, and the only way to know on your unit is to log both. The same process-versus-device split matters when Node-RED runs containerized, where the runtime adds overhead of its own — see High CPU and memory with Node-RED containers on Balena Engine.
Add the restart policy to the unit #
The fix that worked in the reported case was a restart policy in the unit file, so the service recovers without anyone walking to the gateway. The report does not record the exact line that was used, so set the documented policy that matches the goal: systemd restarts the process when it exits in failure and leaves a deliberate stop alone.
- Copy
/usr/lib/systemd/system/node-red.serviceto a dated backup before editing it. - Open the file and find the
[Service]section — on this image it is the section containing the lineStandardOutput = syslog. - Add
Restart=on-failureon its own line in that section, afterStandardOutput = syslog.Restartis the policy name andon-failurestarts the process again after an abnormal exit; if your init rejects the line, use one of the restart conditions your systemd version documents for the same behavior. - Reload the unit files and restart the service:
sudo systemctl daemon-reload
sudo systemctl restart nodered
The file on this image is node-red.service, while many Debian installs name the unit nodered — restart whichever unit name your systemctl lists. Then confirm the process came back under the service's own context, which the unit sets with its -u flag, not under your login.
If the same gateway ran Node-RED in Docker instead of as a systemd unit, the equivalent recovery is the container restart policy — see Auto restart Node-RED in Docker after exit code 255.
Prove the policy before you trust it #
A restart line that has never fired is a hope, not a control. Run the failure test on a bench unit or in a maintenance window, because the test deliberately takes the gateway's data path down.
- End the Node-RED process abnormally, by killing the process rather than stopping the unit cleanly —
on-failurecorrectly ignores a deliberate stop. - Watch systemd start it again and confirm the editor answers on port 1880.
- Check the unit's status shows exactly one restart. The manager rate-limits repeated failures, so a loop means the policy or the flow needs another look.
- Verify the data paths, not just the process: Modbus polling, the SQLite read and write, MQTT publishing and the web charts. A process that is up is not an application that is recovered.
Why a manual restart prints warnings the service does not #
Restarting by hand in the reported case printed terminal alerts that never appeared at boot, and the unit was rebooted to clear them. The cause is user context, not a new fault: a manual launch runs under the login that typed the command, while the boot-time service runs under the user the unit sets with its -u flag. Started by systemd, the process returns to the same context it had at boot — which also answers whether a policy-driven restart keeps the context. It does. Keep diagnosis inside systemd, because a second manually started Node-RED beside the service fights it for the port and the files.
Worked example: Node-RED's share of a 1301.63 MB crash after 30 hours #
Inputs, all from the reported case:
- Peak memory use at the crash: 1301.63 MB, reported by the
devicestatsnode and logged to the cloud. - Assumed other-software load: 700 MB — the hypothesis raised in the report, used here as a working number your trend confirms or discards.
- Run time to the crash: about 30 hours.
Rule in words: subtract the assumed base load from the crash reading to get the flow's share, then divide the share by the run time to get the growth rate your trend must reproduce.
- Flow's share: 1301.63 MB − 700 MB = 601.63 MB
- Growth rate: 601.63 MB ÷ 30 h ≈ 20.05 MB per hour, about 0.33 MB every minute
Result: if the 700 MB assumption holds, the flow grew about 601.63 MB across the run — roughly 20 MB per hour from a standing start — and it returns to the crash reading within the original run time after every clean restart, so restarts alone buy days, not weeks.
Decision, from a three-series trend after the next clean restart — Node-RED's RSS, the device total and the other processes' use: a Node-RED line climbing at anything near 20 MB per hour puts the growth in the flow. Audit what the deployed nodes retain, from context and chart arrays to SQLite records and anything queued while the cloud link is down — the outage behavior of one such path is covered in Fix Node-RED InfluxDB write errors after a network outage. A flat Node-RED line with a climbing device total puts the growth in the base image or another service; then the 700 MB figure is real base load, so measure which process holds it and cap it or move it off the gateway.
The decision for each case #
- Crash with stable memory after recovery: keep the verified restart policy and re-check the unit file after every image or package update. Done.
- Node-RED's series climbs at the example's rate: the flow owns the growth. Redeploying freed memory in the report, which points at allocations the deployed nodes hold — audit retained context, chart history and database records, and prune or size them. The
gc-triggernode is not the fix. - Device total climbs while Node-RED stays flat: another service or the base image owns the RAM. Measure per process, then cap, reconfigure or relocate the offender.
- Restarts fail or loop: an automatic reboot was added in the reported case as a last-resort fallback when systemd could not restore the service, but its parameters were never recorded, so do not copy an inferred configuration. Set your own failure threshold and loop protection, and validate both on a bench unit with the same image before it guards a live line.
Questions asked
Does the systemd restart policy also solve the memory growth?
No. The restart frees RAM only by ending the process, and the flow grows back at close to the same rate as before. Let the policy keep the data path alive between crashes while you trend Node-RED and device memory side by side, find which series climbs, and then change the flow or the image that owns the growth.
Why does a manual restart print warnings that the service does not?
A manual launch runs under your login's user context, while the boot-time service runs under the user the unit sets with its -u flag. The alerts seen after restarting by hand are permission and environment differences, not new faults, and they disappear when systemd starts the process, returning it to the same context it had at boot.
Can systemd be made to reboot the whole gateway instead?
An automatic reboot fallback appears in the reported case, added for times when systemd could not restore the service, but the parameters used were never recorded. Configure your own failure threshold and loop protection and prove both on a bench unit: a mis-set reboot hook turns a single crashed service into a gateway that cycles power and drops every data path with it.
Is the memory reading at the crash a Node-RED limit?
No. That figure is what your monitoring reported on that gateway at the moment of the crash, not a documented ceiling for Node-RED or for the device. Log the Node-RED process's own RSS and the device total as separate series so the next trend shows which one actually reached the wall.
Will the unit-file edit survive an image or package update?
Not necessarily. The edited file sits under /usr/lib, where an update can replace the packaged unit, so keep a dated copy and re-check the policy after every update. A systemd drop-in override keeps the same policy in a file of your own outside the packaged path, which is why many plants prefer it on gateway images.