2

Is it compulsory that finalize()method will be called every time we call System.gc() explicitly in Java?

1. How to Handle Java Finalization's Memory-Retention Issues

I want to understand above link

2. I understand that finalize() will not be called every time we call System.gc() explicitly, But Now the question is why it happens that sometimes it get called and sometime not? I think I have made my question more valuable now.

  • 3
    Finalization is not guaranteed to be called on garbage collection. – M. le Rutte Apr 29 '18 at 10:36
  • `System.gc()` does not guarantee thats GC will start collecting garbage objects . Hence answer is NO. You can also checkout [This discussion](https://stackoverflow.com/questions/2506488/when-is-the-finalize-method-called-in-java). – ADM Apr 29 '18 at 10:45
  • Tried that thing called "prior research"? – GhostCat Apr 29 '18 at 10:49
  • I have closed the question as a duplicate and added some useful links about `System.gc()` and `finalize()` for you. If you feel like you asked not a duplicate, please add an explanation what exactly you are trying to understand. Then we will re-open the question. – Andrew Tobilko Apr 29 '18 at 10:51
  • @Andrew : I want to understand Finalization's Memory-Retention Issues – manoj pandey Apr 29 '18 at 11:00
  • @manojpandey, thank you for clarifying that. Advice: don't hurry with accepting an answer. It seems the answer you have accepted doesn't explain anything you want to know. – Andrew Tobilko Apr 29 '18 at 11:19
  • @Andrew : The answer that i accepted has cleared the first point otherwise i would not have accepted that answer. (unmarked the answer) – manoj pandey Apr 29 '18 at 11:24

3 Answers3

1

No. It isn't guaranteed that GC will ever occur, let alone in response to System.gc(), let alone that finalizers will ever be run in consequence.

now the question is why it happens that sometimes it get called and sometime not

Because GC and finalization may run as a result of System.gc(): nothing to stop it, so it happens, and if not, not, because there is no guarantee.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

No.

System.gc

Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse.

finalize

The finalize method is never invoked more than once by a Java virtual machine for any given object.

xingbin
  • 27,410
  • 9
  • 53
  • 103
1

In this example finalize method() may be called or may not be called. Its totally depends on the JVM that when finalize method will be called. If you run this program sometimes you will get the output sometimes not.. (-,-) So try your luck

class Person{  
public int a;
public void finalize(){
    //System.out.println("finalize called"+this.hashCode());
    System.out.println("finalize called"+this.a);
}  
public static void main(String[] args){  
    Person f1=new Person();  
    f1.a=10;
    Person f2=new Person();  
    f1=null;  
    f2=null;  
    System.gc();  
}}
Onic Team
  • 1,620
  • 5
  • 26
  • 37