I need some solution that will help me tell for a some methods, in a case like this:
void myMethod(...)
{
MyObject obj = new MyObject();
// do stuff
}
if this method has ended or if obj is unreachable (which will indicate the method has ended).
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.
I know that with Weak/Soft/Phantom-References I can partially achieve this. If a GC is called, then by having:
void myMethod(...)
{
MyObject obj = new MyObject();
WeakReference objReference = new WeakReference(obj);
// do stuff
}
I can always verify in a different place:
if (objReference.get() == null) ...
However, this is only maybe true if GC occurred.
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...
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.