Garbage Collection / Finalize
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
Before we leave this subject of the garbage collector, I do want to give you just a couple of things that you can data optimize garbage collection, this whole process. First of all I mentioned a couple of videos back that the garbage collector is using generations and it's using three generations: zero, one and two. And it fills one generation up and then performs a garbage collection when there is no room to put any more data there. Now, I also mentioned that at first it sounds like that this is limited amount of area that we can use but in actuality I think it is 256MB did 32 million of these things or something that is really bizarre high number. Now there is something that you can do on the front end if you think you are going to run out of memory, or if you think there is a possibility: that is use a weak reference. Now this is very simple notice the difference here: instead of Dimming A as a new Mark, assuming Mark is a class, I Dimmed A reference and then passed it to Mark. Now what this is going to do is create an instance called 'a' of Mark, but this is a weak reference. So what happens is this thing sitting out there in the managed heap and if the optimizer, or if the garbage collector needs more space and it don't have any it will basically finalize and destroy this Mark object. Now I'll have to check for that, if I declare if this as weak I am going to have to check to see if this object is still out there, when I get ready to use it. So again I'll refer you out to the software development kit and the documentation to get an idea on exactly how to go about this; obviously there is always a few more ifs ands and buts. But again this allows collection when the memory gets low, so you don't get a stack overflow error. Now let's talk about forcing collection. You hear all the time that garbage collection's automatic, you have to wait on it, you don't know when it's going to happen. Well it's not exactly true, although you don't want to make a habit of this but you can call the system.GC.collect method and pass the generation as an integer - zero, one, or two. And you can force collection on that particular generation. Now again, you may be thinking - wait a minute, why don't I just force collection on a generation as I opposed to call and dispose? Again I am going to send you out to documentation on that. This is obviously much more resource intensive because it begins to move some of these things around. You can locate which generation your object is in by calling the system.GC.GetGeneration and pass it the object as integer, and it will return to you as integer the generation of that particular object and if you need to know that information. So it is possible to check to see what generation an object is in, and then call collection of that object, but again keep in mind if you think dispose and finalize are resource intensive or not very efficient, where do you see garbage collection being forced time and time again? Because again it's going to move some of these pointers around, it is going to drop a bunch of things, it is going to call finalize. You're really going to kick off lot of different things; so just know that you've got it if you need it, don't overuse it. Again I want to stress to you the Microsoft recommendation is to let garbage collection go. Let it take care of your objects automatically for you. Again throw these in the toolbox if you ever need to manually explicitly manage your resources, these are some of the ways that you can do it, and these are some of the ways to optimize the process.
Tutorial Information
| Course: | Microsoft Visual Basic .NET |
| Author: | Mark Long |
| SKU: | 33433 |
| ISBN: | 1932072349 |
| Release Date: | 2003-05-27 |
| Duration: | 6 hrs / 87 lessons |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 