Is it possible to reduce NodeJS memory usage (e.g., Ghost blogging platform)

11,155

Ghost runs a single node process per instance. I believe what you're seeing there is that htop shows the underlying threads of processes, so it looks like you have one Ghost instance running but node/v8 has five active threads. Despite JavaScript being single-threaded, the engine/vm itself can be multi-threaded.

We (the Ghost project) do pay attention to the memory footprint so that we can do whatever we can do keep it as small as possible. I personally think 80MB is pretty good but obviously different people and situations have different criteria for what's "pretty good."

Share:
11,155

Related videos on Youtube

Aizan Fahri
Author by

Aizan Fahri

Updated on September 18, 2022

Comments

  • Aizan Fahri
    Aizan Fahri over 1 year
    CPU [                         0.0%]     Tasks: 15, 2 thr; 1 running
    Mem [||||                 14/256MB]     Load average: 0.00 0.00 0.00 
    Swp [|                     1/256MB]     Uptime: 15 days, 06:02:31
    

    Above is the memory usage on my server (Ramnode, 256MB RAM with 256MB Swap) when there's no Ghost instance. On my VPS I am running 4 Ghost instances.

    So when I ran a Ghost instance here with the command node index.js, it spawns 5 workers.

    PID   USER   PRI  NI  VIRT  RES   SHR  S CPU% MEM%   TIME+  Command
    10380 user    20   0  975M 80328  7712 S  0.0 30.6  0:00.00 node index.js
    10381 user    20   0  975M 80328  7712 S  0.0 30.6  0:00.00 node index.js
    10382 user    20   0  975M 80328  7712 S  0.0 30.6  0:00.00 node index.js
    10383 user    20   0  975M 80328  7712 S  0.0 30.6  0:00.00 node index.js
    10384 user    20   0  975M 80328  7712 S  0.0 30.6  0:00.00 node index.js
    

    5 workers are actually quite a lot. And 30% is also a lot.

    And now, the htop reading

    CPU[                               0.0%]     Tasks: 18, 7 thr; 1 running
    Mem[|||||||||||||||||||        82/256MB]     Load average: 0.00 0.00 0.00 
    Swp[||                          8/256MB]     Uptime: 15 days, 06:22:29
    

    How to reduce the memory usage? If I can adjust the number of workers, which file should I edit? If we have Ghost dev team here, do you have any plan on this issue?

    • Giovanni Tirloni
      Giovanni Tirloni over 9 years
      I thought NodeJS was supposed to be super lightweight.
    • Aizan Fahri
      Aizan Fahri over 9 years
      @gtirloni exactly my point here!
    • Michael Hampton
      Michael Hampton over 9 years
      That's already very lightweight!
    • joeytwiddle
      joeytwiddle over 8 years
      I started a Hello World Node app for comparison, and found it uses 65MB, so that means the Ghost app is only adding 15MB. And that's the whole app in memory at once (unlike say a PHP webapp which loads only part of the app for any given request). Well, I guess this is lightweight for 2014/15. imgur.com/gallery/uqKOX
    • joeytwiddle
      joeytwiddle over 8 years
      But having said that, top here reports my running ghost app uses 37876k under the RES column, which differs significantly from yours. (Node v0.10.37 on a DigitalOcean "Intel Xeon".)
  • Aizan Fahri
    Aizan Fahri over 9 years
    Thanks for the answer @jtw. So 80MB is considered as lightweight. My personal thought on this matter is that 80MB for a less 20 visitors per day is quite overkill.
  • jtw
    jtw over 9 years
    @AizanFahri "lightweight" is not really an objective term but in general, for a package that does as much as Ghost, I think 80MB is a pretty reasonable memory footprint. If your concern is being able to get 4 Ghost blogs running on a single 256MB VPS, I suspect you can pull that off--especially if they are low traffic. There will be some memory savings due to the multiple node processes being able to share some memory.
  • Mike
    Mike over 9 years
    I'l echo @jtw here -- 80Mb is quite good, but what's more important to track is whether that number stays constant. If it slowly goes up, that's indicative of a memory leak, and ought to be tracked down and squashed.