TIL coding
  • Initial page
  • 排版
  • Flexbox
  • Grid
  • jQuery
  • Untitled
  • JavaScript
    • An Introduction to JavaScript
    • Hello, world!
    • Code structure
    • The modern mode, "use strict"
    • Variables
    • Data types
    • Type Conversions
    • Operators
    • Comparisons
    • Interaction: alert, prompt, confirm
    • Conditional operators: if, '?'
    • Logical operators
    • Loops: while and for
    • The "switch" statement
    • Functions
    • Function expressions and arrows
    • JavaScript specials
    • Comments
    • Ninja code
    • Automated testing with mocha
    • Polyfills
    • Objects
    • Garbage collection
    • Symbol type
    • Object methods, "this"
    • Object to primitive conversion
    • Constructor, operator "new"
    • Methods of primitives
    • Numbers
    • Strings
    • Arrays
    • Array methods
    • Iterables
    • Map, Set, WeakMap and WeakSet
    • Object.keys, values, entries
    • Destructuring assignment
    • Date and time
    • JSON methods, toJSON
    • Recursion and stack
    • Rest parameters and spread operator
    • Closure
    • The old "var"
    • Global object
    • Function object, NFE
    • The "new Function" syntax
    • Scheduling: setTimeout and setInterval
    • Decorators and forwarding, call/apply
    • Function binding
    • Currying and partials
    • Arrow functions revisited
    • Property flags and descriptors
    • Property getters and setters
    • Prototypal inheritance
    • F.prototype
    • Native prototypes
    • Prototype methods, objects without __proto__
    • The “class” syntax
    • Class inheritance
    • Static properties and methods
    • Private and protected properties and methods
    • Extending built-in classes
    • Class checking: "instanceof"
    • Mixins
    • Error handling, "try..catch"
    • Custom errors, extending Error
    • Introduction: callbacks
    • Promise
    • Promises chaining
    • Error handling with promises
    • Promise API
  • Bootstrap
    • Navbar
Powered by GitBook
On this page
  • Reachability
  • A simple example
  • Two references
  • Interlinked objects
  • Unreachable island
  • Internal algorithms

Was this helpful?

  1. JavaScript

Garbage collection

Reachability

主要管理記憶體的核心是可取得性,JavaScript 引擎裡有垃圾搜集器,監控並移除未使用的物件。

  • 目前函式的參數或變數

  • 內嵌函式所用到的變數及參數

  • 全域變數

A simple example

// user has a reference to the object
let user = {
  name: "John"
};

// 物件被移除,得不到物件的值 John
user = null;

Two references

// user has a reference to the object
let user = {
  name: "John"
};

let admin = user;

// 只修改 user 的指向,admin 仍存在物件,因此物件依然存在
user = null;

Interlinked objects

function marry(man, woman) {
  woman.husband = man;
  man.wife = woman;

  return {
    father: man,
    mother: woman
  }
}

let family = marry({
  name: "John"
}, {
  name: "Ann"
});
delete family.father;
delete family.mother.husband;

Unreachable island

family = null;

Internal algorithms

基本的垃圾蒐集演算法為 mark-and-sweep

  • 垃圾搜集器找尋所有根並標註。

  • 標註所有根指向的記憶,繼續往下一層標註記憶體直到沒有新的物件。

  • 未被標註的記憶體進行刪除。

PreviousObjectsNextSymbol type

Last updated 5 years ago

Was this helpful?