Skip to content
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

next/og cause memory leak in production standalone build #65451

Open
Innei opened this issue May 7, 2024 · 1 comment
Open

next/og cause memory leak in production standalone build #65451

Innei opened this issue May 7, 2024 · 1 comment
Labels
bug Issue was opened via the bug report template. Image (next/image) Related to Next.js Image Optimization.

Comments

@Innei
Copy link

Innei commented May 7, 2024

Link to the code that reproduces this issue

https://github.com/Innei/next-og-oom-repro

To Reproduce

  1. build project
  2. run as standalone build node server.js
  3. open /og
  4. open devtools and force refresh the page(ignore cache) more times.
  5. watch the system monitor, and the next app memory usage is increasing.

The initial memory usage about to 50M, and refresh /og about 10 times, got 300M
CleanShot 2024-05-07 at 8  26 37@2x

CleanShot 2024-05-07 at 8  30 54@2x

I can provide some ways to try to troubleshoot memory issues.

Add memory dump code in .next/standalone/server.js

process.title = 'next-og-oom'

const v8 = require('v8')
const fs = require('fs')

function createHeapSnapshot() {
  const snapshotStream = v8.getHeapSnapshot()
  const timestamp = new Date().toISOString().replace(/[:\.]/g, '-')
  const fileName = `/tmp/${timestamp}.heapsnapshot`
  const fileStream = fs.createWriteStream(fileName)
  snapshotStream.pipe(fileStream).on('finish', () => {
    console.log('Heap snapshot saved to', fileName)
  })
}

process.on('SIGUSR2', () => {
  console.log('SIGUSR2 received, creating heap snapshot...')
  createHeapSnapshot()
})

And then refresh the page and refresh the page several times and observe the app memory usage afterwards. When memory overflows and is not freed, hit the heap of memory at that point with kill -SIGUSR2 <pid>.

As you can see from the following dump, it's the ImageResponse-related modules that are leaking memory. ImageResponse and FigmaImageResponse that means @vercel/og causes memory leak?

Link to #44685 (comment).

Current vs. Expected behavior

  1. Memory does not free up and increases more and more, finally oom.

expected:

Memory can be freed up.

Provide environment information

Operating System:
  Platform: darwin/linux
  Arch: arm64/amd64
  Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020/ or Linux
  Available memory (MB): 32768
  Available CPU cores: 12
Binaries:
  Node: 18.18.0/20.x
  npm: 10.2.4
  Yarn: 1.22.21
  pnpm: 9.1.0
Relevant Packages:
  next: 14.2.3 // Latest available version is detected (14.2.3).
  eslint-config-next: N/A
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.4.5
Next.js Config:
  output: standalone

Which area(s) are affected? (Select all that apply)

Image (next/image)

Which stage(s) are affected? (Select all that apply)

Other (Deployed)

Additional context

No response

@Innei Innei added the bug Issue was opened via the bug report template. label May 7, 2024
@github-actions github-actions bot added the Image (next/image) Related to Next.js Image Optimization. label May 7, 2024
@Innei Innei changed the title Maybe next/og cause memory leak in production standalone build next/og cause memory leak in production standalone build May 7, 2024
@Innei
Copy link
Author

Innei commented May 7, 2024

Attachment: 2024-05-07T12-30-03-488Z.heapsnapshot.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Image (next/image) Related to Next.js Image Optimization.
Projects
None yet
Development

No branches or pull requests

1 participant