aws-amplify / aws-amplify/amplify-codegen

`npm i` followed by `idealBuild:amplify-codegen-appsync-model-plugin`, and `amplify codegen` commands run out of memory when Query Depth is >= 5

Open
#527 1 comment 1 reaction 0 assignees View on GitHub
bug p3
Dominant language
TypeScript
Stars
59
Forks
64
PR merge metrics
No merged PRs in 30d

Description

### Before opening, please confirm:

- [X] I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
- [X] I have [searched for duplicate or closed issues](https://github.com/aws-amplify/amplify-codegen/issues?q=is%3Aissue+).
- [X] I have read the guide for [submitting bug reports](https://github.com/aws-amplify/amplify-codegen/blob/main/CONTRIBUTING.md#bugs).
- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.

### How did you install the Amplify CLI?

npm

### If applicable, what version of Node.js are you using?

v16.19.0
`npm -v 8.19.3`

### Amplify CLI Version

N/A

### What operating system are you using?

Ubuntu 22.04.1 LTS

### Amplify Codegen Command

Not applicable

### Describe the bug

When running `npm i` on a large project `idealTree:amplify-codegen-appsync-model-plugin` hangs for several minutes, causes fans to spin up heavily, uses 1.8GB of memory and eventually fails due to out of memory exception. In addition, once it starts ctrl+c doesn't kill the process, terminals to be closed and process manually killed. Finally, the resulting JavaScript file also is massive e.g. 20+ Megabytes. If left for about 15-20 minutes, ultimately build fails with `FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory`

![image](https://user-images.githubusercontent.com/1417045/208246294-53a341e4-915c-4c25-9fda-52b6df4ff9e3.png)

![image](https://user-images.githubusercontent.com/1417045/208246307-76974447-1849-485b-85dd-48ef202c7058.png)

This seems to not occur when using yarn instead of npm, however running an `amplify codegen` locally to generate the javascript file (with a query depth of 6), also causes the out of memory exception. The only solution is to increase the javascript heap size:

```
export NODE_OPTIONS=--max_old_space_size=4096
```

Without doing this the process also crashes, so there is definitely a memory leak in the codegen, I actually had to increase this to 6GB to even get it to complete.

### Expected behavior

`npm i` works on my project and `amplify-codegen-appsync-model-plugin` works/completes
`amplify codegen` works with less than 4GB dedicated memory to node process

### Reproduction steps

1. run `npm i` on a project with a large schema and larger query depth
2. watch it hang on `idealTree:amplify-codegen-appsync-model-plugin` (ctrl+c no longer breaks either)
3. try and force quit `npm i` process

### GraphQL schema(s)

Can share privately if needed.

### Log output

```
$ amplify codegen
✔ Generated GraphQL operations successfully and saved at src/graphql
⠋ Generating

<--- Last few GCs --->

[64889:0x4486d70] 34353 ms: Mark-sweep (reduce) 2041.6 (2052.1) -> 2041.4 (2053.6) MB, 726.2 / 0.0 ms (average mu = 0.187, current mu = 0.111) allocation failure GC in old space requested
[64889:0x4486d70] 35084 ms: Mark-sweep (reduce) 2041.9 (2052.6) -> 2041.8 (2053.9) MB, 730.4 / 0.0 ms (average mu = 0.093, current mu = 0.002) allocation failure GC in old space requested

<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
1: 0x8de370 node::Abort() [/home/michael/.amplify/bin/amplify]
2: 0x82772b node::FatalError(char const*, char const*) [/home/michael/.amplify/bin/amplify]
3: 0x9f2a76 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/home/michael/.amplify/bin/amplify]
4: 0x9f2d0d v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/home/michael/.amplify/bin/amplify]
5: 0xb95135 [/home/michael/.amplify/bin/amplify]
6: 0xb95b87 v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [/home/michael/.amplify/bin/amplify]
7: 0xb9e708 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/home/michael/.amplify/bin/amplify]
8: 0xba1d30 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/home/michael/.amplify/bin/amplify]
9: 0xb6b352 v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [/home/michael/.amplify/bin/amplify]
10: 0xe716af v8::internal::Runtime_AllocateInOldGeneration(int, unsigned long*, v8::internal::Isolate*) [/home/michael/.amplify/bin/amplify]
11: 0x1769439 [/home/michael/.amplify/bin/amplify]

```

### Additional information

`amplify-codegen-appsync-model-plugin` has a memory leak when larger (5-6) query depth chosen resulting in a huge javascript file (20+MB).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.