Search code examples
node.jswebsocketramraspberry-pi3modbus

Raspberry Pi 3 script RAM usage


I'm using a raspberry pi 3 to pull serial data (USB) from a device, then I'm sending this data with websockets to the client. All runs on NodeJS.

To pull data from the device we use modbus-rtu, but we have a ram issue. The script stops working after 1-2hours. We found out that .readHoldingRegisters() increment our RAM usage after time.

Any tips on a solution?


Solution

  • First thing to do with node in a ram constraint environment (less than 1.5gb available for node) is to tell node itself, because, it seems it can't know by himself.

    If you don't the GC assume it has at least 1.4Gb available, and node GC is lazy meaning, it will wait to that limit to release a lot of memory. So the first thing to do on a Rasberry Pi 3 with 1Gb ram, is to force the GC lot sooner, like 500Mb of ram.

    node script.js --max-old-space-size=500 
    

    It will force the GC to act sooner (old space size isn't the only memory space of node, but it's the biggest, so GC should act around a node process of 500/600 Mb)

    GC is a synchronous operation (hence the lazy pattern), it can impact performances, so you may have to fine tune the limit.

    If you still encounter trouble after this, you truly have a leak (memory a Garbage can't retrieve).