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
[Webpack 5]: cache leak with incremental compilation #13127
Comments
Sounds like a memory leak somewhere. Difficult to find without access to the repo or a memory profile. But I think the huge memory usage originates from mini-css-extract-plugin and we have an PR open to add an experimental largely more memory efficient mode. I'm looking for testers and your case seems like a good fit. Here is the PR: webpack-contrib/mini-css-extract-plugin#737 |
@nigel-codaio Maybe you can provide example of repo, so we can investigate memory leak |
@sokra -- looks like webpack-contrib/mini-css-extract-plugin#737 was released with v1.5.0. I'll try pulling this in to see if it helps. @alexander-akait -- I haven't had luck trimming this down but should be able to set aside time to investigate this week. |
I tried the latest mini-css-extract-plugin (v1.5.0) with today's webpack5 release (5.34.0) and still hit the issue. @sokra it definitely appears related to the mini-css-extract plugin as editing non-less files doesn't exhibit the memory leak. Sharing the full project is challenging as this is a commercial codebase. Are there any guides or instructions on how I might perform memory profile? Any best practices to follow? I'm game to dig in but don't know how to start. Thanks! |
@nigel-codaio Maybe you can provide full configuration when memory leak? I think you have non official plugin(s) with leaking |
Small notes - |
maybe fixed by #13184 |
Hi @sokra and @alexander-akait -- thanks for the suggestions. I made the following changes today:
With these changes in place, things appear to be working better. @sokra I also pulled in #13184 and tried bundling with that. This also gives some improvement and appears to have a lower memory cap than without it. What's the recommendation of filecache vs. memory cache? Our production builds don't use any form of caching currently so I'm mostly focused on webpack devserver behavior with an eye on incremental compilation cost. Is there a discussion anywhere on the tradeoffs between the caching approaches, and a best-practice example on the optimal config for dev? Thanks. |
You can use the filesystem cache for production and development. The filesystem cache includes a memory cache to avoid reading/writing to disk too often. |
@sokra The filesystem cache includes a memory cache or you mean |
|
@sokra so you mean even though we write cache type as filesystem there will be things stored in memory as well? |
Yes, memory cache is faster. So for incremental builds that's faster. But cache items only stay for a few generations in the memory cache. See |
@sokra The memory consumption stays low if I give cache type as 'memory' but when I give it as filesystem the memory usage is very high and the process crashes in just a few minutes. I'm sticking with cache type memory for now. |
@vijaybritto which webpack version? |
5.35.1 |
@vijaybritto Can you try latest version? |
@vijaybritto can you share your original webpack config? |
…ng builds This reduce memory consumption during re-builds. ``` runtime.ba93f81591909b93394f.hot-update.js.map will be removed styles.ba93f81591909b93394f.hot-update.js.map will be removed runtime.ba93f81591909b93394f.hot-update.json will be removed runtime.ba93f81591909b93394f.hot-update.js will be removed styles.ba93f81591909b93394f.hot-update.js will be removed ``` See webpack/webpack#12947 (comment) and webpack/webpack#13127 (cherry picked from commit f5f41ea)
…ng builds This reduce memory consumption during re-builds. ``` runtime.ba93f81591909b93394f.hot-update.js.map will be removed styles.ba93f81591909b93394f.hot-update.js.map will be removed runtime.ba93f81591909b93394f.hot-update.json will be removed runtime.ba93f81591909b93394f.hot-update.js will be removed styles.ba93f81591909b93394f.hot-update.js will be removed ``` See webpack/webpack#12947 (comment) and webpack/webpack#13127
@nigel-codaio The problem still exists? |
@alexander-akait @sokra we observe the similar issue with our application. Memory consumption with I was able to make a memory snapshot with Chrome, here is a screenshot. Lmk what other info you need, I can probably share the entire snapshot if you give me your email. |
@kanoshin What is webpack version? |
@alexander-akait 5.38.1 |
The html-webpack-plugin stores a singleton of This singleton stores a the result of a child compiler: |
@jantimon this is memory leak... |
Let's call it a "cache". It doesn't leak a infinite amount of memory, only the latest compilation used for html generation. Maybe we can reduce the memory usage a bit by not referencing |
I also can't see any leaks here. But we can definitely try to improve the cache size :) Maybe I should explain why this cache was introduced at all: Previously the html-webpack-plugin child compiler would compile on every watch run even if no relevant file was changed. |
@jantimon Why we need to keep whole compilation, maybe we can cache only related stuff? |
@kanoshin Can you test again, please update webpack https://github.com/webpack/webpack/releases/tag/v5.42.0 and less-loader https://github.com/webpack-contrib/less-loader/releases/tag/v10.0.1 |
I've tried it. I see no memory growth when modifying both TS and LESS modules. We had +100Mb per modification of all files in package once. Right now it is gone 👍 However, when I modify LESS files there's an occasinal error in the debugger. It crashes the process. This is it:
I remember no such error in the previous tests (before today's fixes). May it be caused by the fixes? |
Yes, regression on our side, please wait, we will fix it in near future |
Right now the following is cached:
ChildCompilationResultEntry is coming from: https://github.com/jantimon/html-webpack-plugin/blob/8f8f7c53c4e4f822020d6da9de0304f8c23de08f/lib/child-compiler.js#L189-L192
I assume that entries might keep the entire compilation in memory or I missed something |
@jackfranklin yes
above trace where they stored |
@StreetStrider It can be any non official plugin, please provide profiling |
thanks I will look soon |
I open-sourced a tool to easily analyse these heapsnapshots: https://github.com/sokra/heapdump-analyser NODE_OPTIONS=--max_old_space_size=16000 npx heapdump-analyser /path/to/file.heapsnapshot Compilation This shows the retainer graphs for all |
@StreetStrider I can't see a leak in your profiles... PS: It would be better to have 3 profiles when analysing differences between profiles |
@sokra thanks, I was not exactly sure. Increasing in delta does not mean leak directly, but the numbers seemed suspicious to me. How to prepare the third profile? Usually I take 1st profile, then make some actions, then take 2nd. Do I need to just add another round to that process? |
Another round. Then view the 3rd profile with |
So you see objects that were created by an rebuild that are still alive after another rebuild |
This issue had no activity for at least three months. It's subject to automatic issue closing if there is no activity in the next 15 days. |
Issue was closed because of inactivity. If you think this is still a valid issue, please file a new issue with additional information. |
Hi folks, we've discovered this same memory leak and I can reproduce it with a vanilla Webpack/HtmlWebpackPlugin setup, repo here: https://github.com/helloitsjoe/webpack-memory-leak I'm happy to open a new issue here or in I wrote details in the README, and some of this may be restating points above, but here's a summary: In HtmlWebpackPlugin, Here are some relevant screenshots, also in the readme, let me know if I can help more or provide more info: We can see that each key in that WeakMap has a reference to the |
I've opened #17851 to track this and believe I have a fix, I will have a PR ready shortly. |
Bug report
What is the current behavior?
We recently upgraded from webpack4 to webpack5 and are experiencing memory leaks when using webpack5 caching.
We have the following caching policy defined:
When we launch webpack devserver the VM usage climbs to about 2.5GB and then stabilizes. The bundle outputs in about 70s. When we mutate a source file, this correctly triggers a recompile, and memory doubles before hitting our max cap of 6GB. The process shortly OOM's after that.
We've tried various options for the filesystem cache including maxAge, maxGeneration, etc. all with no difference. I also tried removing contentHashs and setting
output: {clean: true}
to no avail. The only thing that does help is disabling caching completely (cache=false
) but that causes a horrible regression in incremental compilation times.Here's a snapshot of our running webpack5 config:
Here's sample output during the compile:
Versioning information:
webpack: 5.32.0
webpack-cli: 4.5.0
webpack-dev-server: ^.11.2
I noticed similar issues reported in #12947. The proposal there was to use a memory cache and set
output.clean
. I've tried that with{type: "memory", maxGenerations: 1}
andoutput.clean
, as well as stripping contentHashes. I still see the issue.Anything else I can do to help narrow down the underlying issue? Any workaround that would
maintain caching without the OOM?
Thanks,
Nigel.
If the current behavior is a bug, please provide the steps to reproduce.
I'm just running:
and then mutate a source file.
What is the expected behavior?
The memory working-set should be stable and not grow without bounds.
Other relevant information:
webpack version: 5.32.0
Node.js version: 14.16.1
Operating System: MacOS (or Linux Buster)
Additional tools:
The text was updated successfully, but these errors were encountered: