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(); } }