You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/04-object-basics/03-garbage-collection/article.md
+6-6
Original file line number
Diff line number
Diff line change
@@ -169,11 +169,11 @@ The first step marks the roots:
169
169
170
170

171
171
172
-
Then their references are marked:
172
+
Then we follow their references are mark referenced objects:
173
173
174
174

175
175
176
-
...And their references, while possible:
176
+
...And continue to follow further references, while possible:
177
177
178
178

179
179
@@ -183,12 +183,12 @@ Now the objects that could not be visited in the process are considered unreacha
183
183
184
184
We can also imagine the process as spilling a huge bucket of paint from the roots, that flows through all references and marks all reachable objects. The unmarked ones are then removed.
185
185
186
-
That's the concept of how garbage collection works. JavaScript engines apply many optimizations to make it run faster and not affect the execution.
186
+
That's the concept of how garbage collection works. JavaScript engines apply many optimizations to make it run faster and not introduce any delays into the code execution.
187
187
188
188
Some of the optimizations:
189
189
190
-
-**Generational collection** -- objects are split into two sets: "new ones" and "old ones". Many objects appear, do their job and die fast, they can be cleaned up aggressively. Those that survive for long enough, become "old" and are examined less often.
191
-
-**Incremental collection** -- if there are many objects, and we try to walk and mark the whole object set at once, it may take some time and introduce visible delays in the execution. So the engine tries to split the garbage collection into pieces. Then the pieces are executed one by one, separately. That requires some extra bookkeeping between them to track changes, but we have many tiny delays instead of a big one.
190
+
-**Generational collection** -- objects are split into two sets: "new ones" and "old ones". In typical code, many objects have a short life span: they appear, do their job and die fast, so it makes sense to track new objects and clear the memory from them if that's the case. Those that survive for long enough, become "old" and are examined less often.
191
+
-**Incremental collection** -- if there are many objects, and we try to walk and mark the whole object set at once, it may take some time and introduce visible delays in the execution. So the engine splits the whole set of existing objects into multiple parts. And then clear these parts one after another. There are many small garbage collections instead of a total one. That requires some extra bookkeeping between them to track changes, but we get many tiny delays instead of a big one.
192
192
-**Idle-time collection** -- the garbage collector tries to run only while the CPU is idle, to reduce the possible effect on the execution.
193
193
194
194
There exist other optimizations and flavours of garbage collection algorithms. As much as I'd like to describe them here, I have to hold off, because different engines implement different tweaks and techniques. And, what's even more important, things change as engines develop, so studying deeper "in advance", without a real need is probably not worth that. Unless, of course, it is a matter of pure interest, then there will be some links for you below.
@@ -199,7 +199,7 @@ The main things to know:
199
199
200
200
- Garbage collection is performed automatically. We cannot force or prevent it.
201
201
- Objects are retained in memory while they are reachable.
202
-
- Being referenced is not the same as being reachable (from a root): a pack of interlinked objects can become unreachable as a whole.
202
+
- Being referenced is not the same as being reachable (from a root): a pack of interlinked objects can become unreachable as a whole, as we've seen in the example above.
203
203
204
204
Modern engines implement advanced algorithms of garbage collection.
0 commit comments