Hello, welcome back.
And we've now reached part three of this course and we're finally going to be talking about garbage
collection.
In the last chapter, I pointed out some of the optimizations that the virtual machine makes with the
creation of objects.
We now know that sometimes it places objects on the stack and also because of the use of the string.
So it might not even create some objects if it detects there's really a matching string for the one
we're trying to create.
But for this chapter and actually most of this section of the course, I'd like us to put this information
to one side and to continue to work on the basis that all objects are created on the heap as separate
objects.
Whilst we know this is not completely true, it's a good enough assumption and it's a good starting
point for us to be able to learn about garbage collection.
One of the big differences between Java and some other programming languages is that you might have
used, such as C are that when you finished using an object in Java, you don't have to tell Java that
it's no longer needed.
The virtual machine works this out automatically in languages like C or C++.
Plus the exact opposite of this is the case for any object on the heap in these languages.
The programmer must include code that tells the language that you finished with this object in C, you
do this by calling a function called free in visual basic.
You're meant to set the variable reference equal to nothing so that the language knows that the memory
being occupied by that object can be released.
If you don't do this, then well, certainly in earlier versions of that language, the memory that
was being used to store the object would never be released.
In fact, in some languages, even when your program finishes running, the memory occupied by objects
that you haven't freed up will still be in use.
And the only way to free it up is going to be to restart your computer if the computer's a Web server.
Well, we really don't want to have to do that on a regular basis.
This type of fault is called a memory leak.
And if it occurs, it's a horrible problem because your program is actually compromising the integrity
of the computer that it is running on.
If you're running programs that have a memory leak, then over time more and more of your computer's
memory will get used up and eventually your computer will start to slow down and eventually crash.
Memory leaks are the nastiest form of a fault that a computer program can have there.
Hard to detect and might be thought of as harmless because it can take such a long time before the impact
is seen.
And yet when the impact does happen, it can be catastrophic.
Finding memory leaks is incredibly difficult, and for programmers of languages like C, working out
the exact point that an object is no longer needed can be very hard.
So the good news is that Java decided to solve this problem by being a so-called managed language.
This means two things.
Memory leaks should be avoided because firstly, Java runs on a virtual machine.
The point here is that when you call the new keyword in Java to create a new object, you aren't actually
taking a memory from the operating system.
The memory is acquired by the virtual machine.
The virtual machine is actually just another program written in C, and this C program will control
the request for memory objects from the operating system, and it controls the freeing up of memory
when objects are no longer needed.
So we can think of the virtual machine as deciding when to put in the call to the free function that
C needs to erase memory.
It's pretty safe to assume that the designers of the virtual machine have correctly ensured there are
no leaks in this implementation.
If there were some leaks, while there'll be plenty of bug reports about it and I'm sure we'd know.
So if we assume that the virtual machine is correctly implemented, an operating system level memory
leak in Java should be impossible.
If it does happen, it's certainly not the fault of the Java programmer.
And the second way that Java avoids memory leaks is with the strategy of garbage collection.
A common misconception is that Java invented garbage collection.
Well, it didn't.
It was actually invented in 1959 with the Lisp programming language.
However, Java made the concept of garbage collection more popular, and today, other programming languages
use this concept, particularly Microsoft's dot net framework.
The idea of garbage collection is that programmers ask for objects to be allocated on the heap, but
they don't need to free them when they're finished with them.
Instead, an automatic process will analyze the heap, and it aims to work out which objects are no
longer needed and any unneeded objects can be deleted and the memory that they occupy can be freed up.