I was attempting to instrument some of my Java code to ensure objects were being garbage collected correctly, and I found that surprisingly it wasn’t being called as often as I expected.
I am now wondering if this is because of faulty instrumentation or an actual memory leak I need to solve. the VisualVM profiler seems to indicate the former.
The situation of concern is that I have a thread which handles requests and inside the request creates thousands of temporary objects. Sometimes, the socket this thread writes to is unexpectedly closed and the thread hits an exception and dies.
When the Thread dies, it doesn’t seem like .finalize() is ever called on those objects. Is this a reason to not trust my instrumentation?
No. There is no guarantee that it will be called. However, it is usually called. It may be that you are not properly releasing your objects, or the garbage collector has not run, or the finalizer thread is waiting for a good time to run.
You can read about it in the Finalization of Class Instances portion of the language spec.