Memory Leak
Memory Leak
A bug in which unused memory is not released. The system slows down over time.
Memory Leak
A bug in which unused memory is not released. The system slows down over time.
A memory leak (Memory Leak) is a bug that occurs when a program fails to release dynamically allocated memory even though it is no longer used. As a result, the memory available while the program runs keeps decreasing, and in severe cases it can cause system-wide performance degradation or crashes.
The main causes of memory leaks include unreleased references. Typical examples are cases where references to an object remain so the garbage collector cannot reclaim it, cases of event listeners not being released where a DOM element has been removed but the event handler remains, and cases of variable references inside closures that needlessly reference large objects from an outer scope.
Memory management differs by programming language. Languages such as JavaScript, Java, and Python manage memory automatically through garbage collection (GC), but if references remain, even GC cannot reclaim the memory. Languages such as C and C++, on the other hand, require manual memory management, so you must explicitly call malloc/free or new/delete, and incorrect management causes memory leaks or dangling pointer problems.
There are various tools for detecting memory leaks. Valgrind is a powerful tool for detecting memory errors in C/C++ programs, and the Memory tab in Chrome DevTools lets you find leaks by analyzing JavaScript heap snapshots. In Node.js, you can use Chrome DevTools with the --inspect flag or use tools such as clinic.js.
Here are common memory leak patterns in JavaScript and how to fix them:
// ๋ฌธ์ 1: ์ด๋ฒคํธ ๋ฆฌ์ค๋ ๋ฏธํด์ class LeakyComponent { constructor() { this.data = new Array(10000).fill('large data'); // ์ด๋ฒคํธ ๋ฆฌ์ค๋ ๋ฑ๋ก - ํด์ ํ์ง ์์ผ๋ฉด ๋์ ๋ฐ์! window.addEventListener('resize', this.handleResize); } handleResize = () => { console.log('Resized!', this.data.length); } } // ๋ฌธ์ 2: ํด๋ก์ ์์์ ์ฐธ์กฐ ์ ์ง function createClosure() { const hugeArray = new Array(1000000).fill('data'); // ํด๋ก์ ๊ฐ hugeArray๋ฅผ ์ฐธ์กฐํ์ฌ GC ๋ถ๊ฐ return function() { return hugeArray[0]; }; } // ๋ฌธ์ 3: setInterval ๋ฏธ์ ๋ฆฌ function startPolling() { const cache = []; setInterval(() => { // ๊ณ์ ๋ฐ์ดํฐ๊ฐ ์์ด๋ฉฐ ๋ฉ๋ชจ๋ฆฌ ์ฆ๊ฐ cache.push(new Array(1000).fill(Math.random())); }, 1000); }
// ํด๊ฒฐ 1: ์ปดํฌ๋ํธ ์ ๋ฆฌ(cleanup) ํจํด class SafeComponent { constructor() { this.data = new Array(10000).fill('large data'); this.handleResize = this.handleResize.bind(this); window.addEventListener('resize', this.handleResize); } handleResize() { console.log('Resized!', this.data.length); } // ๋ฐ๋์ cleanup ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ๋ฆฌ์ค๋ ํด์ destroy() { window.removeEventListener('resize', this.handleResize); this.data = null; // ๋ช ์์ ์ฐธ์กฐ ํด์ } } // ํด๊ฒฐ 2: WeakRef ๋๋ ํ์ํ ๋ฐ์ดํฐ๋ง ์ฐธ์กฐ function createSafeClosure() { const hugeArray = new Array(1000000).fill('data'); const firstItem = hugeArray[0]; // ํ์ํ ๊ฐ๋ง ์ ์ฅ return function() { return firstItem; // hugeArray ์ฐธ์กฐ ์์ }; } // ํด๊ฒฐ 3: interval ID ์ ์ฅ ๋ฐ ์ ๋ฆฌ function startSafePolling() { const cache = []; const MAX_CACHE_SIZE = 100; const intervalId = setInterval(() => { if (cache.length >= MAX_CACHE_SIZE) { cache.shift(); // ์ค๋๋ ๋ฐ์ดํฐ ์ ๊ฑฐ } cache.push(new Array(1000).fill(Math.random())); }, 1000); // cleanup ํจ์ ๋ฐํ return () => clearInterval(intervalId); }
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free