Skip to content
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

jest-worker farm memory leak #11633

Closed
IIIEII opened this issue Jul 2, 2021 · 4 comments · Fixed by #12497
Closed

jest-worker farm memory leak #11633

IIIEII opened this issue Jul 2, 2021 · 4 comments · Fixed by #12497

Comments

@IIIEII
Copy link
Contributor

IIIEII commented Jul 2, 2021

Parent process continuously grow memory usage while executing multiple child worker processes that return large chunks of data.

I prepared sample repository to reproduce issue: https://github.com/IIIEII/jest-worker-farm-leak
It has worker that returns large array with strings.
Parent process executes it three times with pause of 10s between.

Steps:

  1. Run sample script in inspect mode node --inspect --expose-gc src/parent.js
  2. Connect with chrome inspector and record allocation timeline

Снимок экрана 2021-07-02 в 16 14 52

3. Here you can see that data returned from worker are stored in memory

It seems that problem lies here: https://github.com/facebook/jest/blob/7a64ede2163eba4ecc725f448cd92102cd8c14aa/packages/jest-worker/src/Farm.ts#L136-L138

Possible solution:

  1. Make change to node_modules/jest-worker/build/Farm.js file:
Index: node_modules/jest-worker/build/Farm.js
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/node_modules/jest-worker/build/Farm.js b/node_modules/jest-worker/build/Farm.js
--- a/node_modules/jest-worker/build/Farm.js	(date 1625231320301)
+++ b/node_modules/jest-worker/build/Farm.js	(date 1625231320301)
@@ -140,10 +140,11 @@
     // and other properties of the task object, such as task.request can be
     // garbage collected.
 
-    const taskOnEnd = task.onEnd;
+    let taskOnEnd = task.onEnd;
 
     const onEnd = (error, result) => {
       taskOnEnd(error, result);
+      taskOnEnd = null;
 
       this._unlock(workerId);
 
  1. Run sample script in inspect mode node --inspect --expose-gc src/parent.js
  2. Connect with chrome inspector and record allocation timeline

Снимок экрана 2021-07-02 в 16 19 01

4. Here you can see that data are in GC roots
@sibelius
Copy link

@IIIEII can you send a pull request to fix this?

can you provide a patch-package (https://github.com/ds300/patch-package) so more people can try your solution?

@SimenB
Copy link
Member

SimenB commented Feb 23, 2022

Very much happy to take a PR changing this like suggested

@SimenB
Copy link
Member

SimenB commented Mar 1, 2022

@github-actions
Copy link

github-actions bot commented Apr 1, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants