getsaf / shallow-render

Angular testing made easy with shallow rendering and easy mocking. https://getsaf.github.io/shallow-render
MIT License
273 stars 25 forks source link

Memory leak / not releasing objects #252

Closed johnwest80 closed 8 months ago

johnwest80 commented 8 months ago

@getsaf , so with a16, our karma chrome instance was hitting 3+gigs. now that we've upgraded to a17, it's reaching 4gigs and crashing (4gigs is max for chrome). we took a snapshot, and it looks like mocks aren't getting released. if you take a look at the screenshot below, you'll see what we're seeing. do you think this could be an issue with shallow-render, or, if it's something happening because of angular internals, that there would be a way to force purge it after each test?

image

johnwest80 commented 8 months ago

here are a couple of the MockModule()s above expanded. the two shown here are completely unrelated to each other, but both are still in memory, and as you see above, this is repeated for all the mocked modules.

image
johnwest80 commented 8 months ago

so good news, one of my coworkers found a way to mitigate this with the following code... not sure if this makes sense for you to add in shallow itself?

afterEach(() => { (ɵdepsTracker as any).ngModulesScopeCache.clear(); (ɵdepsTracker as any).ownerNgModule.clear(); (ɵdepsTracker as any).standaloneComponentsScopeCache.clear(); (ɵdepsTracker as any).ngModulesWithSomeUnresolvedDecls.clear(); });

getsaf commented 8 months ago

Thanks for this detail. I just ran a quick test to inspect my cache counts and it looks like I wasn't seeing that particular memory issue because I am running tests with Jest which uses a fresh process for each test file. I think Karma uses a single process for the entire test suite.

At first glance, the DepsTracker stuff is some undocumented internals from Angular that wants to boost performance but there's no mechanism for clipping cached items.

Your solution appears sound but I'll see if I can get something plugged into shallow-render as a built-in.