-
I’ve been hunting a memory leak in my nest application for a few days, and I think I’m fairly sure that I do not understand how the RequestContext is supposed to work. Given the following cron job, I thought it should (since the decorator is there) create a new context, use the em from this clean context, and then this cron is done, garbage collect. (maybe with some delay) What I’m seeing is my heap space jumping 3-400MB, but never dropping (maybe a fraction). I see the same for my requests, every request i can check the RequestContext.currentRequestcontext.id and get a new id, but a call with a simple resolver->service->repository.findAndCount() (returns 6 complex items) loses 1-2MB.
I've removed about everything from my app that could interfere, and it still happens. What am I missing here? |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 2 replies
-
To give some idea, this is just the cronjob every hour |
Beta Was this translation helpful? Give feedback.
-
Trying some snapshots, i see 7 em's are retained with 61k of the usage entities and 61k of other wrapped entities, they are all retained by the mongo driver if i read this correct. |
Beta Was this translation helpful? Give feedback.
-
You need to not use the first entity manager the orm give you once connected to your database, but instead fork it. For example, each time you receive a new http request, in your Express/Koa/whatever middlewares chain, make sure to pass to the code downstream a child of the root entity manager. You can get one by doing |
Beta Was this translation helpful? Give feedback.
-
I was hoping mongo would GC itself at some point, but no (drops are restarts due to k8s memory limits) |
Beta Was this translation helpful? Give feedback.
-
No improvement from downgrading a few versions of mikro, nor switching to AsyncLocalStorage |
Beta Was this translation helpful? Give feedback.
-
In the end:
|
Beta Was this translation helpful? Give feedback.
In the end: