Skip to content

jest-worker farm memory leak #11633

Closed

Description

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions