-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmemory.go
More file actions
44 lines (32 loc) · 712 Bytes
/
memory.go
File metadata and controls
44 lines (32 loc) · 712 Bytes
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
package leak
import (
"runtime"
)
func MemoryLeaks(f func()) int {
var then = new(runtime.MemStats)
var now = new(runtime.MemStats)
runtime.GC()
runtime.ReadMemStats(then)
f()
runtime.GC()
runtime.ReadMemStats(now)
return int((now.Mallocs - then.Mallocs) - (now.Frees - then.Frees))
}
type MemoryMark struct {
then *runtime.MemStats
now *runtime.MemStats
}
func MarkMemory() *MemoryMark {
m := &MemoryMark{
then: new(runtime.MemStats),
now: new(runtime.MemStats),
}
runtime.GC()
runtime.ReadMemStats(m.then)
return m
}
func (m *MemoryMark) Release() int {
runtime.GC()
runtime.ReadMemStats(m.now)
return int((m.now.Mallocs - m.then.Mallocs) - (m.now.Frees - m.then.Frees))
}