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 Type | Description |
|---|---|
| RSS (Resident Set Size) | Node.js ํ๋ก์ธ์ค์ ํ ๋น๋ ๋ชจ๋ ๋ฉ๋ชจ๋ฆฌ(heap, stack, code ๋ฑ๋ฑ) |
| Heap Total | Javascript ๊ฐ์ฒด๋ฅผ ์ํด ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ. heap ์ ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ์ด์ฆ์ด๋ค |
| Heap Used | Javascript ๊ฐ์ฒด์ ์ํด ์ค์ ๋ก ์ฌ์ฉ๋ ๋ฉ๋ชจ๋ฆฌ. ํ์ฌ ์ผ๋ง๋ heap์ด ์ฌ์ฉ์ค์ธ์ง ๋ํ๋ |
| External | Javascript ๊ฐ์ฒด์ ์ฐ๊ฒฐ๋ C++ ๊ฐ์ฒด์์ํด ์ฌ์ฉ์ค์ธ ๋ฉ๋ชจ๋ฆฌ. V8 heap ์์ญ ์ธ๋ถ์์ ๊ด๋ฆฌ๋๋ ๋ฉ๋ชจ๋ฆฌ. Javascript๊ฐ native code์ ์ํธ์์ฉํ๋ ์ฐ๊ฒฐ์ ์ํด ์ฌ์ฉ๋๋ค. Javascript heap ์์ญ ๋ฐ๊นฅ์ ํ ๋น๋จ |
| Array Buffers | ArrayBuffer ๊ฐ์ฒด๋ฅผ ์ํด ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ์ด๋ฉฐ, 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 collection6. 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์ํ ์ฐธ์กฐ ์๋ฐฉ๋ฒ
- Break the Loop : ์ํ์ฐธ์กฐ๊ฐ ๋ฐ์ํ๋ ๋ถ๋ถ์ ์ง์ ๋์ด์ผํ๋ค.
obj.reference = null; - 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๊ฐ ๊ฐ์ฅ ์๊ฐ์ ๋ง์ด ์ฌ์ฉํ ํผ์ผํธ๊ฐ ์ผ๋ง๋ ๋๋์ง ํ์ธํ๋ผ. ์ด๋ ์ดํ๋ฆฌ์ผ์ด์ ์ด ์ผ๋ง๋ ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ์ ์ ๋ฅผ ๋จน๊ณ ์๋์ง ์๋ ค์ค๋ค.