The Real Fullstack

The Memory That Never Leaves

JavaScript has a garbage collector. Why does the heap still grow until the pod dies?

工程师8 分钟调试实验室性能实验室
nodejavascriptjavakubernetes

此挑战尚未翻译为简体中文,你正在阅读英文版本。

任务:Read the evidence, then remove every retention source so the heap stays bounded at a million requests — without just raising the limit.背景 · 学习目标 ▾

Fernway's catalog service (Node.js on Kubernetes) restarts four times a day with OOMKilled. Heap usage grows linearly with traffic and never comes down. A heap snapshot was captured before the last crash — see the Logs tab.

任务: Read the evidence, then remove every retention source so the heap stays bounded at a million requests — without just raising the limit.

学习目标

  • Explain why reachable objects are never garbage-collected.
  • Find retention sources from heap-snapshot retainers.
  • Bound caches and release listeners and timers.
模拟模拟数据

运行模拟以查看输出。

运行模拟
const responseCache = new Map();             // grows with every distinct URL, never evictedapp.get('/products/:id', async (req, res) => {  const key = req.originalUrl;                // ?utm_source=… makes every key unique  if (!responseCache.has(key)) responseCache.set(key, await render(req));  priceEvents.on('price-change', () => invalidate(req));   // one listener per request, never removed  res.send(responseCache.get(key));});wss.on('connection', (socket) => {  const timer = setInterval(() => socket.ping(), 30_000);  // missing: socket.on('close', () => clearInterval(timer));});

简化说明

Heap growth is linear per request for each source. Real heaps grow in steps, fragment and include native memory. 教学模型——数字用于说明行为,并非基准测试。

概念

相关挑战

参考资料