| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Memory_Management#mark-and-sweep_algorithm | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
.
. , JavaScript .
, JavaScript .
const n = 123; //
const s = "azerty"; //
const o = {
a: 1,
b: null,
}; //
// ()
const a = [1, null, "abra"];
function f(a) {
return a + 2;
} // ( )
// .
someElement.addEventListener(
"click",
() => {
someElement.style.backgroundColor = "blue";
},
false,
);
.
const d = new Date(); // Date
const e = document.createElement("div"); // DOM
.
const s = "azerty";
const s2 = s.substr(0, 3); // s2
// JavaScript immutable ,
// [0, 3] .
const a = ["ouais ouais", "nan nan"];
const a2 = ["generation", "nan nan"];
const a3 = a.concat(a2);
// a a2 , 4
. .
. " " .
.
" " . . .
. , A ( ) B "B A " . , JavaScript prototype (implicit reference) ( ) ( ) .
"" JavaScript ( ) .
: - .
- . ' ' ' ' . "" , , .
:
let x = {
a: {
b: 2,
},
};
// 2 . .
// 'x' .
// , .
let y = x;
// 'y' .
x = 1;
// 'y' .
let z = y.a;
// 'a' .
// .
// 'y' 'z' .
y = "mozilla";
// 'x' , .
// , 'a' 'z'
// .
z = null;
// 'x' 'a'
// .
. , . , . , - . .
function f() {
const x = {};
const y = {};
x.a = y; // x y .
y.a = x; // y x .
return "azerty";
}
f();
" " " " .
"roots" . JavaScript root . , roots roots , roots . roots , .
- , . , ( , ) .
- . JavaScript (/// ) , " " .
. , . , .
, . , . , . JavaScript , API , .
JavaScript . , Node.js V8 . , (HTTP ) .
:
node --max-old-space-size=6000 index.js
node --expose-gc --inspect index.js
JavaScript API , JavaScript .
WeakMap WeakSet non-weak Map Set API . WeakMap - , WeakSet . , , .
WeakMap WeakSet "weakly held" . x y "weakly held" , y x , - x _strongly hold* . , strongly hold . WeakMap WeakSet (WeakMap ). :
WeakMap WeakSet . (1 === 1 {} !== {}), . Registered symbols (like Symbol.for("key")) , , Symbol("key") . Symbol.iterator Well-known symbols Array.prototype .WeakMap WeakSet . Array.from(map.keys()).length ( ). WeakMap WeakSet , . , .
const wm = new WeakMap();
const key = {};
wm.set(key, { key });
// , `key`
// map strongly hold .
key , key . key , value.key . , WeakMap WeakSet - ephemerons. Barros et al. (4 ). .
Ephemerons weak strong (weak pairs) . , . [] ephemerons , 2 3 .
, WeakMap :
: , .
class MyWeakMap {
#marker = Symbol("MyWeakMapData");
get(key) {
return key[this.#marker];
}
set(key, value) {
key[this.#marker] = value;
}
has(key) {
return this.#marker in key;
}
delete(key) {
delete key[this.#marker];
}
}
, MyWeakMap . MyWeakMap . - . , WeakMap WeakMap (clear) (clear ).
API keyed collections .
. , "strong" . . WeakRef "weak reference" .
WeakRef URL . WeakMap , WeakMap weakly held "keys" "values" . , ( ). , undefined ( ) , . , Map WeakRef .
function cached(getter) {
// URL WeakRefs Map
const cache = new Map();
return async (key) => {
if (cache.has(key)) {
return cache.get(key).deref();
}
const value = await getter(key);
cache.set(key, new WeakRef(value));
return value;
};
}
const getImage = cached((url) => fetch(url).then((res) => res.blob()));
FinalizationRegistry . FinalizationRegistry . , , WeakRef . Map . FinalizationRegistry .
function cached(getter) {
// URL WeakRefs Map
const cache = new Map();
// ,
//
const registry = new FinalizationRegistry((key) => {
// Note: WeakRef .
// ,
// .
if (!cache.get(key)?.deref()) {
cache.delete(key);
}
});
return async (key) => {
if (cache.has(key)) {
return cache.get(key).deref();
}
const value = await getter(key);
cache.set(key, new WeakRef(value));
registry.register(value, key);
return value;
};
}
const getImage = cached((url) => fetch(url).then((res) => res.blob()));
, , . FinalizationRegistry . finally try...finally . WeakRef FinalizationRegistry .
WeakRef FinalizationRegistry API .
This page was last modified on 2026 1 8 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |