rsnext/packages/next/server/lib
Jimmy Lai 662344c3a5
next-dev: restart dev server exceeds the memory limits (#43958)
## Context

There has been [some reports ](https://github.com/vercel/next.js/issues/42514)of OOMs-related crashes with Next 13. Whilst we're fixing the memory leaks that are causing this, some of which are caused by upstream issues, this PR makes Next.js' dev server restarts if it detects that it is gonna crash soon.

You can disable this behaviour by passing `__NEXT_DISABLE_MEMORY_WATCHER=1` to the env process.

## Details

Under the hood, we're using Node's cluster API to create a child worker that will basically watch the memory usage after every request and then kill itself if it goes over 90% of the maximum heap allowance.


## Test plan

I added manually a  leaking function that I called before handling a request. I then manually tested that the server re-started when we were near the limit.

```

function createMemoryLeak() {
  console.log('createMemoryLeak', process.memoryUsage().heapUsed / 1024 / 1024)
  for (let i = 0; i < 10; i++) {
    buffer.push(new Array(1000000).fill('a'))
  }
}

```


## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-12-12 18:54:55 +00:00
..
incremental-cache Update cache handling for app (#43659) 2022-12-02 23:32:49 -08:00
squoosh Replace global with globalThis (#42627) 2022-11-08 14:45:30 +01:00
etag.ts refactor: rewrite etag (#38568) 2022-07-18 09:20:44 +00:00
find-page-file.ts Ensure backslash is correctly handled in find-page-file (#43057) 2022-11-17 17:30:01 -08:00
mock-request.ts Update on-demand ISR to skip fetch locally (#35386) 2022-03-17 12:06:44 -05:00
recursive-readdir-sync.ts Move next-server directory files to server directory (#26756) 2021-06-30 13:44:40 +02:00
start-server.ts next-dev: restart dev server exceeds the memory limits (#43958) 2022-12-12 18:54:55 +00:00
utils.ts Allow port 0 in next dev and next start (#40118) 2022-08-31 17:13:39 -05:00