Expose garbage collection (e.g. --expose-gc) in a production Firebase Function

Viewed 740

We want to run global.gc() from within a Firebase Function. Locally, we start the node process with node --expose-gc .... How do we do the equivalent of that with a deployed Firebase Function?

As far as I can tell, there is no way to inject node --expose-gc when a Firebase Function starts. Further, there appears no way to set NODE_OPTIONS="--expose-gc before the Firebase Function starts node.

We have tried setting process.env.NODE_OPTIONS='--expose-gc' from within the Firebase Function. That, however, does not work. We assume the reason that does not work is that --expose-gc must be set when node starts (and not after node is already running).

2 Answers

I'm not familiar with how a "Firebase Function" context is structured, but if it is just a normal NodeJS context you can add V8 flags inline:

import v8 from 'node:v8';

v8.setFlagsFromString('--expose-gc');

It's not possible to specify any command line parameters to the node process that runs your function. That would actually be a security problem. So long as GC must be enabled by command line parameters, what you're trying to do will be impossible with Cloud Functions.

Consider instead using Cloud Run, which lets you build your server image any way you want, including the parameters to run node.

Related