Array Lifetimes in Java

File.java:
  int [] function() {
         int [] myArray = new int[10];  // allocates myArray on HEAP
                                        // do some processing with array
         return myArray;        // the REFERENCE is copied back to caller
  }

  int [] someArray = function();  // gets a reference to memory on HEAP
                                  // all is well!

When the call to function() returns the local variable myArray becomes invalid. It will be collected by the garbage collector because it is no longer reachable from anywhere in this program. But the VALUE of myArray has been copied back to the caller and stored in the array variable someArray. This means that the memory allocated in function() is STILL REACHABLE from within your program via someArray. It will not be collected (yet). The caller has received a reference to valid memory.