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
+93-71Lines changed: 93 additions & 71 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,63 +1,65 @@
1
-
# Garbage collection
1
+
# جمع القمامة (Garbage Collection)
2
+
تتم عملية ادارة الذاكرة في الJavaScript بطريقة تلقائية غير مرئية
3
+
نحن ننشئ لبقيم البسيطة, الكائنات , الدوال... و كل ذلك يستهلك من الذاكرة.
2
4
3
-
Memory management in JavaScript is performed automatically and invisibly to us. We create primitives, objects, functions... All that takes memory.
5
+
ما الذي سيحدث اذا لم نعد بحاجة لأحدهم ؟ كيف يكتشفها محرك ال JavaScript و يتخلص منها ؟
4
6
5
-
What happens when something is not needed any more? How does the JavaScript engine discover it and clean it up?
6
7
7
-
## Reachability
8
+
## قابلية الوصول
8
9
9
-
The main concept of memory management in JavaScript is *reachability*.
10
+
المفهوم الأساسي لعملية ادارة الذاكرة في ال JavaScript هو *قابلية الوصول.*
10
11
11
-
Simply put, "reachable" values are those that are accessible or usable somehow. They are guaranteed to be stored in memory.
12
+
ببساطة, القيم التي يمكن الوصول اليها هي القيم التي يمكن الولوج اليها و استخدامها بشكل ما, و تخزينها في الذاكرة شئ حتمي.
12
13
13
-
1. There's a base set of inherently reachable values, that cannot be deleted for obvious reasons.
14
+
1. هناك بعض القيم الأساسية التي يمكن الوصول اليها دائماو لا يمكن مسحها لأسباب واضحة.
15
+
علي سبيل المثال:
14
16
15
-
For instance:
17
+
- المتغيرات المحلية و المعاملات للدالة التي يتم استخدامها
18
+
- المتغيرات و معاملات الدوال في سلسلة متصلة من الاستدعاءات
19
+
- المتغيرات العامة
20
+
- (هنالك المزيد, بعضهم داخلي)
16
21
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)
22
+
هذه القيم تسمي ب *الجذور (roots)*.
21
23
22
-
These values are called *roots*.
24
+
2. أي قيمة اخري يمكن اعتبارها قابلة للوصول اليها اذا ما كان يمكن الوصول اليها بالفعل من جذر عن طريق مرجع(reference) او مجموعة من المراجع.
23
25
24
-
2. Any other value is considered reachable if it's reachable from a root by a reference or by a chain of references.
26
+
علي سبيل المثال, اذا كان هناك كائن مخزن في متغير محلي, و الكائن به خاصية مرجه لكائن اخر, يمكن اعتبار هذا الكائن انه يمكن الوصول اليه, و يمكن الوصول ايضا الي كل مراجع هذا الكائن, كما يتم شرحه في المثال التالي.
25
27
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.
27
28
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.
29
+
تحدث عملية خلفية في محرك ال JavaScript يسمي ب جامع القمامة[garbage collector](https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)) يتم خلالها مراقبة كل الكائنات و ازالة الكائنات التي لا يمكن الوصول اليها.
29
30
30
-
## A simple example
31
+
## مثـــال بـسـيـط
31
32
32
-
Here's the simplest example:
33
+
هذا هوا ابسط مثـال:
33
34
34
35
```js
35
-
// user has a reference to the object
36
+
// user له مرجع الي الكائن
36
37
let user = {
37
38
name:"John"
38
39
};
39
40
```
40
41
41
42

42
43
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.
44
+
السهم هنا يمثل مرجع لكائن, المتغير العام `"user"` يرجع الي الكائن `{name: "John"}` (الذي سنطلق عليه جون اختصارا).
45
+
خاصية `"name"` في كائن جون تخزن قيمة بسيطة لذا تم رسمها داخل الكائن.
44
46
45
-
If the value of `user`is overwritten, the reference is lost:
47
+
اذا تم استخدام اسم المتغير `user`مرة اخري , هنا يتم فقدان مرجع الكائن:
46
48
47
49
```js
48
50
user =null;
49
51
```
50
52
51
53

52
54
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.
55
+
هنا لا يمكن الوصول الي كائن جون و بالتالي لا يمكن الدخول اليه لعدم وجود مرجع له, و هنا يأتي دور جامع القمامة للتخلص من بياناته و افراغ المساحة التخزينية.
54
56
55
-
## Two references
57
+
## مرجعين
56
58
57
-
Now let's imagine we copied the reference from `user`to`admin`:
59
+
تصور الأن اننا نسخنا مرجع `user`الي `admin`:
58
60
59
61
```js
60
-
// user has a reference to the object
62
+
// user له مرجع الي الكائن
61
63
let user = {
62
64
name:"John"
63
65
};
@@ -74,11 +76,12 @@ Now if we do the same:
74
76
user =null;
75
77
```
76
78
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.
79
+
...في هذه الحالة ما زال بالامكان الوصول الي الكائن عن طريق المتغير العام `admin`, لذا فهو مخزن بالذاكرة. اذا تم استخدام المتغير `admin`في مكان أخر ايضا هنا يمكن ازالة الكائن .
78
80
79
-
## Interlinked objects
81
+
## الكائنات المترابطة
80
82
81
83
Now a more complex example. The family:
84
+
و الان الي مثال أكثر تعقيدا, كل العائلة:
82
85
83
86
```js
84
87
functionmarry(man, woman) {
@@ -98,15 +101,14 @@ let family = marry({
98
101
});
99
102
```
100
103
101
-
Function`marry` "marries" two objects by giving them references to each other and returns a new object that contains them both.
104
+
الدالة`marry` "تزوج" كائنين عن طريق اعطاء كل منهم مرجع للأخر و ترجع كائن جديد يحتوي كليهما.
It's not enough to delete only one of these two references, because all objects would still be reachable.
120
+
الغاء مرجع واحد فقط من المرجعين لا يكفي ,ستظل امكانية الوصول الي الكائنين ممكنة.
119
121
120
-
But if we delete both, then we can see that John has no incoming reference any more:
122
+
و لكن اذا تم الغاء المرجعين, هنا يمكن ان نري ان جون لم يعد له اي مرجع:
121
123
122
124

123
125
124
126
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.
125
127
126
-
After garbage collection:
128
+
بعد عملية جمع القمامة:
129
+
127
130
128
131

129
132
130
-
## Unreachable island
133
+
## الجزيرة التي لا يمكن الوصول اليها
131
134
132
-
It is possible that the whole island of interlinked objects becomes unreachable and is removed from the memory.
135
+
هنالك امكانية ان تصبح جزيرة بأكملها من الكائنات المترابطة لا يمكن الوصول اليها و ان تمحي من الذاكرة.
136
+
137
+
الكائن هو كما بالمثال السابق, و بالتالي:
133
138
134
-
The source object is the same as above. Then:
135
139
136
140
```js
137
141
family =null;
138
142
```
139
143
140
-
The in-memory picture becomes:
144
+
الصورة من داخل الذاكرة تصبح كالتالي:
145
+
141
146
142
147

143
148
144
-
This example demonstrates how important the concept of reachability is.
149
+
هذا المثال يشرح اهمية مفهوم قابلية الوصول
145
150
146
-
It's obvious that John and Ann are still linked, both have incoming references. But that's not enough.
151
+
من الواضح ان جون و آن ما زالا متصلين ببعضهما بمراجع, لكن هذا غير كافي
147
152
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.
153
+
كائن `"family"`السابق فقد اتصاله بالجذر, لا يوجد له اي مرجع الآن, لذا فالجزيرة باكملها لا يمكن الوصول اليها و تتم ازالتها.
149
154
150
-
## Internal algorithms
155
+
156
+
## الخوارزميات الداخلية
151
157
152
158
The basic garbage collection algorithm is called "mark-and-sweep".
159
+
الخوارزمية الأساسية لجمع القمامة تسمي ب `"mark-and-sweep"`.
160
+
161
+
تحدث عملية جمع القمامة غالبا علي عدة خطوات:
153
162
154
-
The following "garbage collection" steps are regularly performed:
163
+
- جامع القمامة يأخذ الجذر و يضع عليه علامة
164
+
- ثم يزور كل المراجع المرتبطة به و يضع علي كل منهم علامة
165
+
- ثم يزور كل كل الكائنات التي عليها علامة و يضع علامة علي كل مراجعهم, كل الكائنات التي تمت زيارتها يتم تذكرها حتي لا تتم زيارة نفس الكائن مرتين.
166
+
- ... و هكذا الي ان يتم وضع علامة علي كل المراجع بداية من الجذر
167
+
- كل الكائنات عدي التي تم وضع علامة عليها يتم ازالتها
155
168
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 every reachable (from the roots) references are visited.
160
-
- All objects except marked ones are removed.
169
+
كمثال, فلنفترض ان بناء الكائن لدينا كالتالي:
161
170
162
-
For instance, let our object structure look like this:
163
171
164
172

165
173
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.
174
+
يمكننا بكل بساطة ملاحظة جزيرة لا يمكن الوصول اليها في الجانب الأيمن. و الآن نستطيع ان نري كيف يطبق جامع القمامة ال `"mark-and-sweep"`.
175
+
176
+
أول خطوة هي وضع علامة عل الجذر:
167
177
168
-
The first step marks the roots:
169
178
170
179

171
180
172
-
Then their references are marked:
181
+
ثم وضع علامة علي كل المراجع المرتبطة به:
173
182
174
183

175
184
176
-
...And their references, while possible:
185
+
...و مراجعم كذلك ان امكن
177
186
178
187

179
188
180
-
Now the objects that could not be visited in the process are considered unreachable and will be removed:
189
+
و الآن, كل الكائنات التي لم تتم زيارتها في هذه العملية تعتبر كائنات لا يمكن الوصول اليها و ستتم ازالتها:
181
190
182
191

183
192
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
193
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.
194
+
يمكننا تخيل العملية كصب دلو كبير من الطلاء من الجذر و الذي سيسري خلال كل المراجع و يضع علامة علي كل الكائنات التي يمكن الوصول اليها, و الكائنات التي لا يمكن الوصول اليها يتم ازالتها.
195
+
196
+
تلك هي المبادئ التي يعمل علي اساسها جامع القمامة. محرك ال JavaScript يطبق العديد من التحسينات لجعله يعمل بشكل اسرع و الا يؤثر علي الآداء.
187
197
188
-
Some of the optimizations:
198
+
بعض هذه التحسينات:
199
+
200
+
-**مجموعة الأجيال** -- -- يتم تقسيم الكائنات الي مجموعتين "الجديدة", "القديمة", الكثير من الكائنات يتم انشائها و تؤدي وظيفتها و تموت بسرعة, فيمكن التخلص منها بسرعة. اما الكائنات التي تعيش لفترة طوية تصبح "قديمة" و لا يتم التحقق منها و ازالتها بنفس الكثافة.
201
+
202
+
-**المجموعة المتزايدة** --في حالة وجود العديد من الكائنات, و حاولنا المرور ووضع علامة علي الكائن كله مرة واحدة, سيستهلك هذا بعضا من الوقت مما قد يؤدي الي بعض التأخير في عملية التنفيذ, لذلك يحاول جامع القمامة تقسيم نفسه الي اجزاء, كل الأجزاء يتم استخدامها وحدها واحدة تلو الأخري, مما قد يتطلب المزيد من الادارة لمتابعة التغييرات و تسجيلها, و لكن تأخيرات عديدة صغيرة افضل من تأخير واحد كبير.
203
+
204
+
205
+
-**مجموعة وقت الخمول** -- يحاول جامع القمامة ان يعمل في حالة ان وحدة المعالجة المركزية (CPU) في حالة خمول حتي لا يؤثر علي عملية التنفيذ.
189
206
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.
193
207
194
208
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.
195
209
196
-
## Summary
210
+
هنالك العديد من التحسينات في خوارزميات جامع القمامة. و علي قدر ما اود ان اشرحها هنا,يجب ان نتوقف و ذلك لأن المحركات الختلفة تتبني طرق و حلول مختلفة و الأهم من ذلك ان الأشياء تتغير بتتطور المحركات, لذا ادرس أكثر "مقدما" فبدون الحاجة الحقيقية لمعرفتها فهي لا تستحق العناء الا ان كنت و بالطبع تمتلك الشغف للمعرفة فالروابط بالأسفل ستساعدك بالتأكيد.
211
+
212
+
## الملخص
213
+
214
+
اهم النقاط لتعرفها:
215
+
216
+
- جامع القمامة يعمل بشكل تلقائي, لا يمكن اجباره علي العمل او ايقافه
217
+
- الكائنات تظل في الذاكرة طالما كان بالمكان الوصول اليها
218
+
- كون الكائن له مرجع لا يعني بالضرورة ان يمكن الوصول اليه(من الجذر): قد تصبح مجموعة من الكائنات لا يمكن الوصول اليها
219
+
220
+
المحركات الحديثة تطور خوارزميات حديثة لعملية جمع القمامة
197
221
198
-
The main things to know:
222
+
كتاب `"The Garbage Collection Handbook: The Art of Automatic Memory Management"(R.Jones et al)` يجمع بعضها
199
223
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.
224
+
اذا كنت علي علم بالمستويات العميقة من البرمجيات , فهنالك المزيد من المعلومات عن جـامع القمامة `V8` في هذا المقال
203
225
204
-
Modern engines implement advanced algorithms of garbage collection.
205
226
206
-
A general book "The Garbage Collection Handbook: The Art of Automatic Memory Management" (R. Jones et al) covers some of them.
227
+
[A tour of V8: Garbage Collection](http://jayconrod.com/posts/55/a-tour-of-v8-garbage-collection)
207
228
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).
229
+
تنشر ايضا [V8 blog](https://v8.dev/) مقالات حول تنظيم الذاكرة من آن الي أخر, بطبيعة الحال, لتعلم جامع القمامة يفضل ان تتهيأ عن طريق تعلم مكونات `V8` بشكل عام و قراءة مدونة
230
+
[Vyacheslav Egorov](http://mrale.ph) و الذي عمل كأحد مهندسي `V8`. استطيع ان أقول `V8` تحديدا لأنه الأكثر تغطية عن طريق المقالات علي الانترنت.
231
+
و بالنسبة للمحركات الأخري, العديد من الطرق متشابهة, و لكن جامع القمامة يختلف في نقاط عديدة.
209
232
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.
233
+
المعرفة المتعمقة للمحركات ضرورية في حين الحاجة الي تحسينات ذات مستوي متطور, فأن تخطط لمعرفتها بعد ان تصبح علي معرفة جيدة باللغة لهي بالتأكيد خطوة حكيمة.
211
234
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.
0 commit comments