Skip to content

Commit 2ff23ae

Browse files
authored
Merge pull request #73 from Ahmed-Adel3/object-basics
Garbage collection
2 parents 083ebd4 + 3bb2ae0 commit 2ff23ae

File tree

1 file changed

+93
-71
lines changed
  • 1-js/04-object-basics/03-garbage-collection

1 file changed

+93
-71
lines changed

1-js/04-object-basics/03-garbage-collection/article.md

Lines changed: 93 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,65 @@
1-
# Garbage collection
1+
# جمع القمامة (Garbage Collection)
2+
تتم عملية ادارة الذاكرة في الJavaScript بطريقة تلقائية غير مرئية
3+
نحن ننشئ لبقيم البسيطة, الكائنات , الدوال... و كل ذلك يستهلك من الذاكرة.
24

3-
Memory management in JavaScript is performed automatically and invisibly to us. We create primitives, objects, functions... All that takes memory.
5+
ما الذي سيحدث اذا لم نعد بحاجة لأحدهم ؟ كيف يكتشفها محرك ال JavaScript و يتخلص منها ؟
46

5-
What happens when something is not needed any more? How does the JavaScript engine discover it and clean it up?
67

7-
## Reachability
8+
## قابلية الوصول
89

9-
The main concept of memory management in JavaScript is *reachability*.
10+
المفهوم الأساسي لعملية ادارة الذاكرة في ال JavaScript هو *قابلية الوصول.*
1011

11-
Simply put, "reachable" values are those that are accessible or usable somehow. They are guaranteed to be stored in memory.
12+
ببساطة, القيم التي يمكن الوصول اليها هي القيم التي يمكن الولوج اليها و استخدامها بشكل ما, و تخزينها في الذاكرة شئ حتمي.
1213

13-
1. There's a base set of inherently reachable values, that cannot be deleted for obvious reasons.
14+
1. هناك بعض القيم الأساسية التي يمكن الوصول اليها دائماو لا يمكن مسحها لأسباب واضحة.
15+
علي سبيل المثال:
1416

15-
For instance:
17+
- المتغيرات المحلية و المعاملات للدالة التي يتم استخدامها
18+
- المتغيرات و معاملات الدوال في سلسلة متصلة من الاستدعاءات
19+
- المتغيرات العامة
20+
- (هنالك المزيد, بعضهم داخلي)
1621

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)*.
2123

22-
These values are called *roots*.
24+
2. أي قيمة اخري يمكن اعتبارها قابلة للوصول اليها اذا ما كان يمكن الوصول اليها بالفعل من جذر عن طريق مرجع(reference) او مجموعة من المراجع.
2325

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+
علي سبيل المثال, اذا كان هناك كائن مخزن في متغير محلي, و الكائن به خاصية مرجه لكائن اخر, يمكن اعتبار هذا الكائن انه يمكن الوصول اليه, و يمكن الوصول ايضا الي كل مراجع هذا الكائن, كما يتم شرحه في المثال التالي.
2527

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.
2728

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)) يتم خلالها مراقبة كل الكائنات و ازالة الكائنات التي لا يمكن الوصول اليها.
2930

30-
## A simple example
31+
## مثـــال بـسـيـط
3132

32-
Here's the simplest example:
33+
هذا هوا ابسط مثـال:
3334

3435
```js
35-
// user has a reference to the object
36+
// user له مرجع الي الكائن
3637
let user = {
3738
name: "John"
3839
};
3940
```
4041

4142
![](memory-user-john.svg)
4243

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"` في كائن جون تخزن قيمة بسيطة لذا تم رسمها داخل الكائن.
4446

45-
If the value of `user` is overwritten, the reference is lost:
47+
اذا تم استخدام اسم المتغير `user` مرة اخري , هنا يتم فقدان مرجع الكائن:
4648

4749
```js
4850
user = null;
4951
```
5052

5153
![](memory-user-john-lost.svg)
5254

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+
هنا لا يمكن الوصول الي كائن جون و بالتالي لا يمكن الدخول اليه لعدم وجود مرجع له, و هنا يأتي دور جامع القمامة للتخلص من بياناته و افراغ المساحة التخزينية.
5456

55-
## Two references
57+
## مرجعين
5658

57-
Now let's imagine we copied the reference from `user` to `admin`:
59+
تصور الأن اننا نسخنا مرجع `user` الي `admin`:
5860

5961
```js
60-
// user has a reference to the object
62+
// user له مرجع الي الكائن
6163
let user = {
6264
name: "John"
6365
};
@@ -74,11 +76,12 @@ Now if we do the same:
7476
user = null;
7577
```
7678

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` في مكان أخر ايضا هنا يمكن ازالة الكائن .
7880

79-
## Interlinked objects
81+
## الكائنات المترابطة
8082

8183
Now a more complex example. The family:
84+
و الان الي مثال أكثر تعقيدا, كل العائلة:
8285

8386
```js
8487
function marry(man, woman) {
@@ -98,15 +101,14 @@ let family = marry({
98101
});
99102
```
100103

101-
Function `marry` "marries" two objects by giving them references to each other and returns a new object that contains them both.
104+
الدالة `marry` "تزوج" كائنين عن طريق اعطاء كل منهم مرجع للأخر و ترجع كائن جديد يحتوي كليهما.
102105

103-
The resulting memory structure:
106+
البناء الناتج في الذاكرة:
104107

105108
![](family.svg)
106109

107-
As of now, all objects are reachable.
108-
109-
Now let's remove two references:
110+
حتي الأن, كل الكائنات يمكن الوصول اليها.
111+
و الأن فلنزيل اثنين من المراجع:
110112

111113
```js
112114
delete family.father;
@@ -115,98 +117,118 @@ delete family.mother.husband;
115117

116118
![](family-delete-refs.svg)
117119

118-
It's not enough to delete only one of these two references, because all objects would still be reachable.
120+
الغاء مرجع واحد فقط من المرجعين لا يكفي ,ستظل امكانية الوصول الي الكائنين ممكنة.
119121

120-
But if we delete both, then we can see that John has no incoming reference any more:
122+
و لكن اذا تم الغاء المرجعين, هنا يمكن ان نري ان جون لم يعد له اي مرجع:
121123

122124
![](family-no-father.svg)
123125

124126
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.
125127

126-
After garbage collection:
128+
بعد عملية جمع القمامة:
129+
127130

128131
![](family-no-father-2.svg)
129132

130-
## Unreachable island
133+
## الجزيرة التي لا يمكن الوصول اليها
131134

132-
It is possible that the whole island of interlinked objects becomes unreachable and is removed from the memory.
135+
هنالك امكانية ان تصبح جزيرة بأكملها من الكائنات المترابطة لا يمكن الوصول اليها و ان تمحي من الذاكرة.
136+
137+
الكائن هو كما بالمثال السابق, و بالتالي:
133138

134-
The source object is the same as above. Then:
135139

136140
```js
137141
family = null;
138142
```
139143

140-
The in-memory picture becomes:
144+
الصورة من داخل الذاكرة تصبح كالتالي:
145+
141146

142147
![](family-no-family.svg)
143148

144-
This example demonstrates how important the concept of reachability is.
149+
هذا المثال يشرح اهمية مفهوم قابلية الوصول
145150

146-
It's obvious that John and Ann are still linked, both have incoming references. But that's not enough.
151+
من الواضح ان جون و آن ما زالا متصلين ببعضهما بمراجع, لكن هذا غير كافي
147152

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"` السابق فقد اتصاله بالجذر, لا يوجد له اي مرجع الآن, لذا فالجزيرة باكملها لا يمكن الوصول اليها و تتم ازالتها.
149154

150-
## Internal algorithms
155+
156+
## الخوارزميات الداخلية
151157

152158
The basic garbage collection algorithm is called "mark-and-sweep".
159+
الخوارزمية الأساسية لجمع القمامة تسمي ب `"mark-and-sweep"`.
160+
161+
تحدث عملية جمع القمامة غالبا علي عدة خطوات:
153162

154-
The following "garbage collection" steps are regularly performed:
163+
- جامع القمامة يأخذ الجذر و يضع عليه علامة
164+
- ثم يزور كل المراجع المرتبطة به و يضع علي كل منهم علامة
165+
- ثم يزور كل كل الكائنات التي عليها علامة و يضع علامة علي كل مراجعهم, كل الكائنات التي تمت زيارتها يتم تذكرها حتي لا تتم زيارة نفس الكائن مرتين.
166+
- ... و هكذا الي ان يتم وضع علامة علي كل المراجع بداية من الجذر
167+
- كل الكائنات عدي التي تم وضع علامة عليها يتم ازالتها
155168

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+
كمثال, فلنفترض ان بناء الكائن لدينا كالتالي:
161170

162-
For instance, let our object structure look like this:
163171

164172
![](garbage-collection-1.svg)
165173

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+
أول خطوة هي وضع علامة عل الجذر:
167177

168-
The first step marks the roots:
169178

170179
![](garbage-collection-2.svg)
171180

172-
Then their references are marked:
181+
ثم وضع علامة علي كل المراجع المرتبطة به:
173182

174183
![](garbage-collection-3.svg)
175184

176-
...And their references, while possible:
185+
...و مراجعم كذلك ان امكن
177186

178187
![](garbage-collection-4.svg)
179188

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

182191
![](garbage-collection-5.svg)
183192

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.
185193

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 يطبق العديد من التحسينات لجعله يعمل بشكل اسرع و الا يؤثر علي الآداء.
187197

188-
Some of the optimizations:
198+
بعض هذه التحسينات:
199+
200+
- **مجموعة الأجيال** -- -- يتم تقسيم الكائنات الي مجموعتين "الجديدة", "القديمة", الكثير من الكائنات يتم انشائها و تؤدي وظيفتها و تموت بسرعة, فيمكن التخلص منها بسرعة. اما الكائنات التي تعيش لفترة طوية تصبح "قديمة" و لا يتم التحقق منها و ازالتها بنفس الكثافة.
201+
202+
- **المجموعة المتزايدة** --في حالة وجود العديد من الكائنات, و حاولنا المرور ووضع علامة علي الكائن كله مرة واحدة, سيستهلك هذا بعضا من الوقت مما قد يؤدي الي بعض التأخير في عملية التنفيذ, لذلك يحاول جامع القمامة تقسيم نفسه الي اجزاء, كل الأجزاء يتم استخدامها وحدها واحدة تلو الأخري, مما قد يتطلب المزيد من الادارة لمتابعة التغييرات و تسجيلها, و لكن تأخيرات عديدة صغيرة افضل من تأخير واحد كبير.
203+
204+
205+
- **مجموعة وقت الخمول** -- يحاول جامع القمامة ان يعمل في حالة ان وحدة المعالجة المركزية (CPU) في حالة خمول حتي لا يؤثر علي عملية التنفيذ.
189206

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.
193207

194208
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.
195209

196-
## Summary
210+
هنالك العديد من التحسينات في خوارزميات جامع القمامة. و علي قدر ما اود ان اشرحها هنا,يجب ان نتوقف و ذلك لأن المحركات الختلفة تتبني طرق و حلول مختلفة و الأهم من ذلك ان الأشياء تتغير بتتطور المحركات, لذا ادرس أكثر "مقدما" فبدون الحاجة الحقيقية لمعرفتها فهي لا تستحق العناء الا ان كنت و بالطبع تمتلك الشغف للمعرفة فالروابط بالأسفل ستساعدك بالتأكيد.
211+
212+
## الملخص
213+
214+
اهم النقاط لتعرفها:
215+
216+
- جامع القمامة يعمل بشكل تلقائي, لا يمكن اجباره علي العمل او ايقافه
217+
- الكائنات تظل في الذاكرة طالما كان بالمكان الوصول اليها
218+
- كون الكائن له مرجع لا يعني بالضرورة ان يمكن الوصول اليه(من الجذر): قد تصبح مجموعة من الكائنات لا يمكن الوصول اليها
219+
220+
المحركات الحديثة تطور خوارزميات حديثة لعملية جمع القمامة
197221

198-
The main things to know:
222+
كتاب `"The Garbage Collection Handbook: The Art of Automatic Memory Management"(R.Jones et al)` يجمع بعضها
199223

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` في هذا المقال
203225

204-
Modern engines implement advanced algorithms of garbage collection.
205226

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)
207228

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+
و بالنسبة للمحركات الأخري, العديد من الطرق متشابهة, و لكن جامع القمامة يختلف في نقاط عديدة.
209232

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+
المعرفة المتعمقة للمحركات ضرورية في حين الحاجة الي تحسينات ذات مستوي متطور, فأن تخطط لمعرفتها بعد ان تصبح علي معرفة جيدة باللغة لهي بالتأكيد خطوة حكيمة.
211234

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

Comments
 (0)