-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
151 lines (142 loc) · 3.33 KB
/
Copy pathmain.go
File metadata and controls
151 lines (142 loc) · 3.33 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"log"
"runtime"
"time"
)
//func f() {
//// container := make([]int, 8)
//// log.Println("> loop.")
//// // slice会动态扩容,用它来做堆内存的申请
//// for i := 0; i < 32*1000*1000; i++ {
//// container = append(container, i)
//// }
//// log.Println("< loop.")
//// // container在f函数执行完毕后不再使用
////}
////func testfmt() {
//// log.Println("start.")
//// f()
////
//// log.Println("force gc.")
//// runtime.GC() // 调用强制gc函数
////
//// log.Println("done.")
//// time.Sleep(1 * time.Hour) // 保持程序不退出
////}
func traceMemStats() {
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
log.Printf("Alloc:%d(bytes) HeapIdle:%d(bytes) HeapReleased:%d(bytes)", ms.Alloc, ms.HeapIdle, ms.HeapReleased)
}
func f() {
container := make([]int, 8)
log.Println("> loop.")
for i := 0; i < 32*1000*1000; i++ {
container = append(container, i)
if i == 16*1000*1000 {
traceMemStats()
}
}
log.Println("< loop.")
}
func main() {
log.Println("start.")
traceMemStats()
f()
traceMemStats()
log.Println("force gc.")
runtime.GC()
log.Println("done.")
traceMemStats()
go func() {
for {
traceMemStats()
time.Sleep(10 * time.Second)
}
}()
time.Sleep(1 * time.Hour)
}
//
////
////import (
//// "log"
//// "net/http"
////
//// "github.com/prometheus/client_golang/prometheus"
//// "github.com/prometheus/client_golang/prometheus/promhttp"
////)
////
////var (
//// cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{
//// Name: "cpu_temperature_celsius",
//// Help: "Current temperature of the CPU.",
//// })
//// hdFailures = prometheus.NewCounterVec(
//// prometheus.CounterOpts{
//// Name: "hd_errors_total",
//// Help: "Number of hard-disk errors.",
//// },
//// []string{"device"},
//// )
////)
////
////func init() {
//// // Metrics have to be registered to be exposed:
//// prometheus.MustRegister(cpuTemp)
//// prometheus.MustRegister(hdFailures)
////}
////
////func main() {
//// cpuTemp.Set(65.3)
//// hdFailures.With(prometheus.Labels{"device": "/dev/sda"}).Inc()
////
//// // The Handler function provides a default handler to expose metrics
//// // via an HTTP server. "/metrics" is the usual endpoint for that.
//// http.Handle("/metrics", promhttp.Handler())
//// log.Fatal(http.ListenAndServe(":8080", nil))
////}
//func main() {
// //go func() {
// // http.ListenAndServe(":8088", nil)
// //}()
// //for i := 0; i < 100; i++ {
// // time.Sleep(time.Second)
// // fmt.Println(":", i)
// //}
// //ch := make(chan string)
// //<-ch
//
// var count int32 = 0
// server := &http.Server{
// Addr: ":4444",
// Handler: http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
// rw.Header().Set("Connection", "close")
// }),
// ConnContext: func(ctx context.Context, c net.Conn) context.Context {
// atomic.AddInt32(&count, 1)
// if c2 := ctx.Value("count"); c2 != nil {
// fmt.Printf("发现了遗留数据: %d\n", c2.(int32))
// }
// fmt.Printf("本次数据: %d\n", count)
// return context.WithValue(ctx, "count", count)
// },
// }
// go func() {
// panic(server.ListenAndServe())
// }()
//
// var err error
//
// fmt.Println("第一次请求")
// _, err = http.Get("http://localhost:4444/")
// if err != nil {
// panic(err)
// }
// fmt.Println("\n第二次请求")
//
// _, err = http.Get("http://localhost:4444/")
// if err != nil {
// panic(err)
// }
//}