Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling Manual Garbage Collection in Electron app

I have been trying to get Garbage Collection and Max Old Space Size configured in our compiled and built Electron app.

I am able to debug with these settings on through the CLI electron.cmd --js-flags="--expose_gc --max-old-space-size=128" . and the Global GC is available.

However when I try and use the electron API app.commandLine.appendArgument('--js-flags', '--expose_gc --max-old-space-size=128'); to set these flags it doesn't enable GC as expected, this code is being called before the app.on('ready', ...) function.

Nor does setting NODE_OPTIONS help (I see that this has been disabled in the latest Electron version as per here: https://github.com/electron/electron/issues/12695)

Does anyone have experience getting this working?

like image 203
Jacob Wischnat Avatar asked Jul 17 '18 01:07

Jacob Wischnat


People also ask

Does Nodejs have garbage collection?

Luckily for you, Node. js comes with a garbage collector, and you don't need to manually manage memory allocation.

How do I run a garbage collector in node JS?

If you launch the node process with the --expose-gc flag, you can then call global. gc() to force node to run garbage collection. Keep in mind that all other execution within your node app is paused until GC completes, so don't use it too often or it will affect performance.

What is garbage value in JS?

JavaScript values are allocated when things are created (objects, Strings, etc.) and freed automatically when they are no longer used. This process is called Garbage collection.


Video Answer


1 Answers

app.commandLine.appendSwitch('js-flags', '--expose_gc --max-old-space-size=128')

The first argument of appendSwitch does not use the -- prefix, just drop that. The seconds argument is parsed as is, thats why it keeps the prefix.

Also be aware it only exposes gc for the renderer if you use apppendSwitch, if you want to use it there you will need to add the CLI argument.

like image 178
Hans Koch Avatar answered Oct 21 '22 12:10

Hans Koch