Skip to content

Commit fec94d3

Browse files
authored
Merge pull request #41 from armino-dev/master
Garbage Collection
2 parents b168cfe + dcaaa8c commit fec94d3

File tree

1 file changed

+75
-75
lines changed
  • 1-js/04-object-basics/02-garbage-collection

1 file changed

+75
-75
lines changed
+75-75
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,63 @@
1-
# Garbage collection
1+
# Colectarea reziduurilor
22

3-
Memory management in JavaScript is performed automatically and invisibly to us. We create primitives, objects, functions... All that takes memory.
3+
Gestionarea memoriei în JavaScript este realizată automat și invizibil pentru noi. Creăm primitive, obiecte, funcții... Toate acestea ocupă spațiu în memorie.
44

5-
What happens when something is not needed any more? How does the JavaScript engine discover it and clean it up?
5+
Ce se întâmplă când nu mai este nevoie de ceva? Cum îl descoperă și îl curăță motorul limbajului Javascript?
66

7-
## Reachability
7+
## Accesibilitatea
88

9-
The main concept of memory management in JavaScript is *reachability*.
9+
Conceptul de bază al gestionării memoriei în JavaScript este *accesibilitatea*.
1010

11-
Simply put, "reachable" values are those that are accessible or usable somehow. They are guaranteed to be stored in memory.
11+
Simplu spus, valorile "accesibile" sunt acelea care sunt abordabile sau folosibile în vreun fel. Ele sunt garantate a fi salvate în memorie.
1212

13-
1. There's a base set of inherently reachable values, that cannot be deleted for obvious reasons.
13+
1. Există un set de bază de valori intrinseci accesibile, care nu pot fi șterse, din motive evidente.
1414

15-
For instance:
15+
De exemplu:
1616

17-
- Local variables and parameters of the current function.
18-
- Variables and parameters for other functions on the current chain of nested calls.
19-
- Global variables.
20-
- (there are some other, internal ones as well)
17+
- Variabile locale și parametri ai funcției curente.
18+
- Variabile și parametri ai altor funcții din lanțul curent de apeluri imbricate.
19+
- Variabile globale.
20+
- (mai sunt și altele, cele interne, de asemenea)
2121

22-
These values are called *roots*.
22+
Aceste valori se numesc *rădăcini*.
2323

24-
2. Any other value is considered reachable if it's reachable from a root by a reference or by a chain of references.
24+
2. Orice altă valoare este considerată accesibilă dacă este accesibilă dintr-o rădăcină printr-o referință sau printr-un lanț de referințe.
2525

26-
For instance, if there's an object in a local variable, and that object has a property referencing another object, that object is considered reachable. And those that it references are also reachable. Detailed examples to follow.
26+
De exemplu, dacă există un obiect într-o variabilă locală și acel obiect are o proprietate care face referire la alt obiect, acel obiect este considerat accesibil. Iar cele la care face referire sunt, de asemenea, accesibile. Urmează exemple detaliate.
2727

28-
There's a background process in the JavaScript engine that is called [garbage collector](https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)). It monitors all objects and removes those that have become unreachable.
28+
În motorul limbajului Javascript există un proces de fundal care se numește [garbage collector](https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)). El monitorizează toate obiectele și le șterge pe cele care au devenit inaccesibile.
2929

30-
## A simple example
30+
## Un exemplu simplu
3131

32-
Here's the simplest example:
32+
Iată cel mai simplu exemplu:
3333

3434
```js
35-
// user has a reference to the object
35+
// user are o referință către obiect
3636
let user = {
3737
name: "John"
3838
};
3939
```
4040

4141
![](memory-user-john.svg)
4242

43-
Here the arrow depicts an object reference. The global variable `"user"` references the object `{name: "John"}` (we'll call it John for brevity). The `"name"` property of John stores a primitive, so it's painted inside the object.
43+
Aici săgeata desemnează o referință a obiectului. Variabila globală `"user"` face referire la obiectul `{name: "John"}` (îl vom numi John pentru simplitate). Proprietatea `"name"` a lui John stochează o primitivă, astfel încât este desenat în interiorul obiectului.
4444

45-
If the value of `user` is overwritten, the reference is lost:
45+
Dacă valoarea `user`-ului este rescrisă, referința este pierdută:
4646

4747
```js
4848
user = null;
4949
```
5050

5151
![](memory-user-john-lost.svg)
5252

53-
Now John becomes unreachable. There's no way to access it, no references to it. Garbage collector will junk the data and free the memory.
53+
Acum John devine inaccesibil. Nu există nicio modalitate pentru a-l accesa, nicio referință la el. Garbage collector va elimina datele și va elibera memoria.
5454

55-
## Two references
55+
## Două referințe
5656

57-
Now let's imagine we copied the reference from `user` to `admin`:
57+
Acum să ne imaginăm că am copiat referința de la `user` la `admin`:
5858

5959
```js
60-
// user has a reference to the object
60+
// user are o referință către obiect
6161
let user = {
6262
name: "John"
6363
};
@@ -69,16 +69,16 @@ let admin = user;
6969

7070
![](memory-user-john-admin.svg)
7171

72-
Now if we do the same:
72+
Acum dacă procedăm la fel:
7373
```js
7474
user = null;
7575
```
7676

77-
...Then the object is still reachable via `admin` global variable, so it's in memory. If we overwrite `admin` too, then it can be removed.
77+
...Atunci obiectul este încă accesibil prin intermediul variabilei globale `admin`, deci este în memorie. Dacă rescriem și `admin`, atunci poate fi indepărtat.
7878

79-
## Interlinked objects
79+
## Obiecte interconectate
8080

81-
Now a more complex example. The family:
81+
Acum un exemplu mai complex. Familia:
8282

8383
```js
8484
function marry(man, woman) {
@@ -98,15 +98,15 @@ let family = marry({
9898
});
9999
```
100100

101-
Function `marry` "marries" two objects by giving them references to each other and returns a new object that contains them both.
101+
Funcția `marry` "căsătorește" două obiecte oferindu-le reciproc referințe și returnează un obiect nou care le conține pe amândouă.
102102

103-
The resulting memory structure:
103+
Structura memoriei rezultate:
104104

105105
![](family.svg)
106106

107-
As of now, all objects are reachable.
107+
Începând de acum, toate obiectele sunt accesibile.
108108

109-
Now let's remove two references:
109+
Acum să eliminăm două referințe:
110110

111111
```js
112112
delete family.father;
@@ -115,98 +115,98 @@ delete family.mother.husband;
115115

116116
![](family-delete-refs.svg)
117117

118-
It's not enough to delete only one of these two references, because all objects would still be reachable.
118+
Nu este suficient să ștergem doar una dintre aceste două referințe, pentru că toate obiectele ar fi încă accesibile.
119119

120-
But if we delete both, then we can see that John has no incoming reference any more:
120+
Însă dacă le ștergem pe amândouă, putem observa că John nu mai are nicio referință de intrare:
121121

122122
![](family-no-father.svg)
123123

124-
Outgoing references do not matter. Only incoming ones can make an object reachable. So, John is now unreachable and will be removed from the memory with all its data that also became unaccessible.
124+
Referințele de ieșire nu contează. Doar cele de intrare pot face un obiect accesibil. Așadar, John este acum inaccesibil și va fi eliminat din memorie cu toate datele asociate, care vor deveni, de asemenea, inaccesibile.
125125

126-
After garbage collection:
126+
După colectarea reziduurilor:
127127

128128
![](family-no-father-2.svg)
129129

130-
## Unreachable island
130+
## Insula inaccesibilă
131131

132-
It is possible that the whole island of interlinked objects becomes unreachable and is removed from the memory.
132+
Este posibil ca toată insula cu obiectele interconectate să devină inaccesibilă și să fie eliminată din memorie.
133133

134-
The source object is the same as above. Then:
134+
Obiectul sursă este la fel ca mai sus. Apoi:
135135

136136
```js
137137
family = null;
138138
```
139139

140-
The in-memory picture becomes:
140+
Imaginea din memorie devine:
141141

142142
![](family-no-family.svg)
143143

144-
This example demonstrates how important the concept of reachability is.
144+
Acest exemplu demonstrează cât de important este conceptul de accesibilitate.
145145

146-
It's obvious that John and Ann are still linked, both have incoming references. But that's not enough.
146+
Este evident că John și Ann sunt încă legate, amăndouă au referințe de intrare. Dar nu este suficient.
147147

148-
The former `"family"` object has been unlinked from the root, there's no reference to it any more, so the whole island becomes unreachable and will be removed.
148+
Fostul obiect `"family"` a fost deconectat de la rădăcină, nu mai există nicio referință la el, deci întreaga insulă devine iaccesibilă și va fi eliminată.
149149

150-
## Internal algorithms
150+
## Algoritmi interni
151151

152-
The basic garbage collection algorithm is called "mark-and-sweep".
152+
Algoritmul de bază de colectare a reziduurilor este denumit "mark-and-sweep" (în traducere literală "marchează-și-mătură").
153153

154-
The following "garbage collection" steps are regularly performed:
154+
Următorii pași ai "colectării reziduurilor" sunt efectuați regulat:
155155

156-
- The garbage collector takes roots and "marks" (remembers) them.
157-
- Then it visits and "marks" all references from them.
158-
- Then it visits marked objects and marks *their* references. All visited objects are remembered, so as not to visit the same object twice in the future.
159-
- ...And so on until there are unvisited references (reachable from the roots).
160-
- All objects except marked ones are removed.
156+
- "Garbage collector" ia rădăcinile și le "marchează" (le memorează).
157+
- Apoi vizitează toate referințele dinspre ele.
158+
- Apoi parcurge obiectele marcate și însemnează referințele *lor*. Toate obiectele vizitate sunt memorate pentru a nu vizita același obiect de două ori în viitor.
159+
- ...Și așa mai departe până când nu există referințe nevizitate (accesibil din rădăcini).
160+
- Toate obiectele, cu excepția celor marcate, sunt eliminate.
161161

162-
For instance, let our object structure look like this:
162+
De exemplu, să presupunem că structura obiectului nostru arată așa:
163163

164164
![](garbage-collection-1.svg)
165165

166-
We can clearly see an "unreachable island" to the right side. Now let's see how "mark-and-sweep" garbage collector deals with it.
166+
Putem vedea foarte clar o "insulă inaccesibilă" în partea dreaptă. Acum să vedem cum se ocupă colectorul de reziduuri "mark-and-sweep" de aceasta.
167167

168-
The first step marks the roots:
168+
Primul pas marchează rădăcinile:
169169

170170
![](garbage-collection-2.svg)
171171

172-
Then their references are marked:
172+
Apoi sunt marcate referințele lor:
173173

174174
![](garbage-collection-3.svg)
175175

176-
...And their references, while possible:
176+
...Și referințele acestora, atât cât este posibil:
177177

178178
![](garbage-collection-4.svg)
179179

180-
Now the objects that could not be visited in the process are considered unreachable and will be removed:
180+
Acum, obiectele care nu au putut fi vizitate pe parcursul procesului sunt considerate inaccesibile și vor fi eliminate:
181181

182182
![](garbage-collection-5.svg)
183183

184-
That's the concept of how garbage collection works.
184+
Acesta este conceptul de funcționare a colectării reziduurilor.
185185

186-
JavaScript engines apply many optimizations to make it run faster and not affect the execution.
186+
Motoarele limbajului JavaScript aplică numeroase optimizări pentru a-l face să ruleze mai rapid și să nu afecteze execuția.
187187

188-
Some of the optimizations:
188+
Câteva dintre optimizări:
189189

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.
192-
- **Idle-time collection** -- the garbage collector tries to run only while the CPU is idle, to reduce the possible effect on the execution.
190+
- **Colectare generațională** -- obiectele sunt împărțite în două seturi: "cele noi" și "cele vechi". Multe obiecte apar, își fac treaba și mor repede, pot fi curățate agresiv. Cele care supraviețuiesc suficient de mult devin "vechi" și sunt examinate mai rar.
191+
- **Colectare incrementală** -- dacă există multe obiecte și încercăm să parcurgem simultan întregul set obiect, poate dura mai mult și poate introduce întârzieri vizibile în execuție. Astfel încât, motorul încearcă să împartă în bucăți colectarea reziduurilor. Apoi bucățile sunt executate separat, una câte una. Asta necesită o mai bună contabilitate între ele pentru a urmări modificările, dar avem mai multe întârzieri micuțe în loc de una mare.
192+
- **Colectare în timp-inactiv** -- colectorul de reziduuri încearcă să ruleze numai în timp ce procesorul este inactiv, pentru a reduce posibilul efect asupra execuției.
193193

194-
There are 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 going 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.
194+
Există și alte optimzări și modele ale algoritmilor de colectare a reziduurilor. Oricât de mult mi-ar plăcea să le descriu aici. trebuie să mă abțin, întrucât diferite motoare implementează ajustări și tehnici diferite. Și, ceea ce este și mai important, lucrurile se schimbă pe măsură ce motoarele se dezvoltă, deci aprofundarea "în avans", fără o nevoie reală, probabil că nu merită acest lucru. Cu excepția cazului în care, bineînțeles, este o chestiune de interes pur, atunci vor fi câteva link-uri pentru voi mai jos.
195195

196-
## Summary
196+
## Rezumat
197197

198-
The main things to know:
198+
Lucrurile principale de știut:
199199

200-
- Garbage collection is performed automatically. We cannot force or prevent it.
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.
200+
- Colectarea reziduurilor se face automat. Nu o putem forța sau preveni.
201+
- Obiectele sunt menținute în memorie atât timp cât sunt accesibile.
202+
- A fi referențiat nu este același lucru cu a fi accesibil (dintr-o rădăcină): un pachet de obiecte interconectate poate deveni inaccesibil în ansamblu.
203203

204-
Modern engines implement advanced algorithms of garbage collection.
204+
Motoarele moderne implementează algoritmi avansați de colectare a reziduurilor.
205205

206-
A general book "The Garbage Collection Handbook: The Art of Automatic Memory Management" (R. Jones et al) covers some of them.
206+
O carte generală "The Garbage Collection Handbook: The Art of Automatic Memory Management" (R. Jones et al) acoperă câteva dintre ele.
207207

208-
If you are familiar with low-level programming, the more detailed information about V8 garbage collector is in the article [A tour of V8: Garbage Collection](http://jayconrod.com/posts/55/a-tour-of-v8-garbage-collection).
208+
Dacă sunteți familiari cu programarea low-level, informații detaliate despre V8 garbage collector se află în articolul [A tour of V8: Garbage Collection](http://jayconrod.com/posts/55/a-tour-of-v8-garbage-collection).
209209

210-
[V8 blog](https://v8.dev/) also publishes articles about changes in memory management from time to time. Naturally, to learn the garbage collection, you'd better prepare by learning about V8 internals in general and read the blog of [Vyacheslav Egorov](http://mrale.ph) who worked as one of V8 engineers. I'm saying: "V8", because it is best covered with articles in the internet. For other engines, many approaches are similar, but garbage collection differs in many aspects.
210+
[V8 blog](https://v8.dev/) publică, deasemenea, articole din timp în timp despre schimbările din gestionarea memoriei. Desigur, pentru a învăța colectarea reziduurilor, ar fi bine să vă pregătiți învățând despre structurile interne ale V8-ului în general și să citiți blogul lui [Vyacheslav Egorov](http://mrale.ph) care a lucrat ca unul dintre inginerii V8-ului. Spun: "V8", deoarece este cel mai bine acoperit cu articole din internet. Pentru alte motoare, multe abordări sunt similare, dar colectarea reziduurilor diferă în multe aspecte.
211211

212-
In-depth knowledge of engines is good when you need low-level optimizations. It would be wise to plan that as the next step after you're familiar with the language.
212+
Cunoașterea în profunzime a motoarelor este folositoare atunci când aveți nevoie de optimizări low-level. Ar fi înțelept să planificați acest lucru ca pas următor după ce vă familiarizați cu limbajul.

0 commit comments

Comments
 (0)