Search code examples
node.jspostgresqlexpressgoogle-cloud-platformout-of-memory

Debugging an Out of Memory Error in Node.js


I'm currently working on a Node.js project and my server keeps running out of memory. It has happened 4 times in the last 2 weeks, usually after about 10,000 requests. This project is live and has real users.

I am using

  • NodeJS 16
  • Google Cloud Platform's App Engine (instances have 2048mb of memory)
  • Express as my server framework
  • TypeORM as database ORM (database is postgres hosted on separate GCP SQL instance)

I have installed the GCP profiling tools and have captured the app running out of memory, but I'm not quite sure how to use the results. It almost looks like there is a memory leak in the _handleDataRow function within the pg client library. I am currently using version 8.8.0 of the library (8.9.0 was just released a few weeks ago and doesn't mention fixing any memory leaks in the release notes).

I'm a bit stuck with what I should do at this point.

Any suggestions or advice would be greatly appreciated! Thanks.

enter image description here

enter image description here

enter image description here

Update: I have also cross-posted to reddit and someone there helped me determine that issue is related to large queries with many joins. I was able to reproduce the issue, and will report back here once I am able to solve it.


Solution

  • The problem was the package @google-cloud/debug-agent. There is an open issue about the package causing a memory leak. Many people have had this issue.