// Copyright (c) 2007 Keith D Gregory
package com.kdgregory.example.ref;

/**
 *  This class has a finalizer that takes some time to execute. The main program
 *  loop will create these objects far more quickly than they can be reclaimed,
 *  leading to an out-of-memory error.
 *  <p>
 *  Note that the objects themselves aren't very large.
 */
public class SlowFinalizer
{
    public static void main(String[] argv) throws Exception
    {
        while (true)
        {
            Object foo = new SlowFinalizer();
        }
    }

    // some member variables to take up space -- approx 200 bytes
    double a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;

    // and the finalizer, which does nothing by take time
    @Override
    protected void finalize() throws Throwable
    {
        try { Thread.sleep(500L); }
        catch (InterruptedException ignored) { /* ignored */ }
        super.finalize();
    }
}
