title: any way to tell if a method ended or local in method in no longer in use?
body: <p>I need some solution that will help me tell for a some methods, in a case like this:</p>
  
  <pre><code>void myMethod(...)
  {
      MyObject obj = new MyObject();
      // do stuff
  }
  </code></pre>
  
  <p>if this method has ended or if obj is unreachable (which will indicate the method has ended).</p>
  
  <p>I'm writing a small java agent, and I am hoping for some technique that either will let me know for sure this method ended or obj is unreachable... In my case the "do stuff" section doesn't use obj at all. It doesn't pass this variable somewhere else or adds it to some collection.</p>
  
  <p>I know that with Weak/Soft/Phantom-References I can partially achieve this. If a GC is called, then by having:</p>
  
  <pre><code>void myMethod(...)
  {
      MyObject obj = new MyObject();
      WeakReference objReference = new WeakReference(obj);
  
      // do stuff
  }
  </code></pre>
  
  <p>I can always verify in a different place:</p>
  
  <pre><code>    if (objReference.get() == null) ...
  </code></pre>
  
  <p>However, this is only maybe true if GC occurred.</p>
  
  <p>Is there any alternative way to achieve this?
  For example, some marking the JVM may put on this variable since it is obvious this is a local that is definitely garbage-collectible...</p>
  
  <p>I don't want to rely on ASM - and add a try-finally wrapper to all of these methods. Was hoping for a more subtle, reference based solution.</p>