1 ! Copyright (C) 2004, 2006 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: arrays errors generic hashtables io kernel
5 kernel-internals math namespaces parser prettyprint sequences
6 strings styles vectors words ;
8 : full-gc ( -- ) generations 1- data-gc ;
10 ! Printing an overview of heap usage.
12 : total/used/free, ( free total str -- )
16 over - number>string ,
21 [ , number>string , "" , "" , ] { } make , ;
23 : simple-table ( table -- )
24 H{ { table-gap { 10 0 } } }
25 [ dup string? [ write ] [ pprint ] if ]
30 { "" "Total" "Used" "Free" } ,
32 "Generation " pick number>string append
33 >r first2 r> total/used/free, 1+
37 code-room "Code space" total/used/free,
38 ] { } make simple-table ;
40 ! Some words for iterating through the heap.
42 : (each-object) ( quot -- )
44 [ swap [ call ] keep (each-object) ] [ 2drop ] if ; inline
46 : each-object ( quot -- )
47 [ begin-scan [ (each-object) ] keep ]
48 [ end-scan ] cleanup drop ; inline
50 : (instances) ( obj quot seq -- )
51 >r over >r call [ r> r> push ] [ r> r> 2drop ] if ; inline
53 : instances ( quot -- seq )
55 -rot [ (instances) ] 2keep
56 ] each-object nip ; inline
58 : heap-stat-step ( counts sizes obj -- )
59 [ dup size swap class rot hash+ ] keep
60 1 swap class rot hash+ ;
62 : heap-stats ( -- counts sizes )
63 #! Return a list of instance count/total size pairs.
65 [ >r 2dup r> heap-stat-step ] each-object ;
68 heap-stats dup hash-keys natural-sort [
69 { "Class" "Bytes" "Instances" } ,
71 [ dup , dup pick hash , pick hash , ] { } make ,
73 ] { } make simple-table ;