TestGc finalize()

package com.gc;


public class TestGc {
  
  
  public static void main(String[] args) {
    TestGc testGc =new TestGc();
   
    Man man = new Man("a");
    Person a = new Person();
    a.setMan(man);
    a.setMan(null);
    man = null;
    
    System.out.println("before gc");
    System.gc();
    System.out.println("right after gc");
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    System.out.println("after gc");
    System.out.println(a.getMan() == null? null :a.getMan().getName());
  }
  
}
package com.gc;

public class Person {
  private Man man;

  public Man getMan() {
    return man;
  }
  public void setMan(Man man) {
    this.man = man;
  }
}
package com.gc;

public class Man {

  private String name;
  public Man(String name) {
    
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  
  @Override
  protected void finalize() throws Throwable {
    System.out.println("execute method finalize()"+ name);
    super.finalize();
  }
}

 

上一篇:CSS 溢出overflow属性的使用


下一篇:[转载] java中final,finally,finalize三者的作用和区别