title: Can Java detect when objects can be explicitly freed?
body: <p>Consider the following Java class:</p>
  
  <pre><code>class Main
  {
      private static Integer AddOne(Integer i) {
          return i + 1;
      }
      public static void main(String[] args)
      {
          Integer one = new Integer(1);
          System.out.println(AddOne(one));
      }
  }
  </code></pre>
  
  <p>In this simple example, it is easy to see that the object referred to by <code>one</code> is never referenced again after the call to <code>AddOne</code>.  It will be garbage collected some time after <code>main</code> exits.  However, having the object around in memory until it is reaped is a waste--perhaps not so much for an <code>Integer</code>, but if I had a large data structure (or many of them), it could impact performance.</p>
  
  <p>It seems to me that a compiler could examine the above code and see that it is not possible for a reference to <code>one</code> to escape the <code>Main</code> class.  Then, it could explicitly deallocate/finalize the reference at the end of the <code>main</code> function.</p>
  
  <p>Is such a facility present in any Java implementations?  If not, is there a technical reason why such a mechanism would not work?</p>