// Copyright (c) Keith D Gregory, all rights reserved
package com.kdgregory.example.serialization;

import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.apache.commons.io.output.NullOutputStream;

public class SharedObjectMemoryLeakExample
{
    private static class MyImmutableObject
    implements Serializable
    {
        private static final long serialVersionUID = 1L;
        private byte[] buffer;

        public MyImmutableObject()
        {
            this.buffer = new byte[1000];   // will run out of memory sooner
        }
    }


    public static void main(String[] argv)
    throws Exception
    {
        ObjectOutputStream oos = new ObjectOutputStream(new NullOutputStream());
        for (int ii = 0 ; ii < Integer.MAX_VALUE ; ii++)
        {
            if ((ii % 1000000) == 0) System.out.println(ii);
            oos.writeObject(new MyImmutableObject());

            // uncomment following line to fix memory leak
//            oos.reset();
        }
        oos.close();
    }
}
