Your app runs fine for ten minutes. Then the scroll gets sticky. After half an hour the tab hits a gigabyte. Eventually the page dies with an out-of-memory error and you have no stack trace to show for it.
This is not a render performance problem. The React DevTools Profiler will look quiet because the issue is not how often components redraw. The issue is what stays alive after they unmount. A stray reference somewhere in the JavaScript heap pins an entire tree of DOM nodes, closures, and state. The browser cannot reclaim any of it, so memory climbs until the process collapses.
Reading your source code will not reveal the leak. The bug lives in the gap between what you think unmounted and what the garbage collector actually sees. V8 only frees objects that have zero retaining paths. If a rogue event listener, an uncleared observer, or a long-lived closure holds even one pointer to a fiber or a DOM node, the whole component subtree survives. You unmount a modal, but its detached nodes remain in memory because a listener on window still points to a handler defined inside that modal.
To prove where the leak lives you need to look at the heap, not the editor.
Why the Heap Tells the Truth
Chrome DevTools gives you a direct window into what the garbage collector sees. The Memory tab can record heap snapshots: complete inventories of every object, DOM node, and closure currently held in JavaScript memory. By comparing two snapshots—one before a suspected leak and one after—you can isolate exactly which objects failed to die.
This is not abstract theory. A single leaked React component can retain thousands of detached HTMLElement objects. Those objects are no longer attached to the visible document, but JavaScript references prevent them from being collected. They show up in the comparison view with the constructor name Detached HTMLElement. When you see them multiplying, you have found your leak.
The Chrome DevTools Workflow
Start clean. Close unrelated browser tabs, disable unrelated extensions, and let your application settle into a steady state. Open Chrome DevTools, switch to the Memory tab, and select Heap snapshot. Click Take snapshot. This baseline captures your starting memory footprint.
Now perform the exact user action you suspect. Open and close that heavy modal. Mount and unmount the widget. Navigate to the route and back. Once the UI has returned to its original visual state, click the trash can icon in the Memory tab. This forces a global garbage collection pass. Temporary objects from the render cycle should drop away. Anything that remains is a real candidate for a leak.
Click Take snapshot again. You now have two photographs of memory. Change the view from Summary to Comparison. Set the comparison scope to the first snapshot. The tool will show you only what changed between the two captures, stripping away the noise of the runtime.
Sort by Delta. Look for object counts that grew. Pay special attention to constructors like Detached HTMLElement, Array, Function, or even named class instances from your own codebase. A rising delta means objects were created during your action and were not collected afterward.
Tracing the Retaining Path
When you spot a leaked element, select it. The bottom panel displays the retaining path: a chain of references that explains why this object is still alive. The chain might run from a detached div up through React internal properties, into a closure, and finally land on an event listener registered inside one of your components. That final link in the chain is your line number.
This is where you move from diagnosis to root cause. If the retaining path ends at window.addEventListener, you know a global listener is holding your component hostage. If it ends at an IntersectionObserver instance, you know an observer is still watching a node that should have been garbage collected.
Common Culprits in React
Memory leaks in React usually fall into three patterns.
Orphaned global listeners. A useEffect hooks into window or document to track scroll position, key presses, or resize events. If the effect does not return a cleanup function that calls removeEventListener, the listener survives for the lifetime of the page. Because the listener is a closure, it keeps the entire component scope alive long after React has unmounted the component.
Observers שלא נוקו. IntersectionObserver ו-ResizeObserver הם עוצמתיים, אך הם יוצרים הפניות native מחוץ לשליטתו של React. אם תצרו מופע (instantiate) של observer בתוך קומפוננטה ותשכחו לקרוא ל-disconnect() בשלב הניקוי (cleanup phase), ה-observer יחזיק ב-DOM node היעד, וה-DOM node יחזיק ב-React fibers, props ו-state.
מלכודות closure. כשאתם מגדירים פונקציה בתוך קומפוננטה ומעבירים אותה לספריית צד-שלישי, למטמון (cache) גלובלי, או אפילו ל-setTimeout, הפונקציה הזו מבצעת closure על כל משתנה בטווח הלקסיקלי (lexical scope) שלה. אם הבעלים החיצוני שומר על הפונקציה, הוא שומר גם על כל ה-scope של הקומפוננטה שלכם יחד איתה.
דפוסי ניקוי שעובדים באמת
תיקון דליפה פירושו ניתוק כל נתיב החזקה (retaining path) שמצאתם ב-snapshot.
תמיד החזירו פונקציית ניקוי מ-useEffect. אם הוספתם listener בתוך ה-effect, הסירו אותו שם.
השתמשו ב-useCallback עבור כל handler שאתם מצמידים ל-DOM או ל-window. בלעדיו, כל render יוצר הפניה (reference) חדשה לפונקציה. אם תקראו ל-addEventListener עם הפניה אחת ומאוחר יותר תקראו ל-removeEventListener עם הפניה אחרת, ההסרה תיכשל בשקט. ה-listener המקורי יישאר ב-window לנצח. useCallback שומר על ההפניה יציבה כך שה-add וה-remove יתאימו בדיוק.
התייחסו ל-observers באותה משמעת. שמרו את מופע ה-observer בתוך ref או במשתנה מקומי בתוך ה-effect. בפונקציית הניקוי, קראו ל-observer.disconnect(). אל תניחו ש-unmounting של הקומפוננטה "הורג" את ה-observer. זה לא קורה.
אם הקומפוננטה שלכם מפרסמת משהו ל-namespace גלובלי או לשירות singleton, מחקו את ההפניות הללו בעת unmount. מנוע V8 יכול לפנות זיכרון רק כאשר אובייקט הוא באמת בלתי נגיש (unreachable). השארת hook ב-window או רשומה ב-Map ברמת המודול יוצרת גשר בלתי נראה שגורם ל-heap להמשיך לגדול.
השורה התחתונה
דליפות זיכרון לא מפילות את האפליקציה שלכם באופן מיידי. הן מצטברות, node מנותק אחד בכל פעם, במהלך סשנים ארוכים של משתמשים. הפתרון אינו שדרוג ספרייה או דגל קומפיילר (compiler flag). זהו ההרגל של הוכחת לוגיקת הניקוי שלכם באמצעות heap snapshots.
קחו baseline, הפעילו את התהליך החשוד, כפו garbage collection והשוו. אם ה-delta מראה צמיחה, בדקו את ה-retaining path, מצאו את ה-listener או ה-observer שלא אמורים להיות קיימים, ונתקו את ההפניה. הריצו את הבדיקה שוב. כשה-delta נשאר שטוח, סימן שפתרתם את הבעיה באמת. האפליקציה שלכם תישאר רספונסיבית, והמשתמשים שלכם לא יאבדו את עבודתם בגלל לשונית דפדפן קפואה.
