Skip to content

Firebase Cloud Functions errorWhy is my Firebase Cloud Function running out of memory?

The function works on small inputs, then starts failing. Google’s Cloud Run docs list this as an HTTP 500 or 503 with a log line saying the instance used too much memory and was terminated. Here’s what Google’s docs say uses that memory and what to change, or hand it over at a fixed price.

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

  1. 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
    });
  2. 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.

  3. 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.

  4. 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.

Related: Firebase Functions deploy failed · Firebase Cloud Functions CORS error · How to cut your Firebase bill · Firestore quota exceeded

Questions

More on this error

How do I see how much memory my function uses?

In the Google Cloud console, open the list of functions, select the function and choose the Memory usage plot.

Will more memory cost more?

More memory can change what a function costs, so check the pricing before you raise it. Firebase’s docs refer to Cloud Run pricing for cost calculations on minimum instances. Fixing a leak, or a temp file that never gets deleted, can make the extra memory unnecessary.

What do you need to see to fix it?

The repo, the function’s name and the log lines around the error, ideally with a screenshot of its Memory usage plot.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it: a written diagnosis first, then the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. 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.

A different error?

Describe it and get a written quote

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; we reply by email, usually within one business day.