-
Notifications
You must be signed in to change notification settings - Fork 304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Slow jest test memory leak #2161
Comments
This is extremely critical, it's literally unusable now. Any temporary workarounds, please? |
I've tried to run your reproducing repo with chrome inspector and do not see any memory leaks. Yes, jest reports memory growth for each file BUT it is normal behaviour because of the Garbage Collector didn't triggered yet. If i manually trigger GC then memory footprint becomes ~160MB. The slowness that i see happens because of for each spec file Jest executes
As we can see that for each simple spec file:
we bootstrap the Angulars testing module. Without extra bootstrap logic tests pass in 13 seconds instead of 40 seconds in my laptop. Just to confirm my assumptions regarding memory leak we can launch nodejs instance with the hard limit by available memory to force V8 GC to run much frequently. Lets limit heap size to 300MB:
|
@platov Thanks for looking into this. Yes if you lower the max memory memory garbage collection will be triggered before it grows beyond the limit. However I think it is still leaking, checkout this article: https://chanind.github.io/javascript/2019/10/12/jest-tests-memory-leak.html The memory should be mostly constant especially for simple test like the ones in the sample repo.
*I forked this sample repo FYI. If you can find the file for me that would be great. Looks like you just changed it 18 minutes ago I will retest and see if this has improved the situation. |
@vespertilian, i agree that Jest has memory leak itself that is not related to jest-preset-angular. To solve Jest memory leak issue in my project helped usage of the
Sorry i was not clear. The bootstrap logic of Angular testing module is placed here. If You remove |
@platov Thanks for this. Yes moving out setup jest does speed it up, however you need setup jest if you want to test anything meaningful, like a component. I want to dig into this a bit more, I won't have time until later this week. Thanks again. Stay tuned. |
@vespertilian Did you find something that's worth to share? What I've found out so far was updating to node 20.10.x brought some improvements but more as a positive side effect due to optimizations on "fs" But all in all the tests feel much too slow for what's really happening and it's very time consuming in the hooks/pipeline. |
I have been looking at it again today. No luck yet. I will probably do a bit more on Monday or Tuesday. Weirdly this
Seems to fix the issue in my test repo but you need --expose-gc ass well as --no-compilation-cache Are you using NX or just the Angular CLI? |
Hey @vespertilian I'm using Angular using NX and all in the latest version. Edit: |
Weirdly I tried this suggestion and improved my memory times. Do you know the why ?!?!
Weirdly I tried this suggestion and improved my memory times. Do you know the why @BernhardBehrendt ?!?! |
Just made the upgrade to 14.0.0 and it's still no noticeable improvement. Have actually no more idea and hope that angular will move jest esbuild support soon out of experimental so that it bet better integation into nx. |
@BernhardBehrendt but your problem is memory or cpu ? Because @vespertilian solution could ensure me that in regards to memory after some time i see that memory between tests isnt increasing so much as without the flags mentioned. It seems that it does garbage collection test after test ... it really improves in regards to memory while tests are running .. In my case helped me a lot |
@ricardomiguelmoura it's still memory in my case. CPU is fine |
I am experiencing this problem as well in my nx workspace with a mix of Angular and NestJS apps/libraries. During development it is hardly noticed that the tests leak memory. I am not using the most recent versions of any package used, but I have read multiple times that this issue prevailed through multiple versions of Angular and nx. My investigation is described in the following notes: The CI pipeline executes all tests in the workspace by running First step is to demonstrate the growing memory usage. We can execute jest directly, make it execute tests one at a time and log the heap usage:
For apps with many tests, the memory usage hits the limits of the pipeline runners but not my local machine, so i can verify for sure that the memory usage is way higher than it should be (several gigabytes at some point). To narrow it down, I have an example app with just a couple of tests. It consistently shows increasing memory usage with some ups and downs of course.
I read about similar problems online and found a couple of approaches that I all tried, most importantly running the garbage collection manually with Next I ran the test command for a single test file:
The test runs successfully. However, jest has the experimental feature
This is an experimental feature of course, but at this point it is all I have available. For the next step, we will look at the test and try to locate the problem by eliminating code: describe('AppComponent', () => {
beforeEach(async () => {
// await TestBed.configureTestingModule({
// imports: [RouterTestingModule],
// declarations: [AppComponent],
// }).compileComponents();
});
it('should create the app', () => {
// const fixture = TestBed.createComponent(AppComponent);
// const app = fixture.componentInstance;
// expect(app).toBeTruthy();
expect(true).toBeTruthy();
});
}); All Angular-related code is removed and the memory issue still exists. Next, I removed the // import 'jest-preset-angular/setup-jest'; Now the test succeeds without memory errors:
My result so far is: it seems that For more information, I also tried debugging the memory usage of node directly with I will gladly go into more detail on each of my steps, any help to reduce the memory usage is greatly appreciated. |
Version
13.1.0
Steps to reproduce
Clone repo
https://github.com/vespertilian/jest-preset-angular-mem-usage
npm install
npm run test-jest
Expected behavior
I would expect for simple tests memory to not increase at an alarming rate.
Actual behavior
Testing Angular seem to leak memory which makes the tests run really slow on older computers when you have multiple libraries
Some more info here NX
nrwl/nx#18926
Thanks for all your had work on this!
Additional context
No response
Environment
The text was updated successfully, but these errors were encountered: