[Service] Call global.gc() when terminating a service 79/243879/8
authorYoungsoo Choi <kenshin.choi@samsung.com>
Fri, 11 Sep 2020 01:38:21 +0000 (18:38 -0700)
committerYoungsoo Choi <kenshin.choi@samsung.com>
Tue, 15 Sep 2020 02:36:08 +0000 (19:36 -0700)
This calls global.gc() when a service app is terminated.
Approximately, 1 MB memory is saved by this.

Also, the main thread is immediately terminated
when there's no running worker thread for service app.

Together with:
https://review.tizen.org/gerrit/243858

Change-Id: Iab70e9aadedb19295a3784fb5cc3dc1300412995
Signed-off-by: Youngsoo Choi <kenshin.choi@samsung.com>
wrt_app/common/service_manager.ts

index 6703ff4..9637542 100644 (file)
@@ -21,6 +21,14 @@ export function startService(id: string, filename: string) {
     if (isMainThread) {
       let startService = __dirname + '/service_runner.js';
       workers[id] = new Worker(startService, { workerData: { id: id, filename: filename } });
+      workers[id].on('exit', () => {
+        try {
+          global.gc();
+          console.log(`global.gc() is called by ${id}`);
+        } catch (e) {
+          console.log(`${e.name}: ${e.message}`);
+        }
+      });
     }
   }
 }
@@ -34,11 +42,16 @@ export function stopService(id: string) {
     }
     runner.stop(id);
     setTimeout(() => process.exit(), 500);
-} else {
+  } else {
     workers[id].postMessage('stopService');
     setTimeout(() => {
       workers[id].terminate();
       delete workers[id];
+      let runningServices = Object.keys(workers).length;
+      console.log('Running services : ' + runningServices);
+      if (runningServices === 0 && serviceType !== 'UI') {
+        process.exit();
+      }
     }, 500);
   }
 }