1 00:00:05,330 --> 00:00:05,730 Hello. 2 00:00:07,890 --> 00:00:16,170 In this video, we'll talk about topic nine, what our generation's garbage collector has a lot of work. 3 00:00:16,500 --> 00:00:21,790 It needs to identify objects to be removed, which we learned about in the previous lecture. 4 00:00:22,170 --> 00:00:24,390 Then it must actually remove them. 5 00:00:24,660 --> 00:00:28,500 Finally, it must fragment the memory of the application. 6 00:00:28,890 --> 00:00:35,910 As we know, the garbage collector marks objects as writable or unreachable and removes the latter from 7 00:00:35,910 --> 00:00:36,570 their memory. 8 00:00:36,960 --> 00:00:38,760 Imagine there is some object. 9 00:00:39,090 --> 00:00:43,920 Let's call it object a during the first execution of the garbage collector. 10 00:00:44,220 --> 00:00:50,100 This object is marked as readable, and so are objects reachable from object a. 11 00:00:50,490 --> 00:00:54,750 After some time, the garbage collector gets to work again and again. 12 00:00:54,900 --> 00:00:58,110 It marks objects and its friends as switchable. 13 00:00:58,530 --> 00:01:03,270 Then again, sometimes buses and garbage collector gets triggered again. 14 00:01:03,660 --> 00:01:07,050 And one more time it marks objects as reachable. 15 00:01:07,440 --> 00:01:11,970 If I were the garbage collector, I would probably get a bit frustrated. 16 00:01:12,240 --> 00:01:18,540 This object is obviously a long grift, and I don't want to check if it's still needed every time I 17 00:01:18,540 --> 00:01:19,410 get to work. 18 00:01:19,770 --> 00:01:21,150 I have other things to do. 19 00:01:21,780 --> 00:01:24,310 Well, that's exactly the optimization. 20 00:01:24,330 --> 00:01:30,300 The garbage collector creators come up with to not make it check each object every time. 21 00:01:30,630 --> 00:01:37,530 If some object survived a couple of cycles of the garbage collectors work, it's most likely a long-lived, 22 00:01:37,680 --> 00:01:40,680 and we should check it only every once in a while. 23 00:01:41,280 --> 00:01:46,050 This optimization introduced the concept of generations of objects. 24 00:01:46,440 --> 00:01:53,670 Once an object is first created, it is assigned to Generation Zero if it survives its first collection. 25 00:01:53,970 --> 00:01:58,140 It advances to generation one if it survives the second. 26 00:01:58,230 --> 00:02:05,430 Its advances to generation to the garbage collector checks objects from generation zero most frequently 27 00:02:05,850 --> 00:02:12,060 less frequently, its tookes objects from generation one and the least frequently from generation two. 28 00:02:12,720 --> 00:02:16,350 It makes sense if the object survived two collections. 29 00:02:16,590 --> 00:02:22,590 It's most likely long lived and there is a big chance it will survive collections number 10, 20 or 30 00:02:22,590 --> 00:02:23,400 100. 31 00:02:23,850 --> 00:02:25,950 We don't need to check that often. 32 00:02:26,430 --> 00:02:29,130 For example, think of some larger objects. 33 00:02:29,400 --> 00:02:35,730 Often there is one object created at the start of the program execution, and it is passed around to 34 00:02:35,730 --> 00:02:37,140 other crafts that needs it. 35 00:02:37,620 --> 00:02:43,890 Its lifecycle is basically as long as the time the application runs as the opposite. 36 00:02:43,980 --> 00:02:50,670 We often create objects that lasts only for a very short time, like aligning with objects created to 37 00:02:50,670 --> 00:02:53,040 carry some data between link quarries. 38 00:02:53,340 --> 00:02:59,590 In this case, it is reasonable that the garbage collector will quickly remove them so they don't occupy 39 00:02:59,610 --> 00:03:01,080 memory for too long. 40 00:03:01,620 --> 00:03:06,240 In a world tuned application, most objects die in generation zero. 41 00:03:06,570 --> 00:03:13,020 Please note that during a collection of a generation, all previous generations are also corrected, 42 00:03:13,200 --> 00:03:15,660 so one generation zero is collected. 43 00:03:15,780 --> 00:03:19,620 No other one is, but one generation two is collected. 44 00:03:19,740 --> 00:03:22,290 Generation zero and one are two. 45 00:03:22,680 --> 00:03:29,130 And that's why collecting objects from generation two is sometimes called a full garbage collection. 46 00:03:29,790 --> 00:03:37,380 One more thing that needs to be mentioned is the large objects heap when a very large object so larger 47 00:03:37,380 --> 00:03:41,400 than 85 thousands of birds is initially created. 48 00:03:41,730 --> 00:03:48,030 It is stored in a special area of memory called the large objects heap, and it is assigned to generation 49 00:03:48,030 --> 00:03:49,350 two right away. 50 00:03:49,770 --> 00:03:56,070 It gets this special treatment because it really happens that very large objects are short lived. 51 00:03:56,470 --> 00:04:01,140 Also, the objects in the large objects heap have one more special feature. 52 00:04:01,440 --> 00:04:02,520 They are pinned. 53 00:04:02,790 --> 00:04:05,530 It means they were meant to be moved in memory. 54 00:04:05,550 --> 00:04:11,010 During that, the fragmentation step of the garbage collectors work what happens after removing and 55 00:04:11,020 --> 00:04:12,900 reference objects from memory? 56 00:04:13,350 --> 00:04:20,430 This is because the larger object is the more expensive is the operation of moving it, and the harder 57 00:04:20,430 --> 00:04:24,480 it is to find the chunk of memory large enough to fit it. 58 00:04:24,840 --> 00:04:30,000 It's better to pin such large objects and move smaller objects around them. 59 00:04:30,630 --> 00:04:37,800 Let's summarize the garbage collector device objects into three generations zero, one and two. 60 00:04:38,010 --> 00:04:40,170 Depending on their longevity. 61 00:04:40,680 --> 00:04:46,620 Short lived objects belong to generation zero, and if they survived their first correction, they are 62 00:04:46,620 --> 00:04:53,700 moved to generation one and after duct to generation to the garbage collector collects objects from 63 00:04:53,700 --> 00:04:58,410 generation zero most often and from generation to earliest often. 64 00:04:58,920 --> 00:05:02,550 This feature is introduced in order to improve garbage collector. 65 00:05:02,760 --> 00:05:09,480 Performance objects that survived a couple of cycles of the garbage collectors work tend to be long 66 00:05:09,480 --> 00:05:12,750 lived and they don't need to be looked upon so often. 67 00:05:13,110 --> 00:05:17,760 This way, the garbage collector has less work to do, so it can do it faster. 68 00:05:18,390 --> 00:05:22,380 During the interview, you might be asked What is the large objects? 69 00:05:22,380 --> 00:05:29,280 Heap is the special area of the heap reserved for objects larger than 85000 bites. 70 00:05:29,880 --> 00:05:36,480 Such objects logically belong to generation two from the very beginning of their existence and are pinched. 71 00:05:36,990 --> 00:05:39,840 So what does it mean that the object is pinned? 72 00:05:40,230 --> 00:05:46,380 It means it will not be moved during the memory, the fragmentation that the garbage collector is executing. 73 00:05:46,710 --> 00:05:48,180 It is an optimization. 74 00:05:48,210 --> 00:05:54,360 Ask what objects are expensive to move, and it's hard to find a chunk of memory large enough for them. 75 00:05:55,080 --> 00:05:57,150 All right, that's it for this lecture. 76 00:05:57,450 --> 00:06:00,090 Thanks for watching, and I'll see you in the next one.