[plt-scheme] Weak hash tables
Weak hash tables are weak in the keys, not the values (use weak-boxes
if you need the values to be held onto weakly). Probably you meant to
run this test:
(define wht (make-hash-table 'weak))
(define strong "strong")
(hash-table-put! wht strong strong)
(hash-table-put! wht "weak" "weak")
(collect-garbage)
(hash-table-for-each wht
(lambda (k v)
(display (format "key: ~a value: ~a\n" k v))))
which prints like this for me:
key: strong value: strong
even with the conservative collector.
(I'm not sure when interned symbols are collected. I think Matthew may
have posted something about that here a while back.)
Robby
At Thu, 9 Sep 2004 10:06:24 -0700, Don Felgar wrote:
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> I expected this:
>
> (define wht (make-hash-table 'weak))
> (define strong "bar")
> (hash-table-put! wht 'strong strong)
> (let ((foo "value"))
> (hash-table-put! wht 'key foo))
> (collect-garbage)
> (hash-table-for-each wht
> (lambda (k v)
> (display (format "key: ~a value: ~a\n" k v))))
>
> to print:
>
> key: strong value: bar
>
> but it prints:
>
> key: strong value: bar
> key: key value: value
>
> I presume this is an artifact of conservative gc.
>
> I wrote the above test to determine if a weak hash table shrinks
> during gc, as opposed to accumulating k/v pairs with a #f value. If
> the latter case is true I could periodically purge the hash table, but
> I guess adding my own garbage collector wouldn't be much harder.
>
> Any thoughts?
>
> --Don