//tcp-server code tcp_server.go
package main
import (
"fmt"
"net"
"runtime"
)
func main() {
//tcp test
state := &runtime.MemStats{}
addr, _ := net.ResolveTCPAddr("tcp", ":20000")
l, _ := net.ListenTCP("tcp", addr)
runtime.ReadMemStats(state)
start := state.HeapAlloc
for {
// Wait for a connection.
conn, _ := l.AcceptTCP()
conn.Close()
runtime.GC()
runtime.ReadMemStats(state)
afterGc := state.HeapAlloc
if afterGc != start {
fmt.Println("now heap", start, afterGc)
start = afterGc
}
}
}
//client code test_client.go
package main
import (
"fmt"
"net/http"
"time"
)
func main() {
for {
_, err := http.Get("http://127.0.0.1:20000")
fmt.Println(err)
time.Sleep(100e6)
}
}
//run the program in two shell session
//the server end will output
// go run tcp_server.go
now heap 340384 336176
now heap 336176 336736
now heap 336736 337408
now heap 337408 408752
now heap 408752 408928
now heap 408928 409136
now heap 409136 409312
now heap 409312 409488
now heap 409488 409680
now heap 409680 409856
now heap 409856 410016
now heap 410016 410192
now heap 410192 410384
now heap 410384 410576
now heap 410576 410768
now heap 410768 410976
now heap 410976 411152
now heap 411152 411328
now heap 411328 411520
now heap 411520 411728
....
the only thing executed by the server is to accept the client and close it
immediately,
but the HeapAlloc will increase forever.... how to explain it.... will it
increase for ever. i run runtime gc each time,
--