Openwrt NodeJs With ExpressJs High memory footprint

Hi,
I am running openwrt on vm with base memory 256MB (on actual device it will be 64MB).
I downloaded and installed Nodejs using following command.

opkg update
opkg install node

Here is my expressJs based server code.

var express = require('express');
var app = express();
app.get('/',function(req,res){
	res.json({"hello":"world"});
});
app.listen('8081',
function(){
	console.log('app is now running on 8081');
})

node index.js to run the program
Output of top command

 PID  PPID USER     STAT   VSZ %VSZ %CPU COMMAND
    1     0 root     S    13348   5%   0% /sbin/procd
 1159  1087 root     S     3064   1%   0% udhcpc -p /var/run/udhcpc-eth1.pid -s
  633     2 root     SW       0   0%   0% [kworker/0:2]
 **3236  1323 root     S     583m 238%   0% node index.js**
 1087     1 root     S    15432   6%   0% /sbin/netifd
 1205     1 root     S    15280   6%   0% /usr/sbin/uhttpd -f -h /www -r LEDE -
 1042     1 root     S    13232   5%   0% /sbin/rpcd
 1112     1 root     S    11224   4%   0% /usr/sbin/odhcpd
  852     1 root     S     8984   4%   0% /sbin/ubusd
 1033     1 root     S     7040   3%   0% /sbin/logd -S 64
 1394  1365 root     R     3076   1%   0% top
 1323  1319 root     S     3072   1%   0% -ash
  854     1 root     S     3068   1%   0% /bin/ash --login
 1365  1361 root     S     3068   1%   0% -ash
 1248     1 root     S     3064   1%   0% /usr/sbin/ntpd -n -N -S /usr/sbin/ntp
 1361  1170 root     S     3000   1%   0% /usr/sbin/dropbear -F -P /var/run/dro
 1319  1170 root     S     3000   1%   0% /usr/sbin/dropbear -F -P /var/run/dro
 1314     1 dnsmasq  S     2940   1%   0% /usr/sbin/dnsmasq -C /var/etc/dnsmasq
 1170     1 root     S     2888   1%   0% /usr/sbin/dropbear -F -P /var/run/dro
  7     2 root     SW       0   0%   0% [rcu_sched]

how to reduce memory consumption?
Thanks in advance

The easiest approach would involve not using nodejs.

3 Likes

node --v8-options | grep memory
--optimize_for_size (Enables optimizations which favor memory size over execution speed)
--max_executable_size (max size of executable memory (in Mbytes))
--print_max_heap_committed (print statistics of the maximum memory committed for the heap in name=value format on exit)
--track_gc_object_stats (track object counts and memory usage)
--trace_gc_object_stats (trace object counts and memory usage)
--memory_reducer (use memory reducer)
--histogram_interval (time interval in ms for aggregating memory histograms)
--use_idle_notification (Use idle notification to reduce memory footprint.)
--zap_code_space (Zap free memory in code space with 0xCC while sweeping.)

This is on my laptop. The node from the OpenWrt repo might have other options.

I've got to agree with @slh. With 64 MB of memory, you're going to have to be careful with anything that you run if you want a stable router. An Archer C7 here "doing nothing" past running APs and bridging traffic "idles" at around 25-30 MB of RAM (Edit: that is without uhttpd and LuCI present).

Given that you already have uhttpd installed and running, and that I'm assuming that you need more than "Hello world" running, do you really need NodeJS?

Will NodeJS leverage your existing TLS library, or are you going to have to install another TLS library as well?

1 Like