[JavaScript] メモリ管理を理解するための参考情報(日本語・英語)

JavaScript のメモリ管理を理解するための参考情報を日本語と英語、それぞれ探したのでご紹介します。

JavaScript

前提

メモリ管理の参考情報(日本語)

導入

Cのような低レベル言語は、malloc()free()のような底レベルメモリ管理プリミティブを持ちます。他方では、JavaScriptの値は、もの(オブジェクト、文字列など)が生成されるときに割り当てられます。そして、”自動的に”使用されないとき開放されます。後者のプロセスがガーベジコレクションを呼び出します。この”自動的”は混乱の源であり、JavaScript (そして、高レベル言語) 開発者に彼らは、メモリ管理を気にしないことを決定することができる印象を与えます。これは誤りです。

メモリライフサイクル

プログラミング言語に関係なく、メモリのライフサイクルはほぼいつも同じです:

  1. あなたが必要とするメモリを割り当てます
  2. 使用します (読み, 書き)
  3. それはもはや必要ないときに割り当てられたメモリを解放します

1と2に関してはすべての言語で明示的に行われます。最後の3は、低レベルの言語では明示的ですが、JavaScriptのような高水準言語では、ほとんど暗黙的に行われます。

メモリ管理の参考情報(英語)

Introduction

Low-level languages, like C, have low-level memory management primitives like malloc() and free(). On the other hand, JavaScript values are allocated when things (objects, strings, etc.) are created and “automatically” freed when they are not used anymore. The latter process is called garbage collection. This “automatically” is a source of confusion and gives JavaScript (and other high-level language) developers the impression they can decide not to care about memory management. This is a mistake.

Memory life cycle

Regardless of the programming language, memory life cycle is pretty much always the same:

  1. Allocate the memory you need
  2. Use the allocated memory (read, write)
  3. Release the allocated memory when it is not needed anymore

The second part is explicit in all languages. The first and last parts are explicit in low-level languages, but are mostly implicit in high-level languages like JavaScript.

英語ネイティブな人にエンジニア研修をするときは、MDN は英語と日本語の両方のドキュメントがあるのでとても助かります。