gc debug problem

72 views
Skip to first unread message

sandao

unread,
Aug 14, 2019, 11:56:43 AM8/14/19
to golang-nuts
type A struct {
        a int
}

func gen() []*A {
        r := make([]*A, 1000000)
        for i := 0; i < len(r); i++ {
                r[i] = &A{i}
        }
        return r
}

//var as []*A = gen()

func main() {
        go func() {
                err := http.ListenAndServe(":8034", nil)
                if err != nil {
                        //fmt.Printf("ListenAndServe:%s\n", err)
                }
        }()
        var as []*A = gen()
        fmt.Println(as[0])
        time.Sleep(100 * time.Second)
}

when debug gc:
1)    if the "var as []*A = gen()"  is in main , the debug info is :  gc 4 @2.757s 0%: 0.049+0.59+0.025 ms clock, 2.3+0/0.54/0.28+1.2 ms cpu, 16->16->0 MB, 31 MB goal, 48 P (forced)
2)    if the "var as []*A = gen()"  is not in main, the debug info is: gc 9 @7.172s 0%: 0.077+25+0.020 ms clock, 3.7+0/24/0.73+0.99 ms cpu, 16->16->15 MB, 31 MB goal, 48 P (forced)

Anybody konw why the mark time (red numbers) is so different?    go version is 1.12.7.

fge...@gmail.com

unread,
Aug 14, 2019, 11:02:46 PM8/14/19
to sandao, golang-nuts
when as is allocated on the stack, gc has a smaller task.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/e3a947ed-1a42-4d17-991d-d29cc80685e0%40googlegroups.com.

sandao

unread,
Aug 14, 2019, 11:19:29 PM8/14/19
to golang-nuts
In my opinion, the slice which "as"  point to, is all allocated on the heap in two case . so, I think the mark time should not be so different. Can you tell me more details for you answer(when as is allocated on the stack, gc has a smaller task.)?

在 2019年8月15日星期四 UTC+8上午11:02:46,Gergely Födémesi写道:
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.

fge...@gmail.com

unread,
Aug 15, 2019, 12:06:20 AM8/15/19
to sandao, golang-nuts
see for yourself what the compiler says about that decision:
you'll need -m 

 you can also use pprof to check your assumptions.

(there isn't any computer near me currently)

sandao

unread,
Aug 15, 2019, 12:54:25 AM8/15/19
to golang-nuts
escapes to heap,use "go tool compile -m".

在 2019年8月15日星期四 UTC+8下午12:06:20,Gergely Födémesi写道:
Reply all
Reply to author
Forward
0 new messages