<#> Earlier in the course, you may have heard me refer to C# and .NET as a
<#> managed-code environment.
<#> What that basically means is that the .NET Framework provides automatic
<#> memory management for you.
<#> You as the programmer are not responsible for freeing up the memory that your
<#> program's objects use during the course of the running of your program.
<#> Now, this is not like other languages.
<#> In languages like C and C++ and Objective-C, you as the programmer, when you
<#> create objects that take up memory, you are responsible for making sure that
<#> that memory gets returned to the system at some point.
<#> But languages like .NET and other language like JavaScript in Java and so on,
<#> those are managed-code environments.
<#> There are ways that the system figures out that memory is no longer being used
<#> by your program and reclaims it automatically.
<#> Now, to take a simple example, when you do something like this in your C#
<#> program, you have some object type definition, maybe you've made a new class,
<#> and you use the new operator to create a new instance of that object,
<#> that allocates memory somewhere in the system. That tells .NET, hey, you've got
<#> to go find some memory to hold my object.
<#> And the Framework will take care of figuring out when this object is no longer being used.
<#> You don't have to do anything special. .NET will just figure out that you're
<#> done using this object, and it will put the memory back in the system for you.
<#> So let's take a look at conceptually how this works.
<#> There is a big pool of memory available to the system, and we can think of it as
<#> this big purple box you see right here.
<#> Now, your program is going to use this memory during the course of its
<#> operation. And as your program runs, it's going to allocate blocks of memory and
<#> when it does that those blocks of memory are going to be taken out of the system
<#> memory pool and assigned to your program. And then in your program will do
<#> whatever it does with that memory and all these objects.
<#> At some point there is a special class in the .NET Framework called the
<#> Garbage Collector, and garbage collection is the process by which .NET figures
<#> out that memory is no longer being used by your program and can take it back into the system.
<#> So the Garbage Collector has a way of keeping track of the objects that you have
<#> allocated, and when it figures out that these objects are no longer being used,
<#> it simply puts them back in the system memory pool.
<#> This process is completely invisible to your program.
<#> You don't have to worry about doing this at all.
<#> It all just happens for you.
<#> So let's take a look at how garbage collection works.
<#> Suppose we have a function, looks like this.
<#> It's called myFunction. And inside myFunction I've got this new MyObject.
<#> Well, out in the system memory pool when I do that, a block of memory is going
<#> to be allocated that holds MyObject.
<#> That creates what's called a reference.
<#> That myObj variable that you created inside your function is now going to hold a
<#> reference to that memory location.
<#> Now, you can go and do a bunch of things:
<#> you can call a method, you can set a property, you can call SomeOtherMethod.
<#> But when that function ends, that myObj variable is going to go out of scope.
<#> And at that point there are no more references to MyObject. Because that
<#> variable is gone now, that little reference line disappears. And the Garbage
<#> Collector comes along and says, hey, no one is using that thing anymore.
<#> I can just return that object back to the memory pool.
<#> So what are some important things to know about garbage collection?
<#> First, your program does not need to initiate this process; the Garbage
<#> Collector figures out when it needs to run and reclaim memory.
<#> Now, there is a way for your program to initiate garbage collection, and we'll
<#> actually take a look at that in an example in just a bit.
<#> The Garbage Collector balances between performance and app memory usage, and the
<#> objects are not necessarily reclaimed right away.
<#> So these two things mean that you, A, don't know when Garbage Collection
<#> is going to happen.
<#> The Garbage Collector tries to figure out how your program is doing its work and
<#> how much memory is being used, and it tries to strike a balance between making
<#> sure that your application doesn't take a performance hit while figuring out how
<#> much memory is being used and trying to say, okay, well, memory use is getting
<#> kind high, maybe I should go take some of that memory back.
<#> The other thing you need to realize that when you're finished with an object,
<#> it might stick around for a while until the Garbage Collector figures out that it needs to run.
<#> Now, objects can detect that they are about to be released from memory, so
<#> when you create an object in a class, you can actually write a method called the finalizer.
<#> Now, this is rather advanced.
<#> I'm not going to cover it in this course. But you can just be aware that there
<#> are ways for your objects to figure out that they're about to be released back
<#> into the system memory pool. be released back