1

I'm fantasizing of a tool to detect resource leaks. I have a good idea what the tool should look like and what the requirements are, but I lack one piece of the puzzle to make it work: an event occurring when a reference goes out of scope.

The tool would work like this: When a closeable is created, the tool builds a wrapper around it. When close() is called, the wrapper marks the object closed. When the object becomes garbage-collectable, if the object has not been closed, it delivers a stack trace of the current thread, identifying where in the code we are abandoning the object without closing it.

Sounds nifty, but I have not found any events that occur when references go out of scope. There are finalization and phantom reference events, but these occur in a different thread, after the guilty party has already vacated the scene of the crime. I need something like a method I can override that is called on reference release.

Any ideas for me?

TIA, - Tim.

5
  • 3
    No such mechanism. The closest one would be AutoClosable and try-with-resources, but that would already make sure that resources are cleaned so if you were to consistently use try-with-resources, there would be no need for your tool. Commented Feb 15, 2016 at 10:03
  • If I owned all the code that calls these objects, and if I were able to move all supported versions to Java 8, that would be a much better solution, yes. In the meantime... Commented Feb 15, 2016 at 17:47
  • Well, in the meantime you don't really have anything. Commented Feb 16, 2016 at 8:03
  • Java is not really suitable for resource management. Afaik, you can only achieve this in languages with scope-based resource management, like Rust, Swift and C++. Commented Feb 16, 2016 at 19:11
  • Well, thanks for validating that I'm not giving up prematurely anyhow. Commented Feb 17, 2016 at 19:51

1 Answer 1

3

Strictly speaking, a reference doesn't go out of scope. Variables go out of scope. References are values ... and don't have scopes.

There is no mechanism in Java that allows an object (or manager) to be informed when any reference variable containing its reference goes out of scope. The closest that is a try-with-resources that will automatically call close() on a declared resource variable when the try block terminates. (But that most likely won't help you detect the cases you are trying to detect.)

There is no mechanism in Java that allows an object (or manager) to be informed when it becomes unreachable. There are mechanisms such as finalize(), PhantomReference, and Cleaner (new in Java 9) that can be used to detect that an object has become unreachable some time earlier. But these mechanisms rely on the GC, and detection typically happens a long time after the "event" of becoming unreachable, and they don't tell you which reference variables were involved.


Other languages where storage management is less automatic do provide ways to do this. But there is a downside too. For example, C++ smart pointers (which rely on automatic incrementing and decrement of a ref count) incur a runtime overhead on assignments and do not fully deal with cases involving reference cycles. (Reference cycles need to be "broken" by hand.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.