/
os-detect.go
68 lines (49 loc) · 1.43 KB
/
os-detect.go
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
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"os"
"runtime"
"runtime/debug"
)
func main() {
var m runtime.MemStats
/*If that is not working, or it is too much time, you can add a periodic call to
FreeOSMemory (no need to call runtime.GC() before, it is done by debug.FreeOSMemory() )
Something like this: http://play.golang.org/p/mP7_sMpX4F
package main
import (
"runtime/debug"
"time"
)
func main() {
go periodicFree(1 * time.Minute)
// Your program goes here
}
func periodicFree(d time.Duration) {
tick := time.Tick(d)
for _ = range tick {
debug.FreeOSMemory()
}
}
Take into account that every call to FreeOSMemory will take some time (not much)
and it can be partly run in parallel if GOMAXPROCS>1 since Go1.3.*/
debug.FreeOSMemory()
/*Then you can either render it to a dot file with graphical
representation of the heap or convert it to hprof format. To render it to a dot file:
$ go get github.com/randall77/hprof/dumptodot
$ dumptodot heapdump mybinary > heap.dot
and open heap.dot with Graphviz.*/
f, err := os.Create("heapdump")
if err != nil {
panic(err)
}
debug.WriteHeapDump(f.Fd())
fmt.Println(runtime.GOOS)
fmt.Println(runtime.NumCPU())
fmt.Println(runtime.NumGoroutine())
fmt.Println(runtime.GOARCH)
runtime.ReadMemStats(&m)
fmt.Println(m.TotalAlloc)
fmt.Println(m.Alloc)
fmt.Println(m.Sys)
}