[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Memory_Management#mark-and-sweep_algorithm [Back]  [Original]

JavaScript - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

JavaScript

C , malloc() free() . , JavaScript ( ). , .

In this article

.

  1. .
  2. . (, )
  3. .

. , JavaScript .

JavaScript

, JavaScript .

js
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,
);

.

js
const d = new Date(); // Date    

const e = document.createElement("div"); // DOM    

.

js
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    

. .

. " " .

.

JavaScript " (GC)" . . . .

" " . . .

. , A ( ) B "B A " . , JavaScript prototype (implicit reference) ( ) ( ) .

"" JavaScript ( ) .

-(Reference-counting)

: - .

- . ' ' ' ' . "" , , .

:

js
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' 
//       .

. , . , . , - . .

js
function f() {
  const x = {};
  const y = {};
  x.a = y; // x y .
  y.a = x; // y x .

  return "azerty";
}

f();

-(Mark-and-sweep)

" " " " .

"roots" . JavaScript root . , roots roots , roots . roots , .

- , . , ( , ) .

- . JavaScript (/// ) , " " .

. , . , .

, . , . , . JavaScript , API , .

(memory model)

JavaScript . , Node.js V8 . , (HTTP ) .

:

bash
node --max-old-space-size=6000 index.js

Chrome Debugger :

bash
node --expose-gc --inspect index.js

JavaScript API , JavaScript .

WeakMaps WeakSets

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 , . , .

js
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 :

: , .

js
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 .

WeakRefs FinalizationRegistry

Note: WeakRef FinalizationRegistry . .

. , "strong" . . WeakRef "weak reference" .

WeakRef URL . WeakMap , WeakMap weakly held "keys" "values" . , ( ). , undefined ( ) , . , Map WeakRef .

js
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 .

js
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 .


Web Proxy Viewer  |  New URL  |  Original Page