For this version of your program with

    type S struct{ B byte }

There is an allocation,

// example.go
package main

import (
    "runtime"
    "unsafe"
)

type S struct{ B byte }

func main() {
    var stats runtime.MemStats

    runtime.ReadMemStats(&stats)
    println(stats.Mallocs)
    var x S
    runtime.ReadMemStats(&stats)
    println(stats.Mallocs)
    _ = *ref(x)
    runtime.ReadMemStats(&stats)
    println(stats.Mallocs)

    println(unsafe.Sizeof(x))
}

func ref(z S) *S {
    return &z
}

Output:

$ go version
go version devel +bc29313 Fri Apr 14 16:47:25 2017 +0000 linux/amd64
$ go run -gcflags '-N -l -m' example.go
# command-line-arguments
./example.go:27:9: &z escapes to heap
./example.go:26:16: moved to heap: z
./example.go:14:23: main &stats does not escape
./example.go:17:23: main &stats does not escape
./example.go:20:23: main &stats does not escape
83
83
84
1
$

Peter

On Saturday, April 15, 2017 at 12:14:48 PM UTC-4, peterGo wrote:
>
> 刘桂祥,
>
> Size and alignment guarantees
> https://golang.org/ref/spec#Size_and_alignment_guarantees
>
> A struct or array type has size zero if it contains no fields (or 
> elements, respectively) that have a size greater than zero. Two distinct 
> zero-size variables may have the same address in memory.
>
> You have
>
>     type S struct{}
>
> Why do you expect runtime.MemStats to show an allocation for zero bytes?
>
> Peter
>
> On Saturday, April 15, 2017 at 10:12:34 AM UTC-4, 刘桂祥 wrote:
>>
>>
>> go build -gcflags '-N -l' example.go && ./example
>> If I use this to compile example.go it shound disable function inline and 
>> the compile code should call ref() in example.go and will call mallocgc 
>> runtimestats.Mallocs can see it  but it is not.
>>
>> 在 2017年4月14日星期五 UTC+8下午10:01:25,Ian Lance Taylor写道:
>>>
>>> On Thu, Apr 13, 2017 at 10:45 PM, 刘桂祥 <liuguix...@gmail.com> wrote: 
>>> > go1.7.1 
>>> > 
>>> > go run -gcflags '-N -l -m' example.go 
>>>
>>> It is the -m flag that is generating the output you see. 
>>>
>>> If there is some other bug, please let us know what it is. 
>>>
>>> Ian 
>>>
>>> > 在 2017年4月14日星期五 UTC+8下午1:18:26,Ian Lance Taylor写道: 
>>> >> 
>>> >> On Thu, Apr 13, 2017 at 8:33 PM, 刘桂祥 <liuguix...@gmail.com> wrote: 
>>> >> > // example.go 
>>> >> > package main 
>>> >> > 
>>> >> > 
>>> >> > import "runtime" 
>>> >> > 
>>> >> > 
>>> >> > type S struct{} 
>>> >> > 
>>> >> > 
>>> >> > func main() { 
>>> >> >  var stats runtime.MemStats 
>>> >> > 
>>> >> > 
>>> >> >  runtime.ReadMemStats(&stats) 
>>> >> >  println(stats.Mallocs) 
>>> >> >  var x S 
>>> >> >  runtime.ReadMemStats(&stats) 
>>> >> >  println(stats.Mallocs) 
>>> >> >  _ = *ref(x) 
>>> >> >  runtime.ReadMemStats(&stats) 
>>> >> >  println(stats.Mallocs) 
>>> >> > } 
>>> >> > 
>>> >> > 
>>> >> > func ref(z S) *S { 
>>> >> >  return &z 
>>> >> > } 
>>> >> > 
>>> >> > go build -gcflags '-N -l' example.go && ./example 
>>> >> > or 
>>> >> > go run -gcflags '-N -l' example.go 
>>> >> > 
>>> >> > results: 
>>> >> > ./example.go:21: &z escapes to heap 
>>> >> > ./example.go:20: moved to heap: z 
>>> >> > ./example.go:10: main &stats does not escape 
>>> >> > ./example.go:13: main &stats does not escape 
>>> >> > ./example.go:16: main &stats does not escape 
>>> >> > 61 
>>> >> > 61 
>>> >> > 61 
>>> >> 
>>> >> That looks like the output from -gcflags="-m -l".  Are you really 
>>> sure 
>>> >> you typed -N?  What version of Go are you using? 
>>> >> 
>>> >> 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. 
>>>
>>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to