OutOfMemoryErrors even after using WeakReference's for keys and values
<p>Below is a small test I've coded to educate myself on references API. I thought this would never throw OOME but it is throwing it. I am unable to figure out why. appreciate any help on this.</p>

<pre><code>public static void main(String[] args)
{
    Map&lt;WeakReference&lt;Long&gt;, WeakReference&lt;Double&gt;&gt; weak = new HashMap&lt;WeakReference&lt;Long&gt;, WeakReference&lt;Double&gt;&gt;(500000, 1.0f);

    ReferenceQueue&lt;Long&gt; keyRefQ = new ReferenceQueue&lt;Long&gt;();
    ReferenceQueue&lt;Double&gt; valueRefQ = new ReferenceQueue&lt;Double&gt;();

    int totalClearedKeys = 0;
    int totalClearedValues = 0;

    for (long putCount = 0; putCount &lt;= Long.MAX_VALUE; putCount += 100000)
    {
        weak(weak, keyRefQ, valueRefQ, 100000);

        totalClearedKeys += poll(keyRefQ);
        totalClearedValues += poll(valueRefQ);

        System.out.println("Total PUTs so far    = " + putCount);
        System.out.println("Total KEYs CLEARED so far    = " + totalClearedKeys);
        System.out.println("Total VALUESs CLEARED so far = " + totalClearedValues);
    }

}

public static void weak(Map&lt;WeakReference&lt;Long&gt;, WeakReference&lt;Double&gt;&gt; m, ReferenceQueue&lt;Long&gt; keyRefQ,
        ReferenceQueue&lt;Double&gt; valueRefQ, long limit)
{
    for (long i = 1; i &lt;= limit; i++)
    {
        m.put(new WeakReference&lt;Long&gt;(new Long(i), keyRefQ), new WeakReference&lt;Double&gt;(new Double(i), valueRefQ));
        long heapFreeSize = Runtime.getRuntime().freeMemory();
        if (i % 100000 == 0)
        {
            System.out.println(i);
            System.out.println(heapFreeSize / 131072 + "MB");
            System.out.println();
        }

    }
}

private static int poll(ReferenceQueue&lt;?&gt; keyRefQ)
{
    Reference&lt;?&gt; poll = keyRefQ.poll();
    int i = 0;
    while (poll != null)
    {
        // 
        poll.clear();
        poll = keyRefQ.poll();
        i++;
    }
    return i;

}
</code></pre>

<p>}</p>

<p>And below is the log when ran with 64MB of heap</p>

<pre><code>Total PUTs so far    = 0
Total KEYs CLEARED so far    = 77982
Total VALUESs CLEARED so far = 77980
100000
24MB

Total PUTs so far    = 100000
Total KEYs CLEARED so far    = 134616
Total VALUESs CLEARED so far = 134614
100000
53MB

Total PUTs so far    = 200000
Total KEYs CLEARED so far    = 221489
Total VALUESs CLEARED so far = 221488
100000
157MB

Total PUTs so far    = 300000
Total KEYs CLEARED so far    = 366966
Total VALUESs CLEARED so far = 366966
100000
77MB

Total PUTs so far    = 400000
Total KEYs CLEARED so far    = 366968
Total VALUESs CLEARED so far = 366967
100000
129MB

Total PUTs so far    = 500000
Total KEYs CLEARED so far    = 533883
Total VALUESs CLEARED so far = 533881
100000
50MB

Total PUTs so far    = 600000
Total KEYs CLEARED so far    = 533886
Total VALUESs CLEARED so far = 533883
100000
6MB

Total PUTs so far    = 700000
Total KEYs CLEARED so far    = 775763
Total VALUESs CLEARED so far = 775762
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at Referencestest.weak(Referencestest.java:38)
    at Referencestest.main(Referencestest.java:21)
</code></pre>
