How to delete an array properly in Java

Unlike C, Java provides automatic garbage collection,which will clear the array for you as it becomes unreachable(i.e goes out of scope).If you want you can make the array as null so that the memory location becomes unreachable.

    Foo[][] fooArray = new Foo[2][3];
    .
    .
    .
    fooArray = null;
    System.gc();

This gc call doesn't ensure that JVM will run garbage collector but it suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects


Explanation

You can not explicitly delete something in Java. It is the garbage collectors job to do that. It will delete anything which is not used anymore by anyone. So either

  1. let the variable fall out of scope or
  2. assign null
  3. or any other instance to it.

Then the array instance (as well as its subarrays) is not referenced anymore and the garbage collector will delete it eventually.


References

To understand why re-assigning the outer array is enough to also delete the inner arrays, you need to understand how they are referenced. Again, the garbage collector can delete anything which is unreachable. So let's take a look at an array such as:

int[][] outer = {{1, 2}, {3, 4}, {5, 6}};

We have 4 array instances. One is of type int[][] and three of type int[]. Also, we have one variable outer. The instances are referenced as follows:

                       ___> {1, 2}
                      |
outer  --> int[][] ---|---> {3, 4}
                      |
                      |___> {5, 6}

So by deleting outer, nobody references int[][] anymore. The garbage collector can now delete it. But that also removes all references to the inner arrays, so the garbage collector can now also delete them.

Now assume that you would reference one of the inner arrays by another variable:

int[][] outer = {{1, 2}, {3, 4}, {5, 6}};
int[] thirdInner = outer[2];
other = null; // remove the reference

The situation is now

outer  --> null

                       ___> {1, 2}
                      |
           int[][] ---|---> {3, 4}
                      |
                      |______> {5, 6}
                          |
thirdInner _______________|

So the garbage collector will now delete the outer array int[][], which also removes all references to the first and second inner array. But the third is still referenced by thirdInner, so after garbage collection we have:

outer       --> null
thirdInner  --> {5, 6}

At some point after the array goes out of scope, the garbage collector will reclaim the memory if there are no other references to it.

If you want to null your reference before the variable goes out of scope (keep in mind that if some other code has this reference, it won't get garbage collected):

Foo[][] fooArray = new Foo[2][3];

...

// this will null the reference to the array
fooArray = null;

Tags:

Java

Arrays