0

System.gc() is suggests way to run GC and so does System.runFinalization();

but the use Jmap to do heap dump, it will force to do GC/full GC. so my question is: are there any tools/methods/hooks just like Jmap heap dump that can be used to force execute runFinalization?

I'd like to describe my situation a little bit more: my heap size won't decrease after a certain time and when I do a head dump I can see that:

1: 6793014 271720560 java.lang.ref.Finalizer

2: 7251647 232052704 java.util.HashMap$Node

3: 6791349 162992376 XX.WP<--- which has override finalize method

so I assumed that it could be caused by finalize method and I'd like give it a quick verification by force execute runFinalization.

Keyang Ma
  • 55
  • 1
  • 8
  • As mentioned [here](https://stackoverflow.com/questions/2506488/when-is-the-finalize-method-called-in-java) the finalize method is run when the object is garbage collected. Its not recommended to rely on this method beeing called. What is it that you do in these finalize methods? – second Nov 05 '19 at 20:46
  • I have one more questions: since System.runFinalization() is of suggestion. I'd like to know what is the evaluation process and under what condition it will decide to do or not to do the execution? – Keyang Ma Nov 05 '19 at 20:50
  • @second first in finalize method it clean the resource allocated by JNI. second it is the legacy code left to me. I have read the link u mentioned. thanks – Keyang Ma Nov 05 '19 at 20:53
  • There is no sence in "forcing finalization". Once JVM discovers after GC that a finalizable object is no longer reachable, it automatically pushes the object onto the finalization queue. The queue is then processed by `FinalizerThread`. – apangin Nov 05 '19 at 23:16
  • 1
    `System.runFinalization()` does not force or suggest or something... it just *waits* until the finalization queue gets empty. – apangin Nov 05 '19 at 23:22
  • 1
    This is an [xy-problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378). Just get rid of the `finalize()` method in the class `XX.WP`, that will solve your actual problem. – Holger Nov 06 '19 at 09:08
  • @apangin how did u get this conclusion? did u read JVM code or has some other infos that u can share to me?according to the API doc: Calling this method suggests that the Java Virtual Machine expend effort toward running the finalize methods of objects that have been found to be discarded but whose finalize methods have not yet been run. When control returns from the method call, the Java Virtual Machine has made a best effort to complete all outstanding finalizations. – Keyang Ma Nov 08 '19 at 16:07
  • @Holger the situation is described clearly: I'd like to verified quickly if finalize method is the culprit and if so, then I can get rid of it. so it is not so-called XY-problem. – Keyang Ma Nov 08 '19 at 16:10
  • It is, as relying on finalization is an error in itself, even without heap size problems. Besides that, the presence of `java.lang.ref.Finalizer` in your stats is already enough to indicate the finalization is going on and has an impact, as these objects are only created for instances of classes with a nontrivial finalize method. So what else do you want to verify? Just get rid of them and you have solved at least one problem. At the same time, you'll find out whether it also solves the other problem. – Holger Nov 11 '19 at 07:13

0 Answers0