From 6945a712dfef5fe2fdfdf2b42d51ffbc6e1f55fe Mon Sep 17 00:00:00 2001 From: Girish Ramakrishnan Date: Thu, 10 Sep 2015 19:18:02 -0700 Subject: [PATCH] limit node memory usage node needs to be told how much space it can usage, otherwise it keeps allocating and we cannot keep it under 50M. keeping old space to 30M, lets the memory hover around 40M there are many options to v8 but I haven't explored them all: --expose_gc - allows scripts to call gc() --max_old_space_size=30 --max_semi_space_size=2048 (old/new space) node first allocates new objects in new space. if these objects are in use around for some time, it moves them to old space. the idea here is that it runs gc aggressively on new space since new objects die more than old ones. the new space is split into two halves of equal size called semi spaces. --gc_interval=100 --optimize_for_size --max_executable_size=5 --gc_global --stack_size=1024 http://erikcorry.blogspot.com/2012/11/memory-management-flags-in-v8.html http://jayconrod.com/posts/55/a-tour-of-v8-garbage-collection https://code.google.com/p/chromium/issues/detail?id=280984 http://stackoverflow.com/questions/30252905/nodejs-decrease-v8-garbage-collector-memory-usage http://www.appfruits.com/2014/08/running-node-js-on-arduino-yun/ note: this is not part of shebang because linux shebang does not support args! so we cannot pass node args as part of shebang. --- setup/container/systemd/apphealthtask.service | 2 +- setup/container/systemd/box.service | 2 +- setup/container/systemd/janitor.service | 2 +- setup/container/systemd/oauthproxy.service | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/setup/container/systemd/apphealthtask.service b/setup/container/systemd/apphealthtask.service index 053871962..72c4627eb 100644 --- a/setup/container/systemd/apphealthtask.service +++ b/setup/container/systemd/apphealthtask.service @@ -7,7 +7,7 @@ StopWhenUnneeded=true Type=idle WorkingDirectory=/home/yellowtent/box Restart=always -ExecStart="/home/yellowtent/box/apphealthtask.js" +ExecStart=/usr/bin/node --optimize_for_size --max_semi_space_size=5 --max_old_space_size=30 /home/yellowtent/box/apphealthtask.js Environment="HOME=/home/yellowtent" "USER=yellowtent" "DEBUG=box*,connect-lastmile" "BOX_ENV=cloudron" "NODE_ENV=production" KillMode=process User=yellowtent diff --git a/setup/container/systemd/box.service b/setup/container/systemd/box.service index 6fe6d459f..77da8ea5b 100644 --- a/setup/container/systemd/box.service +++ b/setup/container/systemd/box.service @@ -7,7 +7,7 @@ StopWhenUnneeded=true Type=idle WorkingDirectory=/home/yellowtent/box Restart=always -ExecStart="/home/yellowtent/box/app.js" +ExecStart=/usr/bin/node --max_old_space_size=150 /home/yellowtent/box/app.js Environment="HOME=/home/yellowtent" "USER=yellowtent" "DEBUG=box*,connect-lastmile" "BOX_ENV=cloudron" "NODE_ENV=production" KillMode=process User=yellowtent diff --git a/setup/container/systemd/janitor.service b/setup/container/systemd/janitor.service index d930758f8..7e905bdf6 100644 --- a/setup/container/systemd/janitor.service +++ b/setup/container/systemd/janitor.service @@ -7,7 +7,7 @@ StopWhenUnneeded=true Type=idle WorkingDirectory=/home/yellowtent/box Restart=always -ExecStart="/home/yellowtent/box/janitor.js" +ExecStart=/usr/bin/node --optimize_for_size --max_semi_space_size=5 --max_old_space_size=30 /home/yellowtent/box/janitor.js Environment="HOME=/home/yellowtent" "USER=yellowtent" "DEBUG=box*,connect-lastmile" "BOX_ENV=cloudron" "NODE_ENV=production" KillMode=process User=yellowtent diff --git a/setup/container/systemd/oauthproxy.service b/setup/container/systemd/oauthproxy.service index bec5db226..f52dd7a25 100644 --- a/setup/container/systemd/oauthproxy.service +++ b/setup/container/systemd/oauthproxy.service @@ -7,7 +7,7 @@ StopWhenUnneeded=true Type=idle WorkingDirectory=/home/yellowtent/box Restart=always -ExecStart="/home/yellowtent/box/oauthproxy.js" +ExecStart=/usr/bin/node --optimize_for_size --max_semi_space_size=5 --max_old_space_size=30 /home/yellowtent/box/oauthproxy.js Environment="HOME=/home/yellowtent" "USER=yellowtent" "DEBUG=box*,connect-lastmile" "BOX_ENV=cloudron" "NODE_ENV=production" KillMode=process User=yellowtent