0

Finalizer thread are responsible clearing objects in the finalization queue. Ironically does the same finalizer thread can responsible for OOM?

Mohan Raj
  • 1,104
  • 9
  • 17
  • 4
    Yes. You're theoretically able to run arbitrary code in the thread, so it can easily OOM. Do you have any specific problem in mind? Any code to share? – Petr Janeček Apr 30 '15 at 10:06
  • I have no code to share but GC is responsible for clearing objects in the heap but finalizer thread works independently so wanted to know the ways how finalizer can lead to OOM – Mohan Raj Apr 30 '15 at 10:15
  • I have tried to make the `finalize()` method do something wonky, but it turns out it's not an easy task. Any thrown OOM will get swallowed by the GC thread and it will not crash the VM, any unsuccessful allocations will simply throw the OOM Error. It might require some sareful engineering to make it crash. – Petr Janeček Apr 30 '15 at 10:41
  • I have tried similar like that but did not get it worked but i have seen issue in the past where Finalizer lead to OOM. Wanted to find out the same. Stackoverflow has lots of java genius so someone will help me in finding and understanding the behaviour – Mohan Raj Apr 30 '15 at 10:45

2 Answers2

1

Short answer: theoretically yes.

More specifically it depeneds on how your finalizer thread is constructed and what he does. In general any new object creation can lead to OOM if no free memory left.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
0

The short answer is yes.

Some classes implement the Object.finalize() method. Objects which override this method need to called by a background thread call finalizer, and they can't be cleaned up until this happens. If these tasks are short and you don't discard many of these it all works well. However if you are creating lots of these objects and/or their finalisers take a long time, the queue of objects to be finalised builds up. It is possible for this queue to use up all the memory.

If many objects with finalizers are created, it is likely that there will be performance issues, even if the underlying native resources are explicitly freed using try-finalize blocks.

Code to try and outpace java garbage collector with finalizers showed the following result.

It is surprisingly easy to outpace the Java garbage collector if lots of objects with finalizers are created, leading to spurious out-of-memory errors with lots of objects which could theoretically be reclaimed.More on this can be found on the link http://www.enyo.de/fw/notes/java-gc-finalizers.html

There are some apps that have hit this finalizer queue build up problem in the past, so it is worth considering how to deal with it. One obvious way is to increase the priority of the "Finalizer" daemon thread - there is no API for this, so you have to run through all the threads to find it by name, then increase it's priority.

You could also take explicit control over finalization by removing the finalize() method and using your own explicit queue using your own Reference objects in a very similar way that the Finalizer class processes the objects and their finalize() methods . That way you control your finalization processing thread's priority and schedule.

Note that neither of these techniques reduce the overheads in having finalizable objects, they just avoid the queue building up because of the lower priority thread.

How finalization works is shown in the link below for the curious

http://www.fasterj.com/articles/finalizer2.shtml

Raj
  • 1,945
  • 20
  • 40
  • @Alex agreed. But in this case the answer is yes. The links were provided as supplementary info in case he wants to delve more into it. You do not want me to paste 4 pages of info here ! . – Raj Apr 30 '15 at 10:22
  • If "yes" isn't enough by itself (it isn't) then it needs a description. Links != description. You claim to agree, so why not write a description. (Some great answers on this site are far longer than 4 pages!) –  Apr 30 '15 at 10:31
  • @Alex I said i agreed to the notion but every rule,notion has exceptions and it should be decided based on the situation.Why do not take up homework questions..because we are here to help not do the work for others. In this case i just showed where to "look for more info" and i want to stress "more info" as written in my answer. It is up to the person who asked if he want to look at more info or not. I do not want stackoverflow to be a wikipedia. so sometimes i post links that might help viewer understand the situation more if he wants it ..that all. No more argument. – Raj Apr 30 '15 at 10:42
  • @Alex accept your downvote and your right of opinion. And certainly appreciate you putting a comment after downvote :-) – Raj Apr 30 '15 at 10:43
  • It's not an opinion; it's a rule of the site; just posting a link is never justified. Your answer is at risk of being removed. Why not add a description and earn some more reputation here? –  Apr 30 '15 at 10:56
  • @Alex dont really bother about the reps. But you are as persistent as me. and i appreciate you time on the comments. I have edited my answer and posted a more descriptive answer. – Raj Apr 30 '15 at 11:16
  • Cool. Not sure about the bold text but I'm happy to +1 your answer now. (This is one of the few sites where reputation means anything; you have more power to make it a more productive resource). Have a nice day! –  Apr 30 '15 at 11:23