[plt-scheme] Weak hash tables

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Thu Sep 9 13:24:48 EDT 2004

At Thu, 9 Sep 2004 10:06:24 -0700, Don Felgar wrote:
> I presume this is an artifact of conservative gc.

More or less. Try

 (define wht (make-hash-table 'weak))
 (define strong "bar")
 (hash-table-put! wht 'strong strong)
 (let loop ()
  (hash-table-put! wht (string->symbol (number->string (random 5000000)))
  (printf "~a~n" (hash-table-count wht))

> 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.

It shrinks.

(Actually, internally, it merely avoids growing. If you put 5000 things
into a weak hash table and they all go away, then you're left with 5000
buckets internally, while `hash-table-count' claims 0. If you put 200
things back in, 200 buckets get recycled, but you're still stuck with
4800 empty buckets.)


Posted on the users mailing list.