Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are ES6 global symbols garbage collected?

Is uncapped, dynamic creation of ES6 Symbols something that can result in irrecoverable memory usage?

In Erlang (and also previously, Ruby), creation of atoms/symbols are not garbage collected.

It seems like Symbols created in the global symbol registry (Symbol.for('string')) could not be garbage collected and remain globally unique but I might be missing something. This did not seem to me to be dictated by the ES6 spec.

like image 518
Eoin Avatar asked Oct 12 '16 11:10

Eoin


People also ask

Are Symbols garbage collected?

Basically, all symbols created dynamically while Ruby is running (via to_sym , etc.) can be garbage collected because they are not being used behind the scenes inside the Ruby interpreter.

Is JavaScript garbage collected?

Some high-level languages, such as JavaScript, utilize a form of automatic memory management known as garbage collection (GC). The purpose of a garbage collector is to monitor memory allocation and determine when a block of allocated memory is no longer needed and reclaim it.

Could you make sure a const value is garbage collected?

With a const variable declaration, you can't assign to the variable something little like "" or null to clear its contents. That's really the only difference in regard to memory management. Automatic garbage collection is not affected at all by whether it is declared const or not.

Does node js have garbage collection?

Luckily for you, Node. js comes with a garbage collector, and you don't need to manually manage memory allocation.


1 Answers

Symbols being primitives does not mean they cannot be implemented using references and allocation. Just think about primitive strings in js. Browsers might implement symbols this way, making them subject to gc. A quick test for(;;) Symbol(); (don't run it) in chrome causes a sawtooth memory profile so I assume symbols get allocated and garbage collected.

like image 155
Tamas Hegedus Avatar answered Oct 17 '22 06:10

Tamas Hegedus