~ chicken-core (chicken-5) /tests/symbolgc-tests.scm
Trap1;;;; symbolgc-tests.scm23(import (chicken gc) (chicken format) (chicken keyword))45;; Ensure counts are defined before creating the disposable symbols.6;; This way, this program can also be run in interpreted mode.7(define *count-before* #f)8(define *count-after* #f)910;; Force major GC to ensure there are no collectible symbols left11;; before we start, otherwise the GC might clean these up and we'd end12;; up with less symbols than we started with!13(gc #t)1415(set! *count-before* (vector-ref (##sys#symbol-table-info) 2))1617(print "starting with " *count-before* " symbols")1819(print "interning 10000 symbols ...")2021(do ((i 10000 (sub1 i)))22 ((zero? i))23 (string->symbol (sprintf "%%%~a%%%" i)))2425(print "recovering ...")2627;; Force major GC, which should reclaim every last symbol we just28;; created, as well as "i", the loop counter.29(gc #t)3031;; Don't use LET, which would introduce a fresh identifier, which is a32;; new symbol (at least, in interpreted mode)33(set! *count-after* (vector-ref (##sys#symbol-table-info) 2))34(print (- *count-after* *count-before*) " newly interned symbols left")35(unless (= *count-after* *count-before*)36 (error "unable to reclaim all symbols"))3738(print "interning 10000 keywords ...")3940(do ((i 10000 (sub1 i)))41 ((zero? i))42 (string->keyword (sprintf "kw-%%%~a%%%" i)))4344(print "recovering ...")45(gc #t)46(set! *count-after* (vector-ref (##sys#symbol-table-info) 2))47(print* (- *count-after* *count-before*) " newly interned leywords left")48(unless (= *count-after* *count-before*)49 (error "unable to reclaim all keywords"))5051(print "\ndone.")