-
Notifications
You must be signed in to change notification settings - Fork 308
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. |
@jpv-os I have narrowed down exactly to the same point before seeing your post actually. jest-preset-angular in the setup-jest will show the memory leak while using --detectLeaks. Did you find any solution? Thanks! |
I believe this is happening for me as well. I'm using the latest Nx release (19) and node (22) and every test that runs seems to get linearly slower. Or if I watch heap usage, it linearly grows. Are we going to see any progress on this anytime soon? |
I’m working with Angular team on this #2615 which I think will be the recommended way to go. |
@ahnpnl could you please elaborate why you think that it will fix the memory issue? |
The PR would recommend projects to switch to use "transpilation" mode, which is related to the term |
bump, we have a project that takes 10g+ of ram because of this issue. |
@statop you can try to use
|
I really don’t want to disable type checking. Will try it though. |
We will try to add type checking to that mode via a new flag and enable by default in the next major version |
isolatedModules didn't seem to really fix it. It made the run faster, but still used lots of memory. |
@statop indeed, this is the downside of transpiling codes with typescript API. One possible solution to solve memory issue better is switching to use |
@ahnpnl What would be required for rewriting the angular AST? Would something like https://github.com/dlvandenberg/tree-sitter-angular be helpful in that scenario? |
@nb-midwestern there are 2 main AST transformers need to be rewritten in Rust to work with
I think the repo you gave only focuses on HTML part. The transformers we need focus on component source code part, mainly processing |
It doesn't seem like that is the problem. It still just seems like a memory leak. Memory shouldn't "only go up" with each test. Perhaps create a new typescript compiler for each test. |
I remember there is one issue in Jest repo about memory leak. This repo has limited scope to transpilation process only. Each test runs with a standalone instance ts Idk how garbage collection works with Jest either. Jest supposes to free memory of each worker instance once it finishes the job. |
@ahnpnl You've mentioned 2 AST transformers that need to be rewritten in rust, after that is done what would be the next step? It sounds like an interesting challenge, but I don't really know where to start, what would be the end goal? Are there any resources that you know of that I could reference? |
@nb-midwestern Since Currently, we rely on typescript API to transpile codes, which is a lot slower than Angular team is looking into approach to build everything and run tests based on build output. It is still in progress and I think we shouldn’t reinvent the wheel into that direction giving limited maintaining resources. The builder is at https://github.com/angular/angular-cli/blob/main/packages/angular_devkit/build_angular/src/builders/jest/index.ts |
oh neat, I suppose we'll wait. |
Oh yesss, that’s exactly what I am looking for. Feel free to investigate how to integrate it. I will try to take a look as well. Thanks! |
I implemented it on my tests and saw only a slight preformance increase but it still eats through all of the memory |
I see the instruction from I'm thinking about a rewritten the existing https://github.com/thymikee/jest-preset-angular/blob/main/src/ng-jest-transformer.ts which I think should improve further. |
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: