Profiling memory for a NodeJS Lambda function


Is there any tool, solution, framework, service or alike to allow memory profiling for NodeJS based lambda functions? Maybe even running on AWS directly and not on a local development environment? What would be the recommendation for such kind of profiling?

I'm aware that local profiling can be done using Chrome's Inspect / DevTools, but that actually means debugging a "long running process", not a short-lived one like a lambda function.

Additional Context:

We migrated a reporting service from an EC2 instance to a ECS Fargate based docker container using a basic lift & shift approach, but with the catch that we assigned less memory to the ECS Task than the original configuration of the EC2 instance purposely, as usually the instance had quite a lot of unused resources. So, now we are transitioning the "outliers" that fail with OutOfMemoryError into lambdas, but we'd like to improve their code and not just move them to a lambda container.

  • What is your goal for using a memory profiler? Are you running out of memory in some invocations? Are you trying to optimize memory usage to optimize the cost of Lambda? What details are you trying to capture that are missing from the the Lambda invocation logs that show the total memory used?

2 Risposte

Also searching for the best solution to create a heap snapshot from AWS Lambda written in NodeJS. Tried to call V8 heap utilities. Statistics works but as soon as I want to get a dump, it hangs:

  console.log("Heap space stats = ", v8.getHeapSpaceStatistics())
  console.log("Heap code stats heapdump = ", v8.getHeapCodeStatistics())
  console.log("Heap stats = ", v8.getHeapStatistics())

  console.log("Getting heapdump...")
  const snapshotStream = v8.getHeapSnapshot()

Any idea why? Is this even possible in the context of AWS Lambda? Any alternative - I'd really like to get memory details, not just stats?

con risposta 2 anni fa

Jeff's questions are good. We may be able to provide more suggestions if you can answer those. In the meantime, one easy way to get basic insight into memory usage in a NodeJS script is to use the process.memoryUsage() method. This article provides a nice overview with examples.

profile picture
con risposta 3 anni fa

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande