https://www.trevorlasn.com/blog/common-causes-of-memory-leaks-in-javascript

Understanding Memory Usage In Node.js (V8)

Node.js(V8)์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ํƒ€์ž…๋“ค (process.memoryUsage() ์„ ํ†ตํ•ด ํ™•์ธ ๊ฐ€๋Šฅ)

Memory TypeDescription
RSS (Resident Set Size)Node.js ํ”„๋กœ์„ธ์Šค์— ํ• ๋‹น๋œ ๋ชจ๋“  ๋ฉ”๋ชจ๋ฆฌ(heap, stack, code ๋“ฑ๋“ฑ)
Heap TotalJavascript ๊ฐ์ฒด๋ฅผ ์œ„ํ•ด ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ. heap ์— ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ์˜ ์‚ฌ์ด์ฆˆ์ด๋‹ค
Heap UsedJavascript ๊ฐ์ฒด์— ์˜ํ•ด ์‹ค์ œ๋กœ ์‚ฌ์šฉ๋œ ๋ฉ”๋ชจ๋ฆฌ. ํ˜„์žฌ ์–ผ๋งˆ๋‚˜ heap์ด ์‚ฌ์šฉ์ค‘์ธ์ง€ ๋‚˜ํƒ€๋ƒ„
ExternalJavascript ๊ฐ์ฒด์™€ ์—ฐ๊ฒฐ๋œ C++ ๊ฐ์ฒด์—์˜ํ•ด ์‚ฌ์šฉ์ค‘์ธ ๋ฉ”๋ชจ๋ฆฌ. V8 heap ์˜์—ญ ์™ธ๋ถ€์—์„œ ๊ด€๋ฆฌ๋˜๋Š” ๋ฉ”๋ชจ๋ฆฌ. Javascript๊ฐ€ native code์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ์—ฐ๊ฒฐ์— ์˜ํ•ด ์‚ฌ์šฉ๋œ๋‹ค. Javascript heap ์˜์—ญ ๋ฐ”๊นฅ์— ํ• ๋‹น๋จ
Array BuffersArrayBuffer ๊ฐ์ฒด๋ฅผ ์œ„ํ•ด ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ์ด๋ฉฐ, raw binary data๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค

Common Causes of Memory Leaks in Javascript

1. Improperly Managed Variables

์—๋ฅผ๋“ค์–ด ์ž„์‹œ๋กœ ์„ ์–ธํ•œ ๋ณ€์ˆ˜๋ฅผ ์žŠ์–ด๋ฒ„๋ฆฌ๊ณ  ์ง€์šฐ์ง€ ์•Š์œผ๋ฉด ๊ณ„์† ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋‹ค

let cache = {};
 
function storeData(key, value) {
    cache[key] = value;
}
 
// Simulating the function being called multiple times
storeData('item1', new Array(1000000).fill('A'));
storeData('item2', new Array(1000000).fill('B'));
 
// Memory leak: data stored in 'cache' is never released
  • cache๊ฐ€ ๊ธ€๋กœ๋ฒŒ ๊ฐ์ฒด์— ์ถ”๊ฐ€๋˜์–ด์žˆ๊ธฐ์— ๋”์ด์ƒ ํ•„์š”์—†์–ด์ ธ๋„ ๋ฉ”๋ชจ๋ฆฌ์— ๊ณ„์† ๋‚จ์•„์žˆ๋Š”๋‹ค
  • global scope ์— ์ €์žฅ๋˜๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ผ์ดํ”„์‚ฌ์ดํด ๋‚ด๋‚ด ์‚ด์•„์žˆ๊ธฐ์— ๋”์šฑ ๋ฌธ์ œ๊ฐ€ ๋œ๋‹ค.

2. Persistent Global Objects

์ „์—ญ ๊ฐ์ฒด๋Š” ๋”์ด์ƒ ํ•„์š”๊ฐ€ ์—†์–ด์ ธ๋„ ๊ณ„์† ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ „์—ญ ๊ฐ์ฒด์— ์„ ์–ธํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š”

global.config = {
    settings: new Array(1000000).fill('Configuration')
};

์•„๋ž˜์ฒ˜๋Ÿผ ์ง€์—ญ์ ์œผ๋กœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค

function createConfig() {
    return {
        settings: new Array(1000000).fill('Configuration')
    };
}
 
// Use config only when needed, and let it be garbage collected afterwards
function processConfig() {
    const config = createConfig();
    // Perform operations with config
    console.log(config.settings[0]);
 
    // Config will be cleared from memory once it's no longer referenced
}
 
processConfig();

3. Event Listeners Not Removed

์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ๋Š” ๋„˜๊ฒจ์ง„ ํ•จ์ˆ˜์™€ ๊ฑฐ๊ธฐ์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ณ€์ˆ˜์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ด๋Š” GC๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ •๋ฆฌํ•˜๋Š”๊ฑธ ๋ง‰๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ๋งŒ์•ฝ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์ง€์šฐ์ง€ ์•Š๊ณ  ์œ ์ง€ํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค

4. Closures Capturing Variable

clousre ์— ์˜๋„์น˜์•Š๊ฒŒ ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”๊ฐ€ ์—†์Œ์—๋„ ๋‚จ์•„ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

function createClosure() {
    let capturedVar = new Array(1000000).fill('Data');
 
    return function() {
        console.log(capturedVar[0]);
    };
}
 
const closure = createClosure();

๋ˆ„์ˆ˜๋ฅผ ์˜ˆ๋ฐฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” closure๊ฐ€ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ํฐ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๋„๋กํ•˜๊ณ , ํ•„์š”๊ฐ€ ์—†์–ด์ง€๋ฉด ์‚ญ์ œํ•ด์•ผํ•œ๋‹ค

function createClosure() {
    let capturedVar = new Array(1000000).fill('Data');
 
    return function() {
        console.log(capturedVar[0]);
        capturedVar = null; // Release memory when no longer needed
    };
}
 
const closure = createClosure();
closure(); // 'capturedVar' is released after use.

5. Unmanaged Callbacks

์ฝœ๋ฐฑ์ด ๋ถˆํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

function fetchData(callback) {
    let data = new Array(1000000).fill('Data');
 
    setTimeout(() => {
        callback(data);
    }, 1000);
}
 
function handleData(data) {
    console.log(data[0]);
}
 
fetchData(handleData); // The 'data' array remains in memory.

JS GC๋Š” ๋”์ด์ƒ ํ•„์š”ํ•˜์ง€ ์•Š๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ์ •๋ฆฌํ•˜๊ธฐ์—, ํŠน์ˆ˜ํ•œ ์‹œ๋‚˜๋ฆฌ์˜ค๊ฐ€ ์•„๋‹ˆ๋ฉด ์ˆ˜๋™์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ •๋ฆฌํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

Avoiding Unnecessary Complexity

์ผ๋ฐ˜์ ์œผ๋กœ ๋น„๋™๊ธฐ ์ฝœ๋ฐฑ์— ์ˆ˜๋™ ๋ฉ”๋ชจ๋ฆฌ ์ •๋ฆฌ๋Š” ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๊ฐ€ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ •๋ฆฌํ•œ ์˜ˆ์‹œ๋‹ค

function fetchData(callback) {
  let data = new Array(1000000).fill('Data');
 
  setTimeout(() => {
      callback(data);
      data = null; // Release the reference
      global.gc(); // Explicitly trigger garbage collection
  }, 1000);
}
 
function handleData(data) {
  console.log(data[0]);
  data = null; // Clear reference after handling
}
 
console.log('Initial Memory Usage:', process.memoryUsage());
 
fetchData(handleData);
 
setTimeout(() => {
  console.log('Final Memory Usage:', process.memoryUsage());
}, 2000); // Give some time for garbage collection

6. Incorrect Use of bind()

bind()๋Š” this๋ฅผ ์„ธํŒ…ํ•˜๋ฉด์„œ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ ์กฐ์‹ฌ์Šค๋Ÿฝ๊ฒŒ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋กœ ์ด์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค.

function MyClass() {
    this.largeData = new Array(1000000).fill('leak');
 
    window.addEventListener('click', this.handleClick.bind(this));
}
 
MyClass.prototype.handleClick = function() {
    console.log('Clicked');
};
  • bind()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋Š” ๊ธฐ์กด ํ•จ์ˆ˜์™€ ๊ธฐ์กดํ•จ์ˆ˜์˜ this๋ฅผ ๊ธฐ์–ตํ•œ๋‹ค. ๋งŒ์•ฝ ํ•จ์ˆ˜๋ฅผ ์ง€์šฐ๋”๋ผ๋„ ๋ฉ”๋ชจ๋ฆฌ์— ๊ณ„์† ํ•จ์•„์žˆ๋Š”๊ฒƒ์ด๋‹ค.

7. Circular References

์ˆœํ™˜ ์ฐธ์กฐ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ๊ทธ ๋ถ€๋ชจ์˜ ์—ฐ๊ฒฐ์„ ๋Š์–ด๋„ ๋ฉ”๋ชจ๋ฆฌ์— ๊ณ„์† ๋‚จ์•„์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

function CircularReference() {
    this.reference = this; // Circular reference
}
 
let obj = new CircularReference();
obj = null; // Setting obj to null may not free the memory

์ˆœํ™˜ ์ฐธ์กฐ ์˜ˆ๋ฐฉ๋ฒ•

  1. Break the Loop : ์ˆœํ™˜์ฐธ์กฐ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๋ถ€๋ถ„์„ ์ง์ ‘ ๋Š์–ด์•ผํ•œ๋‹ค.
obj.reference = null; 
  1. Use Weak References : WeakMap, WeakSet, WeakRef๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ references๊ฐ€ ์žˆ์–ด๋„ GC๊ฐ€ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •๊ฐ€๋Šฅํ•˜๋‹ค
let weakMap = new WeakMap();
 
function CircularReference() {
    let obj = {};
    weakMap.set(obj, "This is a weak reference");
    return obj;
}
 
let obj = CircularReference();

Quick Note

WeakMap ๋“ฑ์€ ๋ˆ„์ˆ˜๋ฅผ ๋ง‰๊ธฐ์œ„ํ•œ ์ข‹์€ ๋„๊ตฌ์ง€๋งŒ, ๋งค๋ฒˆ ์‚ฌ์šฉํ•  ํ•„์š”๋Š” ์—†๋‹ค. ๊ทธ๋ณด๋‹ค ๋” ์ข‹์€ ์˜ˆ์‹œ๊ฐ€ ์žˆ๋‹ค

  • Profiling Memory Usage : ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋ฅผ ์ฐพ๊ธฐ์œ„ํ•ด ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์–ผ๋งˆ๋‚˜ ์‚ฌ์šฉ๋˜๋Š”์ง€ ํ…Œ์ŠคํŒ…๊ณผ์ •์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ๊ฐ์‹œํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ํ•จ์ˆ˜์— ์ŠคํŠธ๋ ˆ์Šค ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
  • Pay particular attention to
    • High CPU usage functions
    • Memory-intensive functions
    • Event Loop and Garbage Collection : GC๊ฐ€ ๊ฐ€์žฅ ์‹œ๊ฐ„์„ ๋งŽ์ด ์‚ฌ์šฉํ•œ ํผ์„ผํŠธ๊ฐ€ ์–ผ๋งˆ๋‚˜ ๋˜๋Š”์ง€ ํ™•์ธํ•˜๋ผ. ์ด๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์–ผ๋งˆ๋‚˜ ๋ฉ”๋ชจ๋ฆฌ ์ •๋ฆฌ์— ์• ๋ฅผ ๋จน๊ณ  ์žˆ๋Š”์ง€ ์•Œ๋ ค์ค€๋‹ค.