FAQ
//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,

--

Search Discussions

  • Minux at Oct 8, 2012 at 6:23 pm

    On Mon, Oct 8, 2012 at 6:30 PM, yang zhou wrote:

    //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,
    I bet you're using 32-bit architecture, aren't you?

    GC is ineffective on 32-bit architectures, but this will change when Go 1.1
    comes out.
    For now, the workaround is to use amd64 version of Go.

    PS:
    There are several unidiomatic usages of Go in your code.
    1. don't ignore errors
    2. use time.Second, time.MilliSecond, etc for time.Sleep.

    --
  • Stephen Day at Oct 8, 2012 at 8:12 pm
    I suspect it won't affect the result, but please try it without ignoring all the errors.

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 8, '12 at 4:44p
activeOct 8, '12 at 8:12p
posts3
users3
websitegolang.org

3 users in discussion

Minux: 1 post Stephen Day: 1 post Yang zhou: 1 post

People

Translate

site design / logo © 2022 Grokbase