github.com/etecs-ru/ristretto@v0.9.1/contrib/demo/node.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "runtime" 6 "unsafe" 7 8 "github.com/dustin/go-humanize" 9 "github.com/etecs-ru/ristretto/z" 10 ) 11 12 type node struct { 13 next *node 14 val int 15 } 16 17 var ( 18 nodeSz = int(unsafe.Sizeof(node{})) //nolint:unused,varcheck,gochecknoglobals,lll,deadcode,revive // adopt fork, do not touch it 19 alloc *z.Allocator //nolint:unused,varcheck,gochecknoglobals,lll,deadcode,revive // adopt fork, do not touch it 20 ) 21 22 func printNode(n *node) { 23 if n == nil { 24 return 25 } 26 if n.val%100000 == 0 { 27 fmt.Printf("node: %d\n", n.val) 28 } 29 printNode(n.next) 30 } 31 32 func main() { 33 N := 2000001 34 root := newNode(-1) 35 n := root 36 for i := 0; i < N; i++ { 37 nn := newNode(i) 38 n.next = nn 39 n = nn 40 } 41 fmt.Printf("Allocated memory: %s Objects: %d\n", 42 humanize.IBytes(uint64(z.NumAllocBytes())), N) 43 44 runtime.GC() 45 printNode(root) 46 fmt.Println("printing done") 47 48 if alloc != nil { 49 alloc.Release() 50 } else { 51 n = root 52 for n != nil { 53 left := n 54 n = n.next 55 freeNode(left) 56 } 57 } 58 fmt.Printf("After freeing. Allocated memory: %d\n", z.NumAllocBytes()) 59 60 var ms runtime.MemStats 61 runtime.ReadMemStats(&ms) 62 fmt.Printf("HeapAlloc: %s\n", humanize.IBytes(ms.HeapAlloc)) 63 }