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. نموذج تعليمي — الأرقام توضّح سلوكًا وليست قياسات أداء.

المفاهيم

تحديات ذات صلة

المراجع