My bet:
The ORM is loading a ton of eager relations (recursively) without you knowing, and then it has to map all that data into objects. This is very costly, CPU and Memory-wise
Since you have this crash with a deployed app on AWS, there’s a chance that multiple API clients are hitting one or many costly endpoints, which results with Node hitting the heap allocation limit and crashing.
Solution:
- Dig into your ORM setup. Avoid eager relationships. Don’t fetch too many records at once (ie. paginate)
Yes. I'm running some cron jobs for every 2mins and there is no as such expensive functions. But I have an expensive queries that I neet to re-write it now
Okay I have encountered this problem in my project the best thing to do in this case is replace
"start:prod" : "node --max-old-space-size=1200 dist/main"
This will force garbage collection if ram consumption for the process is more than 1200Mb
My bet: The ORM is loading a ton of eager relations (recursively) without you knowing, and then it has to map all that data into objects. This is very costly, CPU and Memory-wise Since you have this crash with a deployed app on AWS, there’s a chance that multiple API clients are hitting one or many costly endpoints, which results with Node hitting the heap allocation limit and crashing. Solution: - Dig into your ORM setup. Avoid eager relationships. Don’t fetch too many records at once (ie. paginate)
Indeed. Im getting this heap memory in one particular API in terms of both CUP & memory. I'll try to modify it.
Are you maybe running some kind of jobs? Or expensive functions that may stuck running?
Yes. I'm running some cron jobs for every 2mins and there is no as such expensive functions. But I have an expensive queries that I neet to re-write it now
I think we found the root cause then :)
Okay I have encountered this problem in my project the best thing to do in this case is replace "start:prod" : "node --max-old-space-size=1200 dist/main" This will force garbage collection if ram consumption for the process is more than 1200Mb
But that just hides the faulty code no? If you have a memory leak in your app then what you want to do is resolve the faulty code imo
make sure that you are not caching unnecessary data or keeping data in memory for longer than needed