एक बग रिपोर्ट आई जिसने डिबगिंग के हर सहज बोध (instinct) को चुनौती दे दी। लो-एंड Android फोन वाले यूजर्स ने बताया कि ऐप बस गायब हो जाता है। लॉन्च के समय नहीं। किसी खास टैप या स्वाइप के दौरान भी नहीं। सेशन शुरू होने के लगभग बीस मिनट बाद, स्क्रीन फ्रीज हो जाती है और प्रोसेस खत्म हो जाता है। लॉग्स बिल्कुल साफ थे। QA इसे अपने हाई-एंड हार्डवेयर पर रीप्रोड्यूस नहीं कर सका। इसे फॉलो करने के लिए कोई स्टेप्स नहीं थे। तीन घंटे की मेमोरी प्रोफाइलिंग के बाद, आखिरकार तस्वीर साफ हुई। एक सिंगल इवेंट लिसनर एक React hook के अंदर बैठा था। उस लिसनर ने एक बड़े डेटासेट को क्लोजर (closure) में ले लिया था। कंपोनेंट अनमाउंट (unmount) हो गया। लिसनर वहीं रहा। डेटासेट मेमोरी में बना रहा। 2GB RAM वाले डिवाइस पर, इस संचय (accumulation) ने हीप (heap) को खत्म कर दिया और ऑपरेटिंग सिस्टम ने ऐप को बंद कर दिया। यह कोई सिंटैक्स एरर या लॉजिक की कमी नहीं थी। यह एक स्कोप बग (scope bug) था, और यह घातक था।

क्लोजर (Closure) लीक कैसे बन जाता है

अधिकांश ट्यूटोरियल स्कोप को एक अकादमिक पहेली के रूप में सिखाते हैं कि कोई वेरिएबल कहाँ दिखाई देता है। प्रोडक्शन में, स्कोप मेमोरी लाइफटाइम (memory lifetime) का एक अनुबंध है। जब एक JavaScript फंक्शन किसी वेरिएबल पर क्लोजर बनाता है, तो इंजन उस वेरिएबल को तब तक जीवित रखता है जब तक कि वह क्लोजर खुद सुलभ (reachable) है। एक React कंपोनेंट में, इसका मतलब है कि आपका डेटा यूजर के जाने और UI नोड के खत्म होने के बहुत बाद तक जीवित रहता है।

एक ऐसे hook पर विचार करें जो window ऑब्जेक्ट पर एक लिसनर रजिस्टर करता है। कंपोनेंट रेंडर होता है, लिसनर अटैच करता है, और बाद में अनमाउंट हो जाता है। यदि क्लीनअप फेज (cleanup phase) गायब है या गलत तरीके से किया गया है, तो लिसनर बना रहता है। हर नया माउंट RAM में फंसे हुए डेटा की एक और 'घोस्ट कॉपी' जोड़ देता है। प्रचुर मेमोरी वाले डेवलपर वर्कस्टेशन पर, आप शायद इस सूजन (bloat) पर कभी ध्यान न दें। Android Go चलाने वाले बजट फोन पर, सामान्य उपयोग के बीस मिनट ही उपलब्ध हीप (heap) को खत्म करने के लिए काफी हैं। OS हस्तक्षेप करता है और प्रोसेस को समाप्त कर देता है। लॉग करने के लिए कोई एक्सेप्शन (exception) नहीं होता। सिस्टम बस प्लग खींच लेता है।

यही कारण है कि स्कोप ही मेमोरी मैनेजमेंट है। लेक्सिकल एनवायरनमेंट (lexical environment) कोई दार्शनिक सीमा नहीं है। यह एक रिटेंशन ग्राफ (retention graph) है। हर वेरिएबल जिसे आप एक अनकलेक्टेड क्लोजर (uncollected closure) के अंदर छोड़ देते हैं, वह एक ऐसी दीवार की ईंट है जो अंततः आपके ऐप को कैद कर लेती है।

तीन तरीके जिनसे स्कोप प्रोडक्शन ऐप्स को खत्म कर देता है

स्कोप की समस्याएं सभी एक जैसी नहीं दिखतीं। कुछ मेमोरी को धीरे-धीरे सोखती हैं। अन्य तुरंत विस्फोट कर देती हैं। यहाँ वे पैटर्न दिए गए हैं जो भरोसेमंद रूप से ऐप्स को डाउन कर देते हैं।

ग्लोबल स्कोप पॉल्यूशन (Global Scope Pollution)

माइक्रो-फ्रंटएंड आर्किटेक्चर टीमों को स्वतंत्र रूप से शिप करने की अनुमति देते हैं, लेकिन वे सभी एक ही window ऑब्जेक्ट साझा करते हैं। जब एक एप्लिकेशन window.config जैसा ग्लोबल वेरिएबल सेट करता है या window पर एक साझा यूटिलिटी को पैच करता है, तो वह आइसोलेशन में नहीं रहता है। दूसरी टीम का ऐप उसी ग्लोबल के लिए एक अलग स्ट्रक्चर पर निर्भर हो सकता है, या अपने स्वयं के बूटस्ट्रैप के दौरान उसे ओवरराइट कर सकता है। इसका परिणाम एक फीचर टकराव (feature collision) होता है जो आपके संगठन के साथ बढ़ता जाता है। एक रिपॉजिटरी के डेवलपर को पता ही नहीं होता कि उनका शॉर्टकट दूसरी टीम के लिए एक ब्रेकिंग चेंज है। जैसे-जैसे सतह का क्षेत्र बढ़ता है, ये ग्लोबल्स साझा जमीन में दबे लैंडमाइन्स बन जाते हैं।

क्लोजर मेमोरी लीक्स (Closure Memory Leaks)

सिंगल पेज एप्लिकेशन घंटों तक चलने के लिए बनाए जाते हैं। यही टिकाऊपन लीक होने वाले क्लोजर्स को विषाक्त (toxic) बना देता है। यह पैटर्न भ्रामक रूप से आम है: एक useEffect ग्लोबल इवेंट बस, एक WebSocket हैंडलर, या स्वयं DOM के साथ एक कॉलबैक रजिस्टर करता है। यदि डिपेंडेंसी ऐरे (dependency array) अस्थिर है या छोड़ दिया गया है, तो क्लीनअप कभी भी मूल सब्सक्रिप्शन से मेल नहीं खाता। क्लोजर अपने लेक्सिकल स्कोप में जो कुछ भी है उसे कैप्चर कर लेता है, जिसमें विशाल पार्स किए गए ऐरे (parsed arrays), फेच किए गए JSON ब्लब्स (blobs), या DOM ट्री के रेफरेंस शामिल हो सकते हैं। हर नेविगेशन और अधिक वजन जोड़ता है। यूजर को यह नहीं पता होता कि उनका ब्राउज़र टैब 800MB क्यों ले रहा है। वे केवल इतना जानते हैं कि ऐप सुस्त महसूस हो रहा है और अंततः बंद हो जाता है।

यह विशेष रूप से तब खतरनाक होता है जब डिपेंडेंसी ऐरे हर रेंडर पर बदल जाते हैं। प्रत्येक चक्र में एक नया फंक्शन रेफरेंस जन्म लेता है, एक लिसनर के साथ रजिस्टर होता है, और पुराने को कभी रिलीज़ नहीं किया जाता है। इसका परिणाम मृत क्लोजर्स का एक म्यूजियम होता है, जिनमें से प्रत्येक उस डेटा को जमा करके रखता है जिसके साथ वे पैदा हुए थे।

डायनेमिक मॉड्यूल्स में TDZ एरर्स (TDZ Errors in Dynamic Modules)

The Temporal Dead Zone is not a theoretical edge case. When you access a let or const before its declaration executes, the engine throws a ReferenceError. In large monorepos with circular dependencies and dynamic imports, the exact execution order is often implicit. Module A imports Module B, which dynamically imports a chunk that depends back on Module A. If one branch touches a variable that has not finished initializing, the app crashes during load. These failures are maddening because they are timing-dependent. A small change in the bundler split points, a network delay in code-loading, or a shift in chunk caching can alter the order just enough to trigger the TDZ. The crash is unpredictable, and the stack trace usually points to a perfectly innocent line of code.

Defensive Tactics

You cannot rely on stack traces to save you from scope bugs. You need prevention and detection.

Start with static analysis. Configure ESLint to enforce strict boundaries. Rules like no-implicit-globals and no-shadow catch the obvious sins. Shadowing is particularly treacherous because it tricks you into thinking you are mutating a local variable when you are actually building a closure over an outer one, or creating an accidental duplicate. These rules force explicit intent and eliminate silent collisions.

Profile your memory with the same discipline you apply to unit tests. Open Chrome DevTools, take a heap snapshot on your starting route, navigate through your application for five minutes, and take another. Compare the two. Filter for "Closure" and look for counts that grow without bound. Look for detached DOM nodes that still retain event listeners. If the second snapshot shows thousands of new Closure entries while your user count stayed flat, you have trapped functions holding trapped data. That is your leak.

Architecturally, stop reaching into the global window object for configuration. Pass settings as props or through a typed context. Dependency injection is not an enterprise buzzword here; it is the practice of giving a function everything it needs through arguments rather than letting it sniff the global scope. The result is code you can test without browser shims, and modules that do not collide when multiple apps mount inside the same shell.

Finally, respect the cleanup phase ruthlessly. Every addEventListener needs a matching removeEventListener inside the effect cleanup. For asynchronous work, use an AbortController and pass its signal to fetch so in-flight requests cancel when the component dies. These habits directly control how long a scope lives. They are not boilerplate. They are memory management.

What This Means for Your Team

Scope is not a parlor trick to quiz candidates with during interviews. In production, scope is memory management. Every variable you declare is a potential hostage. Every closure is a promise the engine will keep. When you forget to release a listener, you are not leaving a light on. You are chaining a weight to your app and dropping it in the ocean. On powerful hardware, the app swims anyway. For users on low-end devices, it sinks. Start treating scope like the finite resource it is. Your users, and your three-hour debugging sessions, will thank you.