-
Notifications
You must be signed in to change notification settings - Fork 4k
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
aws-cdk-lib: significantly more memory used as module than commonjs #32624
Comments
For a more realistic test I converted the TypeScript sample app to ESM and CJS here ~/github/cdk-test(±cjs) $ node --expose-gc -r ./log-memory.cjs bin/cdk-test.mjs
rss 128.20mb heap 59.91mb
~/github/cdk-test(±cjs) $ node --expose-gc -r ./log-memory.cjs bin/cdk-test.cjs
rss 94.88mb heap 33.03mb ~/github/cdk-test(±cjs) $ hyperfine -L EXT cjs,mjs 'node --expose-gc -r ./log-memory.cjs bin/cdk-test.{EXT}'
Benchmark 1: node --expose-gc -r ./log-memory.cjs bin/cdk-test.cjs
Time (mean ± σ): 244.6 ms ± 5.5 ms [User: 260.3 ms, System: 85.3 ms]
Range (min … max): 234.3 ms … 252.3 ms 12 runs
Benchmark 2: node --expose-gc -r ./log-memory.cjs bin/cdk-test.mjs
Time (mean ± σ): 426.4 ms ± 7.1 ms [User: 440.7 ms, System: 149.4 ms]
Range (min … max): 415.4 ms … 440.7 ms 10 runs
Summary
node --expose-gc -r ./log-memory.cjs bin/cdk-test.cjs ran
1.74 ± 0.05 times faster than node --expose-gc -r ./log-memory.cjs bin/cdk-test.mjs |
By adding a resolve hook we can see that mjs loads about twice as many files
|
Replacing
|
I'm guessing this is an interaction between lazify and node's ESM CJS interop. I think node enumerates all properties on the CJS export to discover named imports, which triggers the lazy load of sub modules. The reason I'm looking into this is that I'm seeing Jest tests running out of memory after upgrading from node 18 to 20. I'm seeing it across different packages and aws-cdk-lib is the common factor. While upgrading to node 20 is increasing the OOM errors, there's evidence that the packages are seeing high memory usage with node 18, like limiting test parallelism or increasing node's memory. |
Thank you. We'll bring this up to the core team's visibility. |
Describe the bug
test.mjs and .cjs are the same except
import 'aws-cdk-lib
vsrequire('aws-cdk-lib)
.I see similar differences when importing
aws-cdk-lib/core
oraws-cdk-lib/aws-lambda
.I don't see similar issues with other large libraries (like @aws-sdk/client-ec2)
Regression Issue
Last Known Working CDK Version
No response
Expected Behavior
Similar memory use between ESM and CJS
Current Behavior
importing cdk uses more memory than requiring it.
Reproduction Steps
test.mjs uses significantly more heap and rss
Possible Solution
No response
Additional Information/Context
No response
CDK CLI Version
N/A
Framework Version
2.173.2
Node.js Version
18, 20, 22
OS
Arch Linux
Language
TypeScript
Language Version
N/A (JavaScript)
Other information
No response
The text was updated successfully, but these errors were encountered: