OOP-07.ObjectLifecycle
class Garbage { static boolean gcrun = false; static int created = 0; static int finalized = 0; public Garbage() { created++; } public void finalize() { if (!gcrun) { gcrun = true; System.out.println(GC after +created+ garbage); } finalized++; } } public class TestGarbage { public static void main(String[] args) { while (!Garbage.gcrun) { new Garbage(); new String(To take up space ...); } System.out.println(Created = + Garbage.created); System.out.println(Finalized = + Garbage.finalized); } } java Test GC after 5425 garbage Created = 5426 Finalized = 542
原创力文档

文档评论(0)