Skip to content

Instantly share code, notes, and snippets.

@janisz
Last active June 6, 2016 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janisz/d04301463dd663584a2498f4ec086c1c to your computer and use it in GitHub Desktop.
Save janisz/d04301463dd663584a2498f4ec086c1c to your computer and use it in GitHub Desktop.
BigCache leak
package main
import (
"fmt"
"runtime"
. "github.com/allegro/bigcache"
"github.com/pkg/profile"
)
const (
entries = 200000
valueSize = 100
repeats = 10
)
func main() {
defer profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
fmt.Printf("Number of entries: ", entries)
printAllocs()
config := Config{
Shards: 256,
LifeWindow: 0,
MaxEntriesInWindow: entries,
MaxEntrySize: 2 * valueSize,
Verbose: true,
HardMaxCacheSize: 1,
}
bigcache, err := NewBigCache(config)
if err != nil {
panic(err)
}
for i := 0; i < repeats; i++ {
printAllocs()
for i := 0; i < entries; i++ {
key, val := generateKeyValue(i, valueSize)
err := bigcache.Set(key, val)
if err != nil {
panic(err)
}
}
}
}
func generateKeyValue(index int, valSize int) (string, []byte) {
key := fmt.Sprintf("key-%010d", index)
fixedNumber := []byte(fmt.Sprintf("%010d", index))
val := append(make([]byte, valSize - 10), fixedNumber...)
return key, val
}
func printAllocs() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Printf("Alloc: %6d MB \n", m.Alloc / 1e6)
}
GODEBUG="gctrace=1" go run caches_mem_overhead.go
gc 1 @0.024s 1%: 0.14+2.2+0.067 ms clock, 0.42+0.014/0.65/2.5+0.20 ms cpu, 4->4->0 MB, 5 MB goal, 4 P
gc 2 @0.044s 1%: 0.010+0.64+0.29 ms clock, 0.041+0.006/0.59/1.3+1.1 ms cpu, 4->4->0 MB, 5 MB goal, 4 P
# github.com/allegro/bigcache
gc 1 @0.015s 9%: 0.093+4.1+0.36 ms clock, 0.18+3.5/3.7/5.5+0.73 ms cpu, 4->4->2 MB, 5 MB goal, 4 P
gc 2 @0.027s 9%: 0.011+2.1+0.19 ms clock, 0.045+0/2.1/3.9+0.78 ms cpu, 4->4->2 MB, 5 MB goal, 4 P
gc 3 @0.039s 10%: 0.010+2.8+0.37 ms clock, 0.043+2.1/2.6/2.6+1.4 ms cpu, 4->4->3 MB, 5 MB goal, 4 P
gc 4 @0.051s 11%: 0.010+3.0+0.43 ms clock, 0.043+2.3/2.8/5.0+1.7 ms cpu, 5->6->4 MB, 6 MB goal, 4 P
gc 5 @0.068s 11%: 0.009+3.7+0.37 ms clock, 0.038+2.8/3.4/6.0+1.5 ms cpu, 7->7->5 MB, 8 MB goal, 4 P
# command-line-arguments
gc 1 @0.013s 7%: 0.15+2.3+0.35 ms clock, 0.31+1.5/2.2/2.7+0.70 ms cpu, 4->4->2 MB, 5 MB goal, 4 P
# command-line-arguments
gc 1 @0.019s 4%: 0.18+1.8+0.20 ms clock, 0.37+1.3/1.4/3.1+0.40 ms cpu, 4->4->2 MB, 5 MB goal, 4 P
gc 2 @0.029s 5%: 0.010+15+0.23 ms clock, 0.042+2.9/2.1/4.2+0.93 ms cpu, 5->6->5 MB, 6 MB goal, 4 P
gc 3 @0.053s 7%: 0.013+7.3+0.062 ms clock, 0.055+3.5/5.0/10+0.24 ms cpu, 7->7->6 MB, 9 MB goal, 4 P
gc 4 @0.085s 9%: 0.011+11+0.31 ms clock, 0.045+10/7.3/14+1.2 ms cpu, 12->12->10 MB, 13 MB goal, 4 P
gc 5 @0.135s 10%: 0.013+14+0.22 ms clock, 0.054+9.7/14/17+0.88 ms cpu, 20->20->18 MB, 21 MB goal, 4 P
gc 6 @0.222s 10%: 0.011+24+0.23 ms clock, 0.044+19/16/31+0.95 ms cpu, 34->34->28 MB, 36 MB goal, 4 P
2016/06/06 17:11:33 profile: memory profiling enabled (rate 4096), mem.pprof
Number of entries: %!(EXTRA int=200000)Alloc: 0 MB
gc 1 @0.004s 4%: 0.058+0.30+0.21 ms clock, 0.17+0.006/0.13/0.23+0.64 ms cpu, 4->5->5 MB, 5 MB goal, 4 P
gc 2 @0.005s 6%: 0.006+0.16+0.22 ms clock, 0.020+0/0.093/0.14+0.68 ms cpu, 6->7->6 MB, 8 MB goal, 4 P
gc 3 @0.006s 9%: 0.004+0.34+0.21 ms clock, 0.019+0/0.10/0.15+0.84 ms cpu, 13->13->13 MB, 14 MB goal, 4 P
gc 4 @0.008s 11%: 0.008+0.64+0.22 ms clock, 0.034+0/0.38/0+0.91 ms cpu, 26->26->26 MB, 27 MB goal, 4 P
Alloc: 46 MB
gc 5 @0.056s 2%: 0.010+0.23+0.20 ms clock, 0.043+0/0.12/0.32+0.82 ms cpu, 50->50->44 MB, 52 MB goal, 4 P
gc 6 @0.307s 0%: 0.010+0.44+0.075 ms clock, 0.043+0.18/0.35/0.52+0.30 ms cpu, 86->86->44 MB, 88 MB goal, 4 P
Alloc: 64 MB
gc 7 @0.559s 0%: 0.011+0.54+0.19 ms clock, 0.045+0.19/0.40/0.98+0.78 ms cpu, 86->86->44 MB, 88 MB goal, 4 P
Alloc: 87 MB
gc 8 @0.836s 0%: 0.010+0.59+0.051 ms clock, 0.042+0.23/0.58/0.82+0.20 ms cpu, 87->87->44 MB, 89 MB goal, 4 P
gc 9 @1.099s 0%: 0.012+1.1+0.21 ms clock, 0.050+0.27/0.50/0.67+0.86 ms cpu, 87->87->44 MB, 89 MB goal, 4 P
Alloc: 67 MB
gc 10 @1.393s 0%: 0.011+0.62+0.19 ms clock, 0.044+0.046/0.53/1.1+0.77 ms cpu, 87->87->44 MB, 89 MB goal, 4 P
gc 11 @1.661s 0%: 0.012+0.74+0.23 ms clock, 0.049+0/0.49/1.0+0.94 ms cpu, 87->87->44 MB, 89 MB goal, 4 P
Alloc: 62 MB
gc 12 @1.925s 0%: 0.011+0.57+0.052 ms clock, 0.044+0/0.53/1.0+0.20 ms cpu, 87->87->44 MB, 89 MB goal, 4 P
Alloc: 71 MB
gc 13 @2.200s 0%: 0.010+0.59+0.21 ms clock, 0.042+0.33/0.58/0.80+0.84 ms cpu, 87->87->44 MB, 89 MB goal, 4 P
gc 14 @2.469s 0%: 0.011+0.70+0.21 ms clock, 0.047+0/0.64/1.1+0.85 ms cpu, 87->87->44 MB, 89 MB goal, 4 P
Alloc: 53 MB
gc 15 @2.738s 0%: 0.011+2.1+0.092 ms clock, 0.044+0.074/0.64/0.79+0.37 ms cpu, 87->87->44 MB, 89 MB goal, 4 P
Alloc: 76 MB
gc 16 @3.012s 0%: 0.010+0.69+0.20 ms clock, 0.043+0.015/0.64/1.1+0.81 ms cpu, 87->87->44 MB, 89 MB goal, 4 P
gc 17 @3.288s 0%: 0.010+0.82+0.13 ms clock, 0.043+0.34/0.45/1.1+0.55 ms cpu, 87->87->44 MB, 89 MB goal, 4 P
Alloc: 56 MB
gc 18 @3.553s 0%: 0.009+0.70+0.21 ms clock, 0.039+0.37/0.50/0.69+0.85 ms cpu, 87->87->44 MB, 89 MB goal, 4 P
Alloc: 80 MB
gc 19 @3.828s 0%: 0.010+3.6+0.070 ms clock, 0.042+0/0.62/0.81+0.28 ms cpu, 87->87->45 MB, 89 MB goal, 4 P
gc 20 @4.102s 0%: 0.011+2.1+0.21 ms clock, 0.046+0.014/0.70/0.81+0.86 ms cpu, 87->87->44 MB, 89 MB goal, 4 P
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment