Shivank MITTAL’s Post

𝗪𝗲𝗹𝗰𝗼𝗺𝗲 𝘁𝗼 𝗗𝗮𝘆 𝟭𝟯 𝙃𝙤𝙬 𝙅𝙖𝙫𝙖𝙎𝙘𝙧𝙞𝙥𝙩 𝙂𝙖𝙧𝙗𝙖𝙜𝙚 𝘾𝙤𝙡𝙡𝙚𝙘𝙩𝙞𝙤𝙣 𝙒𝙤𝙧𝙠𝙨 Your code doesn’t free memory. JavaScript does. But it only removes what is unreachable. 𝙍𝙚𝙖𝙘𝙝𝙖𝙗𝙞𝙡𝙞𝙩𝙮 An object stays in memory if there is a reference path from the roots. Roots: • Global scope • Execution stack • Active closures No path → eligible for garbage collection. 𝙈𝙖𝙧𝙠 & 𝙎𝙬𝙚𝙚𝙥 • Start from the roots and mark all reachable objects • Remove everything unmarked Reachable stays. Unreachable is freed. 𝙂𝙚𝙣𝙚𝙧𝙖𝙩𝙞𝙤𝙣𝙖𝙡 𝘾𝙤𝙡𝙡𝙚𝙘𝙩𝙞𝙤𝙣 Most objects die young. Memory is divided into: • Young generation → short-lived objects, frequent fast cleanup • Old generation → long-lived objects, less frequent cleanup 𝙋𝙧𝙤𝙢𝙤𝙩𝙞𝙤𝙣 Every object starts in the young generation. If it survives multiple GC cycles or memory pressure increases, it gets promoted to the old generation. Survival determines placement. 𝙒𝙝𝙮 𝙈𝙚𝙢𝙤𝙧𝙮 𝙇𝙚𝙖𝙠𝙨 𝙎𝙩𝙞𝙡𝙡 𝙃𝙖𝙥𝙥𝙚𝙣 GC removes only unreachable objects. Leaks occur when references are retained: • Globals • Closures • Timers / event listeners • Growing caches 𝘎𝘊 𝘸𝘰𝘳𝘬𝘴 𝘤𝘰𝘳𝘳𝘦𝘤𝘵𝘭𝘺. 𝘠𝘰𝘶𝘳 𝘳𝘦𝘧𝘦𝘳𝘦𝘯𝘤𝘦𝘴 𝘥𝘦𝘤𝘪𝘥𝘦 𝘸𝘩𝘢𝘵 𝘭𝘪𝘷𝘦𝘴. #JavaScript #WebDevelopment #SoftwareEngineering #FrontendDevelopment #Programming #Coding #Developers #TechCommunity #ComputerScience #PerformanceOptimization #Learning #ContinuousLearning #LearnInPublic

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories