node-server icon indicating copy to clipboard operation
node-server copied to clipboard

feat: introduce new option requestsPerForcedGC

Open usualoma opened this issue 2 months ago • 3 comments

Normally, this setting is not required, but for environments where GC execution is expected to be delayed, an option has been added to explicitly trigger GC every N requests.

app

import { Hono } from 'hono'
import { serve } from '@hono/node-server' 

const app = new Hono()

app.get('/', (c) => c.text('OK'))

serve(
  {
    fetch: app.fetch,
    requestsPerForcedGC: 1000, // Explicitly trigger garbage collection every 1000 requests.
  },
  (info) => {
    console.log(`Server is running on http://localhost:${info.port}`)
  }
)

runner

$ node --expose-gc dist/index.js

usualoma avatar Oct 19 '25 11:10 usualoma

Hi @yusukebe

There's no guarantee that issue #269 will be resolved, but based on our investigation so far, I believe memory is being freed when global.gc() is explicitly called. How about we try implementing this change?

usualoma avatar Oct 19 '25 11:10 usualoma

I've been thinking about this for a long time. "Clearing something every certain number of requests" is making me think nostalgically of MaxRequestsPerChild in Apache.

It may be a practical solution. But I wonder if this is a good way for "Node.js"? I checked, and Express doesn't have a function like that. So I am not sure this method is suitable for the Node.js Adapter. It's better to wait; we've got more reports about memory leaks.

yusukebe avatar Oct 31 '25 09:10 yusukebe

Hi @yusukebe, thank you for the review. Yes, I don't think it's an essential fix.

Ideally, we'd eliminate the memory increase caused by AbortSignal...

usualoma avatar Nov 01 '25 22:11 usualoma