1 00:00:00,480 --> 00:00:01,330 ‫We'll come back. 2 00:00:01,350 --> 00:00:06,390 ‫In this video, I would like to talk about the garbage collector and how C-sharp takes care of memory. 3 00:00:06,390 --> 00:00:12,240 ‫So the garbage collection is a tool that is available in multiple programming languages. 4 00:00:12,240 --> 00:00:16,950 ‫And in some of them it's done manually and others it's done automatically. 5 00:00:16,950 --> 00:00:23,010 ‫And the dot net framework provides an automatic memory management, which means that you don't have 6 00:00:23,010 --> 00:00:28,380 ‫to use the garbage collector manually as you do in all the programming languages. 7 00:00:28,380 --> 00:00:37,140 ‫So programming languages such as C or C++ and others, they have a manual garbage collection that you 8 00:00:37,140 --> 00:00:41,400 ‫have to run every now and then and you have to know when you have to run it. 9 00:00:41,400 --> 00:00:44,130 ‫So that's something that is done automatically in C shop. 10 00:00:44,130 --> 00:00:49,200 ‫That's why it's not something that yeah, many people use very often. 11 00:00:49,200 --> 00:00:57,510 ‫So once you have more complex programs and really advanced, then you will start using it every now 12 00:00:57,510 --> 00:00:57,840 ‫and then. 13 00:00:57,840 --> 00:01:02,730 ‫But really it's not something that you have to use all the time or even have to use at all in most cases. 14 00:01:02,730 --> 00:01:05,910 ‫So that's why it's not a major topic, so to speak. 15 00:01:06,090 --> 00:01:06,540 ‫All right. 16 00:01:06,540 --> 00:01:11,370 ‫So let's say we have this object called Dennis and it's of type human. 17 00:01:11,370 --> 00:01:16,590 ‫So I created a class which is called human and there are multiple different properties and so forth. 18 00:01:16,590 --> 00:01:21,270 ‫So in the end, I'm just creating an object of type human, and that's called Dennis. 19 00:01:21,270 --> 00:01:30,270 ‫And by doing so, by using this new keyword, I require memory because it creates a object in my memory. 20 00:01:30,270 --> 00:01:38,190 ‫So C Sharp allocates memory for us and the framework takes care of reclaiming memory for use in the 21 00:01:38,190 --> 00:01:41,190 ‫system once objects are no longer being used. 22 00:01:41,190 --> 00:01:46,380 ‫So this case, I'm creating this object and then I might use it and I might use it again and again. 23 00:01:46,380 --> 00:01:51,390 ‫But at one point I'm not going to use it anymore and I'm maybe even cutting the references to it. 24 00:01:51,390 --> 00:01:58,230 ‫And then, yeah, the garbage collector will clean it up because this memory is now blocked and the 25 00:01:58,230 --> 00:02:00,000 ‫garbage collector has to clean up. 26 00:02:00,000 --> 00:02:06,270 ‫So it has to make this memory available again for other objects, for example. 27 00:02:08,350 --> 00:02:11,280 ‫Now let's look at it from a visual perspective. 28 00:02:11,290 --> 00:02:16,390 ‫So we have this system memory pool on the left hand side and our program on the right hand side. 29 00:02:16,390 --> 00:02:22,240 ‫And the system memory pool has a bunch of memory, so we can use this memory for multiple different 30 00:02:22,240 --> 00:02:22,930 ‫programs. 31 00:02:22,930 --> 00:02:29,020 ‫But now we have our program which runs it, and now in our program we create an object. 32 00:02:29,200 --> 00:02:33,310 ‫Now, suddenly we need a little bit of space in our memory. 33 00:02:33,610 --> 00:02:33,970 ‫All right. 34 00:02:33,970 --> 00:02:39,160 ‫So there's a reference from our program to that memory where our object is stored. 35 00:02:39,190 --> 00:02:39,550 ‫All right. 36 00:02:39,550 --> 00:02:45,370 ‫So there's a direct reference and we can reuse it and we can call it from different spots and so forth. 37 00:02:45,730 --> 00:02:46,150 ‫All right. 38 00:02:46,150 --> 00:02:48,790 ‫And then we have another one and another one. 39 00:02:48,790 --> 00:02:51,370 ‫So we have multiple different objects that we created. 40 00:02:51,370 --> 00:02:58,270 ‫And each time we create a new object, there is a memory allocated in the memory pool and there is a 41 00:02:58,270 --> 00:03:00,940 ‫reference from our program to that memory. 42 00:03:01,090 --> 00:03:02,860 ‫So these references are there. 43 00:03:02,860 --> 00:03:06,070 ‫So there's a reference from our program to the memory pool. 44 00:03:06,280 --> 00:03:09,190 ‫Now at one point the reference might be deleted. 45 00:03:09,190 --> 00:03:16,480 ‫So we maybe delete the reference by setting a variable null or by setting the object null, and then 46 00:03:16,600 --> 00:03:18,550 ‫the reference is suddenly gone. 47 00:03:19,090 --> 00:03:26,560 ‫Now there is no reference from our program to this little memory allocation here anymore. 48 00:03:26,560 --> 00:03:32,440 ‫So our garbage collector notices it and says, okay, there's no reference, so this little piece of 49 00:03:32,440 --> 00:03:39,370 ‫memory is never used, so let's just clean it up or let's re allocate it or let's just freed up for 50 00:03:39,400 --> 00:03:40,660 ‫other allocation. 51 00:03:40,900 --> 00:03:42,490 ‫And that's what happens then. 52 00:03:44,850 --> 00:03:49,290 ‫So now let's have a look at an example of when the garbage collector would kick in. 53 00:03:49,350 --> 00:03:53,400 ‫And I have a function here called my function, and I'm creating an object here. 54 00:03:53,400 --> 00:03:54,720 ‫Again, it's called Dennis. 55 00:03:54,720 --> 00:04:00,550 ‫And then Dennis is calling his methods, or we call his methods such as teach. 56 00:04:00,570 --> 00:04:05,100 ‫Then we set his age and then we call another method, get older and so forth. 57 00:04:05,220 --> 00:04:12,810 ‫But at one point, this Dennis object in the memory is not required anymore because the function call 58 00:04:12,810 --> 00:04:13,580 ‫is over. 59 00:04:13,590 --> 00:04:15,370 ‫So the function ends the object. 60 00:04:15,390 --> 00:04:22,680 ‫Dennis goes out of scope and there are no references to Dennis anymore because my function is a function 61 00:04:22,830 --> 00:04:28,860 ‫and we have this object which is within a function, so the scope of it is within the function. 62 00:04:28,860 --> 00:04:34,980 ‫So once the function call is over, there's no direct reference to it anymore because we can't reference 63 00:04:34,980 --> 00:04:38,730 ‫to an object which is within a function from outside of this function. 64 00:04:38,730 --> 00:04:39,120 ‫Right. 65 00:04:39,120 --> 00:04:43,590 ‫So we would need to create fields for that or do other approaches for this. 66 00:04:43,590 --> 00:04:49,200 ‫But in the end, the garbage collector, which is clean up this Dennis and re-allocate this memory. 67 00:04:49,350 --> 00:04:54,820 ‫And the general idea here is really that this is just one simple example. 68 00:04:54,840 --> 00:04:59,520 ‫There are plenty of other situations where the garbage collector would kick in, but this is something 69 00:04:59,520 --> 00:05:00,740 ‫that we have already used. 70 00:05:00,750 --> 00:05:06,210 ‫So the concepts that you can see here, we have seen them in other videos or lectures of this course 71 00:05:06,210 --> 00:05:06,810 ‫already. 72 00:05:06,990 --> 00:05:08,630 ‫That's why I chose this example. 73 00:05:08,640 --> 00:05:11,370 ‫So what do you need to know about the garbage collection? 74 00:05:11,370 --> 00:05:18,180 ‫Well, you don't have to call the garbage collector manually, but you can, so you can do it manually. 75 00:05:18,540 --> 00:05:24,600 ‫The garbage collector requires processing power, so it's on a separate thread and it's very lightweight. 76 00:05:24,600 --> 00:05:27,560 ‫But still some processing power is required. 77 00:05:27,570 --> 00:05:32,280 ‫The garbage collectors goal is to free up memory but not use too much processing power. 78 00:05:32,280 --> 00:05:38,310 ‫So it will not need much processing power in general, but it's not going to be called all the time 79 00:05:38,310 --> 00:05:42,420 ‫or the garbage collector won't run all the time to free up memory. 80 00:05:42,420 --> 00:05:47,580 ‫It's just going to run when it's necessary or when it deems necessary, when it thinks it's necessary. 81 00:05:47,580 --> 00:05:49,860 ‫So that's really the tricky part here. 82 00:05:50,070 --> 00:05:53,370 ‫The memory isn't necessarily reclaimed right away. 83 00:05:53,370 --> 00:05:58,590 ‫It might take a little while for the garbage collector, even if you call it manually, doesn't have 84 00:05:58,590 --> 00:05:59,550 ‫to run straightaway. 85 00:05:59,550 --> 00:06:02,670 ‫So these are some tricky things about the garbage collector. 86 00:06:02,670 --> 00:06:08,310 ‫You can implement a finalized method to execute code just before an object is released from memory by 87 00:06:08,310 --> 00:06:09,680 ‫the garbage collector. 88 00:06:09,690 --> 00:06:18,480 ‫This means that if you decide to run a specific code just before an object is cleaned up, so just before 89 00:06:18,480 --> 00:06:20,100 ‫the garbage collector is cleaning it up. 90 00:06:20,100 --> 00:06:26,130 ‫So once there are no references to it anymore, for example, then you can use the final laser method 91 00:06:26,130 --> 00:06:30,720 ‫within this class in order to implement the specific functionality that you want them to run. 92 00:06:30,810 --> 00:06:36,150 ‫Maybe you want to clean up stuff yourself just before the garbage collector comes in and cleans up your 93 00:06:36,150 --> 00:06:41,340 ‫object or relocates the memory or releases it to this object from memory. 94 00:06:41,880 --> 00:06:42,420 ‫All right. 95 00:06:42,420 --> 00:06:44,280 ‫When does the garbage collector run? 96 00:06:44,280 --> 00:06:50,610 ‫Well, when the system has low physical memory, when the memory allocated exceeds a preset threshold, 97 00:06:50,610 --> 00:06:58,020 ‫when the garbage collector collect or collect method is called, even though this is a super tricky 98 00:06:58,020 --> 00:06:58,500 ‫method. 99 00:06:58,500 --> 00:07:02,430 ‫So this is something that is. 100 00:07:02,430 --> 00:07:04,770 ‫Yes, very specific, very special. 101 00:07:04,770 --> 00:07:09,870 ‫If you want to know more about it, you should check out the documentation on the garbage collector. 102 00:07:09,870 --> 00:07:17,280 ‫But the dot collect method isn't always called straight away and it doesn't directly clean everything 103 00:07:17,280 --> 00:07:17,610 ‫up. 104 00:07:17,610 --> 00:07:22,860 ‫It's more like you're giving the tool a suggestion, so you're saying, okay, garbage collector. 105 00:07:22,860 --> 00:07:29,040 ‫I personally think that would be a good point or a good spot to collect the garbage, but in some cases 106 00:07:29,040 --> 00:07:32,790 ‫it might not accept it or it might just do it later on or something like that. 107 00:07:32,790 --> 00:07:37,440 ‫Maybe it just did garbage collection and it decides that it's not necessary or something like that. 108 00:07:37,440 --> 00:07:39,570 ‫So it's a very tricky one. 109 00:07:39,570 --> 00:07:42,570 ‫If you want to know more about it, check out the documentation. 110 00:07:42,570 --> 00:07:46,920 ‫But I think this is a good starting point to understand what the garbage collector does. 111 00:07:46,920 --> 00:07:52,800 ‫And as I said, in most cases you won't need it because nowadays we have so much memory. 112 00:07:52,800 --> 00:07:57,690 ‫And if you don't have huge memory leaks in your programs, you should never run into these problems.