Hacker News new | ask | show | jobs
by fctorial 1983 days ago
> You can still have memory leaks in GC-d languages by reference cycles.

    public class T {
      Object ref;

      @Override
      protected void finalize() {
        System.out.println("finalize");
      }

      static void leak() {
        T a = new T(), b = new T();
        a.ref = b;
        b.ref = a;
      }

      public static void main(String[] as) throws Throwable {
        leak();
        System.gc();
        Thread.sleep(10000);
      }
    }
1 comments

A full GC will count the a and b objects as garbage once leak returns.
Yes it will. The snippet counters the statement.