Short answer
Firebase says 2nd gen functions are powered by Cloud Run, and Google’s Cloud Run docs say a container instance that uses too much memory is terminated, with a log message saying you may have a memory leak or may need more memory. Check the Memory usage plot for the function in the Google Cloud console, then raise the memory option in code if it really needs more, delete files you write to the temporary directory (they count as memory), and stream large files instead of loading them whole.
Find what fills the memory, then fix it
01
The function needs more memory than it was given
“To set memory allocation and timeout in functions source code, use the global options for memory and timeout seconds to customize the virtual machine running your functions.” Firebase, Manage functions
Image processing, PDFs, large JSON and AI SDKs can need more than the memory the function was given. Set memory in the function’s options in code (or in the Google Cloud console under Memory allocated) and deploy again. Each 2nd gen function defaults to one CPU up to 2GB, and 2 CPU at 4 and 8GB.
const { onObjectFinalized } = require("firebase-functions/v2/storage"); exports.convertLargeFile = onObjectFinalized({ timeoutSeconds: 300, memory: "1GiB", }, (event) => { // the work that needs the memory });02
Files in the temporary directory count as memory
“Local disk storage in the temporary directory is an in-memory filesystem. Files that you write consume memory available to your function, and sometimes persist between invocations.” Firebase, Tips & tricks
Writing a download, an upload or a generated file to /tmp uses the function’s memory, and the file can sometimes still be there on the next invocation. Delete each temporary file when you are done with it.
03
A large file is loaded into memory all at once
“You can reduce memory requirements when processing larger files using pipelining.” Firebase, Tips & tricks
Reading a whole video, image or export into a buffer holds all of it in memory. Google’s tips page says pipelining reduces memory requirements for larger files: process the data as a stream instead of loading it whole.
04
Memory grows until the instance is terminated
“If you see this message frequently, you may have a memory leak in your code or may need more memory.” Google Cloud, Troubleshoot Cloud Run issues
If the function fails after running fine for a while, memory may be building up between requests: for example, objects added to a global variable on every call. Open the function’s Memory usage plot in the Google Cloud console and look for memory that keeps rising between requests.
Checked against the vendor’s docs on : Firebase docs: Manage functions; Firebase docs: Tips & tricks; Google Cloud docs: Troubleshoot Cloud Run issues.
Rather hand it over?
Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.
- from $450App Fix TicketOne reproducible bug in an existing mobile or web app, diagnosed and fixed.See scope and start
- from $450AI-Built App RescueA Lovable, Bolt or Cursor app checked end to end in 3 days, then its worst problems fixed at a fixed price.No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way.See scope and start
Related: Firebase Functions deploy failed · Firebase Cloud Functions CORS error · How to cut your Firebase bill · Firestore quota exceeded