-
Notifications
You must be signed in to change notification settings - Fork 417
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
JavaScript heap out of memory when packaging many functions #299
Comments
Hi @daniel-cottone , Can you post the function definitions from your |
Hey @HyperBrain thanks for quick response. Here's the webpack configuration:
The definitions for all 40 functions is too large to post, but I'll post an example:
They pretty much all look the same, I've clipped out VPC, authorizer, and environment config. I'm pretty confident that they're all configured correctly. |
The handlers look good. However, version 2.x did not support individual packaging (in fact it only copied the whole artifact per function). // webpack config
const nodeExternals = require('webpack-node-externals');
...
externals: [ nodeExternals() ]
... Additionally, webpack > 3.0.0 now uses a Please also check if you have set Then do a If aws-sdk should be packaged, you can either put it into your devDependencies or use # serverless.yml
custom:
webpackIncludeModules:
forceExclude:
- aws-sdk to keep it outside of your packages. |
I've made your suggested changes to webpack externals and have added the |
It also appears to be related to the fact that there are so many functions in this serverless project; if I comment out all but 5 then |
Hmmm... that sounds like a memory leak somewhere when using individual packaging. What you can try is, to increase node's heap memory limit (which is at 1.7GB by default) with: If that works, we have to find out, where exactly the memory leak comes from and if it can be fixed by reusing objects. |
That definitely seems to be the problem. I got much further along, looks like about 50% of the way through. If I bump it up to 12GB then the process finishes after about 8-10 minutes. |
Good to know - thanks for testing this 👍 . Can you adjust the title of the issue to reflect that this will happen with many functions? Then it's more clear how to reproduce it and we can find a solution. Is the workaround using the increased heap ok for you as long as there's no real fix? |
Sure thing. I think the 12GB heap size is probably a bit much; in addition to that it seems to run significantly slower than our build does currently. I'll just opt to not make use of individual packaging for now. If/when this does get fixed I can turn it on then. |
The slower runtime is expected, because it takes each webpack compile's output to determine the modules that are really needed for each function and assembles only these for the function package. That takes some time (when using |
@HyperBrain That makes sense, thanks! |
I tried rolling back versions until I found one that didn't experience this issue. I got to 2.2.2, at which point my webpack config didn't work anymore. |
@BobbieBarker Thanks for the investigation 👍 So I'm quite sure that the memory leak is somewhere in the individual packaging part (maybe the file copy). Did it also happen for you with a |
I did some experiments with node's internal profiler The outcome is, that there seem to be no critical object remnants (or leaks) in the npm install or copy steps. The only step where memory consumption increases (but is always cleaned up by the GC) is the actual zipping of the function packaged. This behavior matches the log above: It crashed for you at the webpack step! And it seemed to have loaded the ts-loader multiple times. For my tested JS project, the memory showed roughly the same fill state before and after the webpack run. So for finding the root issue, we should concentrate on the webpack step and especially typescript. Did you experience the same issue without using typescript with projects that have many functions? |
I thought a bit about the issue. A workaround could be that the plugin would run the compiles in batches of some functions at once. However I do not know, if the webpack library will free the allocated resources after the compile again. But it could be worth a try. According to the crash trace it already happened after 7 compiled - if every ts-loader line is for one function - and was at 1500 MB. The first try should be to disable some plugins in the webpack.config and check if the ts-loader might allocate all the memory. |
I hit this too after setting
I don't think I can declare anything else of significance other than having only 9 functions. Do ask tho, I'll check whatever necessary. Here's my webpack:
These might be useful:
The output after running
|
An update: it works when I set |
@Birowsky Thanks for the info 🥇 . @BobbieBarker , @daniel-cottone can you confirm, that this setting also works for you? |
@HyperBrain That setting does appear to be working for me. I'll look into using |
@daniel-cottone please share your thoughts after u succeed. I was thinking on doing a single |
@Birowsky Seems to work. Only gripe I could have is that the type checking doesn't fail fast; if you would prefer to check types before you even start the build, which could take some time, then maybe |
@daniel-cottone I've been dealing with the same issue for a couple weeks now. Using fork-ts-checker-webpack-plugin will spawn a thread per function to type check. I'm wondering if fork-ts-checker is smart enough to do just the type check for the specific lambda or it just type checks the entire project since it's based on tsconfig.json. My project has 20+ functions, fork-ts-checker spawns 20+ threads just for type checking. It works but I don't think it's necessary. |
@HyperBrain with transpileOnly: true, it starts to crash around 30+ functions |
@HyperBrain @VuBui83 I've also experienced the same problem; setting |
Maybe a solution would be to provide a PR for the ts-checker plugin that limits the number of spawned processes when using multi-compiles in webpack. |
Can someone confirme this has been improved or fixed by 5.4.0? |
I have not seen improvements with 5.4.0. I was helping out a friend on his project and I had to rollback to 5.3.5 to see some stability with the out-of-memory issue. I also had to roll back to an older webpack (4.46.0). |
@j0k3r I can confirm that the concurrency setting added in #681 works as intended after update to 5.4.0 (i.e. limits the number of concurrent compiles in the CI system thus effectively limiting the amount of necessary memory and avoiding the out-of-memory errors). Note that in my case I run it with a value of 3 in the CI build; I have it configured in
In CI, I deploy as follows: |
Switch webpack back from 5 to 4 solve this problem for me. |
I am the author of #681, my project is on-and-off dealing with 200 lambda functions. Recent updates in minor versions introduced this again, subsequent builds in the same process does linear increases in bundle time. This is further confirmed when tested with Using the My educated guess is that packages in node_modules contains side effects that webpack has no way to cleanup after bundling. Try to avoid having webpack to dip its toes into node_modules when Lambda Function Layers are available, otherwise pushing for #570 and helps rebasing maybe your only choice. EDIT: Also make sure you read webpack/webpack#6389 if you are thinking of downgrading to webpack 4. |
Can someone confirm this has been improved or fixed by 5.5.1? |
#858 surely looks interesting, I'll give it a try next week. |
#858 seems to have resolved it for us. |
Yes, my team has been trying deployments in the last weeks. I am fairly confident that the problem is at least minimized to unnoticeable even for 200+ lambdas. |
Adding --compile-concurrency 3 fixed problem for me |
@j0k3r I'm on 5.5.1 and still have this issue unfortunately |
I'm experiencing the same issue with the latest versions of both serverless-webpack ( PS I'm only using 1 function (NestJS API) and I constantly run into memory issues. |
This fix will only improve memory usage when packaging many functions, anything under ~8 functions probably won't make a difference since they will be packaged concurrently. |
We still get those with version 5.5.1. |
Got those on v5.5.5 |
@omry-hay and @eranelbaz, it would really help if you don't mind sharing more information such as the number of entries, how are they importing I am in the middle of contemplating a multi-approach, guideline/workaround thingy. I'll start by sharing related points here. From my own experience 90% of the time you are actually hitting TerserPlugin's optimization/compression stage terser/terser#164, and one or more of the followings should move you forward,
|
sadly i can't share the rest
|
Try enabling |
We were hitting this issue in a 100+ function project. Can confirm that @vicary 's solution of using serverless-layers to provide dependencies + webpack-node-externals to avoid parsing node_modules quartered our RAM usage during build and halved the build time (thank you!). Outsourcing the typechecking to fork-ts-checker-webpack-plugin helped further, but using serverless-layers + node externals was by far the biggest gain in our situation. |
@nmlynch94 Caveats if you are using If it gets too annoying, you may want to fallback to the include/exclude config of this plugin, where |
I actually just ran into this yesterday after a function deploy and was very confused as to why my layer version wasn't being set correctly. A full refresh of the stacks fixed it and I didn't investigate any further. Will def do that if we hit that problem a lot in the future. Thanks again! |
I ran into an issue, where webpack was exported as a class and it was getting cleaned up, while being used.. EDIT: |
@mbostwick Sounds like a separate issue, would you mind opening a new one and provide with enough context so we can pin it down? |
It's worth checking if you are importing somewhere in code |
This is a Bug Report
Description
I'm in the process of trying to upgrade
serverless-webpack
version from2.2.3
, where I do not experience the following issue. Our serverless configuration haspackage: invididually: true
set, and about 40 functions. When I try to upgrade to a later version ofserverless-webpack
and runsls webpack
, the build will run for about a minute and then I get the following error:If I change my serverless config to not package individually,
package: individually: false
then this error goes away. I have tested this with version3.0.0
and the latest,4.1.0
with the same results. Don't have this issue with2.2.3
.Additional Data
The text was updated successfully, but these errors were encountered: