Garbage Collection for Newbies
I recently made contact with a friend I worked with 5 years ago on a project to start a new line of business for an internet cafe. He worked on the specifiec hardware requir4d for this while I wrote the software for the hardware, and the software the client would use to run their new business. The result was that we were both employed by the company to maintain and further develop this system. During this period I began to teachhim to write code in .NET and for some reason I was unsuccessful in explaining the garbage collector to him. No matter what real-world example I used.
On one occasion, we went to a small restaurant
for lunch as we did once a week to discuss the project and other plans we had.
This restaurant was virtually unknown because you had to go into a super
market and up to the second floor to get to it. Once there, you would stand in
line, make an order by the counter, pay, get your food, find a free table, eat
and leave. It was hardly ever full so this model worked quite well.
On this particular occasion however, we found it
packed, and after getting our food we had to wait a few minutes before finding
a free table. As we finished eating, we spoke of some example programs he had
been looking at, one of which explicitly invoked the garbage collector using
GC.Collect(). As I began to explain this, a cleaning lady quickly came and took
our plates and went over to clean them so they could be used for someone else’s
meals.
This was the break I needed. I then explained to
him the GC was much like this cleaning lady. She is there all the time though
we are usually unaware of her presence. We often finish our meals (set
variables to Nothing or null) and leave before she begins her work. On any
given day, we cannot determine at what point she will collect the empty or
abandoned plates from the tables for cleaning.
Someone higher up the food chain may instruct
her to start cleaning (explicitly invoking a collection using GC.Collect) and
we may be there to witness it, or not. When memory availability is low due to
some memory intense process, (restaurant is busy and all plates are in use),
the GC may scramble to reclaim memory used by unreferenced objects to allow
other processes to use the memory without having to hunt for it. This is just
what we had witnessed. Most of the plates were in use. People were sharing
tables (we had two strangers on ours) just to avoid eating on their feet. This wasn’t
helped by the fact that about 20 people were still waiting in line to get their
meals.
Any free plates and tables needed to be
reclaimed as quickly as possible to avoid people having to eat on dirty tables;
I won’t even consider dirty plates. The cleaning lady had to be on hand this
time and kept going round the dining area picking up any empty plates she could,
even if the people were still at the table and she would also clean the tables
as soon as they were freed.
This isn’t the best explanation in the world,
but it was enough to get the concept across to someone who didn’t understand “how
we use” the garbage collector. And I have employed it a number of times since I
encounter a lot of people starting out in .NET who think they have to use the
garbage collector, and wonder when it is best to do so.
The GC is something the .NET runtime will manage
for us and w never have to (at least we hardly ever have to) invoke it ourselves.
It has 3 generations with long running objects occupying the higher generations
which are collected less frequently. When our applications stop executing (are
terminated/unloaded) all the memory they were using is collected. We can also
sometimes submit specific objects ourselves by calling Dispose on IDisposable objects
(restaurant equivalent is handing in our plates at the cleaning sink for
cleaning). I must note that the GC doesn’t dispose these objects, they free any
resources they were using themselves and then tell the GC to ignore them when
collecting dead objects.
I just touched on this subject lightly and if you want a more detailed discussion to the technical aspects, here are a couple of links;
I hope this helps a little more than a few newbies. Happy coding.