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