Garbage Collection Tutorial

I want to begin by saying that this tutorial will introduce you to a memory management concept that may as well be controlled by the Wizard of OZ. The topics of garbage collection and the finalize method go hand in hand and I will cover the finalize method in the next tutorial. So what is Garbage Collection? Good question, in order to answer that I first need to provide a brief explanation of a memory location called the 'heap'. The heap is where your object data is stored. Every time you use the new operator to allocate and initialize an object, the Java Virtual Machine (JVM) creates your new object in a memory space on the heap and returns a reference to where your object is located. The size of the heap memory is largely determined by the amount of memory available on the device that you are running the JVM on - in this case, your computer. Let's begin by creating some new objects and displaying their object reference values to the console when the no-argument constructor is initialized.

Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>md GarbageCollection
C:\Java>cd GarbageCollection

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.

class GarbageCollection {
    public static void main(String args[]) {
        for (int i = 0; i < 6; i ++) {
            new Box();
class Box {
    Box() {
        System.out.println("This object reference is located on the heap at: " + this);

Now switch back to the command prompt (CMD) and type in javac and press Enter.
Now type in java GarbageCollection and press Enter.

C:\Java\GarbageCollection>java GarbageCollection
This object reference is located on the heap at: Box@659e0bfd
This object reference is located on the heap at: Box@2a139a55
This object reference is located on the heap at: Box@15db9742
This object reference is located on the heap at: Box@6d06d69c
This object reference is located on the heap at: Box@7852e922
This object reference is located on the heap at: Box@4e25154f

The numbers that you see after the Box@ represent some reference to a memory location on the heap. The numbers displayed to your console will most likely be different than the numbers displayed on my console - that is normal. Keep running the program over and over again. You should see the exact same thing displayed to the console each and every time you run this simple program. Each time you run the program, something is cleaning up the used heap memory and making it available for use again. That something is the automatic garbage collector. Basically, when the garbage collector decides to do its job, it looks at the heap memory, and determines which objects are in use and which are not. It will leave an object that still has an active reference alone, but if it finds an unreferenced object it will delete that memory and reclaim it for use again. You can't force the garbage collector to do its job. There are times when the garbage collector falls asleep on the job and you run out of memory on the heap and your program crashes! I can tell you right now, it will happen to you and when it does you will have to deal with it at that time. As an example, I can tell you that working with very large megapixel image objects can quickly chew up your heap and crash your program in no time flat! Anyway, we can definitely see that the garbage collector is doing its job in this small simple program. In my next tutorial, the Finalize Method Tutorial, I will explain how to implement the finalize method that 'should' run just before the garbage collector reclaims the object memory.

Final thoughts