golang precise gc

128 views
Skip to first unread message

刘桂祥

unread,
Mar 30, 2017, 10:08:25 AM3/30/17
to golang-nuts
package main

import (
"fmt"
"runtime"
"unsafe"
)

func main() {
var m runtime.MemStats

runtime.ReadMemStats(&m)
println(m.HeapObjects, m.Mallocs)

runtime.GC()

runtime.ReadMemStats(&m)
println(m.HeapObjects, m.Mallocs)

runtime.GC()

runtime.ReadMemStats(&m)
println(m.HeapObjects, m.Mallocs)

p := foo()
runtime.ReadMemStats(&m)
println(m.HeapObjects, m.Mallocs)

runtime.GC()

runtime.ReadMemStats(&m)
println(m.HeapObjects, m.Mallocs)
println(*((*int)(unsafe.Pointer(p))))

foo1()

println(*((*int)(unsafe.Pointer(p))))
}

func foo() uintptr {
i := new(int)
*i = 1000
if false {
fmt.Sprintf("%v", i)
}

return uintptr(unsafe.Pointer(i))
}
func foo1() uintptr {
i := new(int)
*i = 2000
if false {
fmt.Sprintf("%v", i)
}

return uintptr(unsafe.Pointer(i))
}



Ian Lance Taylor

unread,
Mar 30, 2017, 11:03:48 AM3/30/17
to 刘桂祥, golang-nuts
I don't understand what you are trying to say.

Note that your code uses unsafe.Pointer in an unsupported way. See
https://golang.org/pkg/unsafe/#Pointer .

Ian
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages