sourabh
sourabh

Reputation: 11

Java Memory leak: Java.lang.ref.finalizer object

I've upgraded a Java microservice from Java 8 to Java 17, and I'm encountering an Out of Memory (OOM) issue when processing bulk records. In heap dumps, the suspect is identified as java.lang.ref.Finalizer loaded by the system class loader, occupying 44.97%. Despite reviewing the code and confirming the absence of finalize() methods, I haven't found any relevant information in the heap dump related to my microservice. I am using Eclipse Memory Analyzer tool.

Problem Suspect

(https://i.sstatic.net/ennEM.png)

As it can be seen, almost half of the space is taken by this only i.e. java.lang.ref.Finalizer but when I did a field Java search in my project, I don't see any code using the finalize() method.

And I have hit a dead end trying to find the memory leak. Is there any other reason which might be causing the issue.

Upvotes: 1

Views: 581

Answers (1)

Gregor Koukkoullis
Gregor Koukkoullis

Reputation: 2305

There might be other libraries or the JDK with overridden finalize() method.

Avoid creating large amounts of object of a classes with finalize() method. The GC keeps a queue and collects all objects that needs to be finalized. If you create and drop objects too fast the queue can fill up quickly and it takes time for the GC to clean them. It might help to reuse objects instead of dropping them.

Also make sure to "close()" objects where possible/necessary. This can help to free resources before waiting for the GC to do its job.

Upvotes: 0

Related Questions