GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — zlib deflate results in a memory leak · Issue #8871 · nodejs/node

zlib deflate results in a memory leak #8871

New issue
Copy link
New issue
Copy link
Closed
Closed
zlib deflate results in a memory leak#8871
Copy link
Labels
confirmed-bugIssues and PRs for confirmed bugs.memoryIssues and PRs related to Node.js memory management or memory footprint.zlibIssues and PRs related to the zlib module and its compression dependencies.

Description

@jasonmcaffee
jasonmcaffee
opened on Sep 30, 2016
Issue body actions
  • Version:6.7.0
  • Platform:Darwin ITs-MacBook-Pro.local 15.6.0 Darwin Kernel Version 15.6.0: Thu Jun 23 18:25:34 PDT 2016; root:xnu-3248.60.10~1/RELEASE_X86_64 x86_64
  • Subsystem:zlib

I'm using the graylog2 package for logging, and we ran into significant memory leak issues.
After tracking it down, I found zlib.deflate is the source of the issue.
The issue is magnified when running code inside of docker with the latest node distribution.

Running the below code on my macbook pro results in the memory spiking to ~3GB, then released down to 600MB.
Running the code in the latest node docker distro results in memory spiking to ~3GB, and it is never released.

let zlib = require('zlib');

let message = {
  some:"data"
};
let payload = new Buffer(JSON.stringify(message));

for(var i =0; i < 30000; ++i){
  zlib.deflate(payload, function (err, buffer) {
  });
}

setTimeout(()=>{}, 2000000);

This has resulted in our docker containers crashing due to memory exhaustion.

Reacted by TylerBrock, Joran Dirk Greef, ixcviw7bw, Aaron Bell, Antoine Pultier, Matthew Miner, Rob Cowsill, Ilia Sidorenko, bl-ue, Paulo Coghi and 7 more

Activity

Next
  1. addaleax
    added
    zlibIssues and PRs related to the zlib module and its compression dependencies.
    memoryIssues and PRs related to Node.js memory management or memory footprint.
    on Sep 30, 2016
  2. mscdex commented on Sep 30, 2016

    @mscdex
    mscdex
    on Sep 30, 2016
    Contributor
    More actions

    Are you sure the node version is the same between the two?

  3. jasonmcaffee commented on Sep 30, 2016

    @jasonmcaffee
    jasonmcaffee
    on Sep 30, 2016
    Author
    More actions

    Yes. I've also recreated the issue on v4.5 and 5.12.

  4. smyt1 commented on Sep 30, 2016

    @smyt1
    smyt1
    on Sep 30, 2016
    More actions

    @jasonmcaffee have you experimented with the sync version zlib.deflateSync? I see the same thing with the async version but the sync version seems more tame

  5. bnoordhuis commented on Oct 1, 2016

    @bnoordhuis
    bnoordhuis
    on Oct 1, 2016
    Member
    More actions

    The loop is creating 30,000 concurrent zlib.deflate requests that are handed off to a threadpool; i.e., it's creating a giant backlog. They are dispatched eventually and that is why memory goes down again on OS X.

    On Linux (or more precisely, with glibc), something else happens. When you run it through strace -cfe mmap,munmap,mprotect,brk you can see what is going on:

    % time     seconds  usecs/call     calls    errors syscall
    ------ ----------- ----------- --------- --------- ----------------
     99.63    3.803199          32    119985           brk
      0.20    0.007680          12       632           mmap
      0.12    0.004748           7       675           munmap
      0.05    0.001882          30        62           mprotect
    ------ ----------- ----------- --------- --------- ----------------
    100.00    3.817509                121354           total
    

    Node.js uses malloc/new and glibc translates those overwhelmingly to brk system calls. The problem is that the "program break" goes up but a few allocations with longer lifetimes make it impossible for the break to go down again. There is no real memory leak, it's ordinary - but rather catastrophic - memory fragmentation.

    IOW, can confirm but node.js is not the root cause. Maybe we can sidestep it by using a custom allocator like jemalloc but that has a lot of ramifications, it's not something we can do lightly.

    Reacted by Nikita Skovoroda, Gibson Fahnestock, Andras, sevastos, Nico, Ali Rahbari, Erik Landvall, Mayank Asthana, Brian M Hunt, Greg Kubisa and 19 more
  6. bnoordhuis
    added
    confirmed-bugIssues and PRs for confirmed bugs.
    on Oct 1, 2016
  7. jasonmcaffee commented on Oct 3, 2016

    @jasonmcaffee
    jasonmcaffee
    on Oct 3, 2016
    Author
    More actions

    Thanks @bnoordhuis for the breakdown and explanation.

  8. kevinburke commented on Jan 30, 2017

    @kevinburke
    kevinburke
    on Jan 30, 2017
    More actions

    FWIW, we just fixed a problem that had similar symptoms, and our solution was to disable transparent huge pages. I'd encourage you to investigate and see if a) they are turned on, and b) if disabling them fixes your problem.

  9. jorangreef commented on Jun 30, 2017

    @jorangreef
    jorangreef
    on Jun 30, 2017
    More actions

    Could this be an issue if multiple clients are triggering async deflate calls within a few ms of each other? Should people just avoid deflate() all together?

  10. jorangreef commented on Jun 30, 2017

    @jorangreef
    jorangreef
    on Jun 30, 2017
    More actions

    Does this go much further than just deflate()? Could this be affecting most async calls which allocate memory on Linux?

    Reacted by shitpoet
  11. TylerBrock commented on Jul 19, 2017

    @TylerBrock
    TylerBrock
    on Jul 19, 2017
    More actions

    Does anyone know which versions of node do not have this bug? We are seeing this on latest Node8 but also on all versions of 6 that I sampled.

    I run the code snippet above in node invoked with --expose-gc and call global.gc() to free the memory but 3gb is still resident no matter what I do. This is killing us in production as we have to restart our containers every couple of hours.

    Reacted by Vic C, Paulo Coghi, Byong-Wu Chong and shitpoet
  12. bnoordhuis commented on Jul 19, 2017

    @bnoordhuis
    bnoordhuis
    on Jul 19, 2017
    Member
    More actions

    @TylerBrock Did you read through this issue and the linked issues? If you did, then you already know the answer.

    Reacted by Jeremy, Qwerty (Vítězslav Ackermann Ferko), HarbingerNight, Andrew Nester, vladyslav-huba-lanars, p0358, Davo, Paulo Coghi, Stillward, Szymon Marczak and 8 more
    Reacted by Alexey Sh
  13. TylerBrock commented on Jul 20, 2017

    @TylerBrock
    TylerBrock
    on Jul 20, 2017
    More actions

    I did but I'm still not sure so I apologize if I missed something.

    People have suggested it could be new zlib, new v8, or something in crypto (possibly related to zlib) but going back to versions of node having older zlib and v8 (checked via process.versions) yielded similar results.

    Would you mind summarizing where we are at?

  14. TimothyGu commented on Jul 20, 2017

    @TimothyGu
    TimothyGu
    on Jul 20, 2017
    Member
    More actions

    @TylerBrock

    From #8871 (comment):

    There is no real memory leak, it's ordinary - but rather catastrophic - memory fragmentation.

    IOW, can confirm but node.js is not the root cause. Maybe we can sidestep it by using a custom allocator like jemalloc but that has a lot of ramifications, it's not something we can do lightly.

    In other words, this is not a Node.js, or zlib, or V8 issue, but rather caused by how the system memory allocator works.

    Reacted by Qwerty (Vítězslav Ackermann Ferko)
  15. 62 remaining items

  16. alestor123 commented on Jul 30, 2020

    @alestor123
    alestor123
    on Jul 30, 2020
    More actions

    I should try with jemalloc on Linux.

    @lpinca I've tried that with jemalloc 3.6.0-11 and here is the output:

    $ LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.1 ./node zlib.js
    testLeak: 13.778s
    Memory: {
      rss: 334114816,
      heapTotal: 131715072,
      heapUsed: 103527424,
      external: 472453668,
      arrayBuffers: 471975702
    }
    

    Try using - zlib-bug-inflate

  17. lpinca commented on Jul 30, 2020

    @lpinca
    lpinca
    on Jul 30, 2020
    Member
    More actions

    @puzpuzpuz

    That's without the mitigation patch right? I also tried with jemalloc and there is an order of magnitude difference (~40 MB with the patch and ~400 MB without it).

  18. puzpuzpuz commented on Jul 30, 2020

    @puzpuzpuz
    puzpuzpuz
    on Jul 30, 2020
    Member
    More actions

    @lpinca no, that's with the patch. I may be doing something wrong, as I don't see an order of magnitude difference.

  19. lpinca commented on Jul 30, 2020

    @lpinca
    lpinca
    on Jul 30, 2020
    Member
    More actions

    Nvm it seems it was me that did something wrong:

    luigi@ubuntu:~/leak$ node -v
    v14.7.0
    luigi@ubuntu:~/leak$ env LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.1 node index.js
    testLeak: 57.830s
    Memory: {
      rss: 46489600,
      heapTotal: 4251648,
      heapUsed: 2202536,
      external: 998285,
      arrayBuffers: 17590
    }
    luigi@ubuntu:~/leak$ nvm use 14.6.0
    Now using node v14.6.0 (npm v6.14.6)
    luigi@ubuntu:~/leak$ node -v
    v14.6.0
    luigi@ubuntu:~/leak$ env LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.1 node index.js
    testLeak: 1:24.327 (m:ss.mmm)
    Memory: {
      rss: 49336320,
      heapTotal: 3989504,
      heapUsed: 2189736,
      external: 997652,
      arrayBuffers: 17590
    }
    

    It seems jemalloc is a working workaround.

  20. puzpuzpuz commented on Jul 30, 2020

    @puzpuzpuz
    puzpuzpuz
    on Jul 30, 2020
    Member
    More actions

    It seems jemalloc is a working workaround.

    @lpinca you probably mean that jemalloc works more or less like #34048 when used with pre-14.7.0 versions of node, right? At least, that's how it looks like.

  21. lpinca commented on Jul 30, 2020

    @lpinca
    lpinca
    on Jul 30, 2020
    Member
    More actions

    Better than that, I mean that with jemalloc there is no leak at all.

    luigi@ubuntu:~/leak$ node -v
    v14.7.0
    luigi@ubuntu:~/leak$ node index.js
    testLeak: 1:11.205 (m:ss.mmm)
    Memory: {
      rss: 700317696,
      heapTotal: 132943872,
      heapUsed: 101497112,
      external: 493479181,
      arrayBuffers: 492498486
    }
    luigi@ubuntu:~/leak$ env LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.1 node index.js
    testLeak: 57.279s
    Memory: {
      rss: 49631232,
      heapTotal: 9494528,
      heapUsed: 2323328,
      external: 998285,
      arrayBuffers: 17590
    }
    luigi@ubuntu:~/leak$ nvm use 14.6
    Now using node v14.6.0 (npm v6.14.6)
    luigi@ubuntu:~/leak$ node -v
    v14.6.0
    $ env LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.1 node index.js
    testLeak: 1:24.564 (m:ss.mmm)
    Memory: {
      rss: 51523584,
      heapTotal: 4513792,
      heapUsed: 2273040,
      external: 997652,
      arrayBuffers: 17590
    }
    
  22. puzpuzpuz commented on Jul 30, 2020

    @puzpuzpuz
    puzpuzpuz
    on Jul 30, 2020
    Member
    More actions

    Better than that, I mean that with jemalloc there is no leak at all.

    Strictly speaking memory fragmentation is not a leak and the allocator should be able to get rid of it when the allocation rate gets lower and fewer allocated memory chunks remain. If jemalloc doesn't have a noticeable fragmentation in the discussed scenario, that's great.

    As a summary, it would be great to get some feedback from the community on v14.7.0 (which includes #34048) and v14.7.0 (or any previous version) with jemalloc.

    Reacted by Luigi Pinca, Anna Henningsen and Alexis Tyler
  23. lpinca commented on Jul 30, 2020

    @lpinca
    lpinca
    on Jul 30, 2020
    Member
    More actions

    Yes "leak" is not the correct term in this case. My point is that with jemalloc memory usage remains pretty much stable regardless of how many times the testLeak() function in the above example is run. This is not true with glibc even with #34048 applied.

    Reacted by Andrei Pechkurov, Pablo Moleri, Yunyu Lin, Alexis Tyler, Mikko Rantalainen and Che Fisher
  24. intellix commented on Oct 23, 2023

    @intellix
    intellix
    on Oct 23, 2023
    More actions

    How does this look in 2023 with Node 18+ ? There's no comments on this issue for 3 years so I'm wondering if it's safe to use permessage-deflate to gzip websockets and was pointed to this issue.

  25. STRML commented on Oct 23, 2023

    @STRML
    STRML
    on Oct 23, 2023 via email
    Contributor
    More actions
    I don’t know. We’ve moved to another solution. You would have to test it to be sure.
    …
    On Mon, Oct 23, 2023 at 10:39 AM D ***@***.***> wrote: How does this look in 2023 with Node 18+ ? There's no comments on this issue for 3 years so I'm wondering if it's safe to use permessage-deflate to gzip websockets and was pointed to this issue. — Reply to this email directly, view it on GitHub <#8871 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AAJEKP5IML76FCNMS77LAR3YAZ6RLAVCNFSM4CROFDHKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCNZXGUZTKNJZGM2Q> . You are receiving this because you were mentioned.Message ID: ***@***.***>
  26. cyantree commented on Dec 3, 2023

    @cyantree
    cyantree
    on Dec 3, 2023
    More actions

    I've run the test from #8871 (comment)
    on my machine with the following results:

    • Win 10
    • Ryzen 7 1800X
    • 32GB RAM
    testLeak: 4:14.889 (m:ss.mmm)
    Memory: {
      rss: 95129600,
      heapTotal: 5390336,
      heapUsed: 3328040,
      external: 1524293,
      arrayBuffers: 18659
    }
    

    During the test the process memory usage consistently oscillated between around 600 MB and 6,4 GB.

    Reacted by kai zhu
  27. titanism
    added a commit that references this issue on Jul 12, 2024

    fix: increase cache for storage quota from 10s to 1d, disabled perMes…

    15340b9
  28. davidje13 commented on Nov 5, 2025

    @davidje13
    davidje13
    on Nov 5, 2025
    Contributor
    More actions

    Since this was originally reported on mac, and reported to be worse on Linux, I've run an adapted version of the updated reproduction on a MacBook (48GB RAM) and a Raspberry Pi (8GB RAM), using standard Node.js (i.e. not using jemalloc):

    import { promisify } from 'util';
    import { deflate } from 'zlib';
    
    const deflatePromise = promisify(deflate);
    const payload = Buffer.from(JSON.stringify({ some: 'data' }));
    
    async function testLeak() {
      const promises = [];
      for (let i = 0; i < 30000; i++) {
        promises.push(deflatePromise(payload));
      }
      await Promise.all(promises);
    }
    
    for (let i = 0; i < 50; i++) {
      await testLeak();
      await new Promise((resolve) => setTimeout(resolve, 2000));
      console.log((process.memoryUsage().rss / 1000000).toFixed() + ' MB');
    }

    Results:

    Image

    Memory useage clearly increases for the first few runs, but it's not unbounded on either system, so it does appear the original issue of fragmentation has been resolved (somewhere; not necessarily in Node.js itself).

    I think it would be safe to close this issue.

    Reacted by Gürgün Dayıoğlu
  29. gurgunday commented on Nov 5, 2025

    @gurgunday
    gurgunday
    on Nov 5, 2025
    Member
    More actions

    Closing

    Feel free to comment if anyone still has this issue

  30. gurgunday
    closed this as completedon Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues and PRs for confirmed bugs.memoryIssues and PRs related to Node.js memory management or memory footprint.zlibIssues and PRs related to the zlib module and its compression dependencies.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.