垃圾回收演示图

 
 
plan9汇编
iface定义
type iface struct { // 16 bytes on a 64bit arch tab *itab data unsafe.Pointer }
itab定义
type itab struct { inter *interfacetype _type *_type hash uint32 // copy of _type.hash. Used for type switches. _ [4]byte fun [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter. }
_type字段描述了 interface 所持有的值的类型,data 指针所指向的值的类型。
inter字段描述了 interface 本身的类型。
 
由于 interface 只能持有指针,任何用 interface 包装的具体类型,都会被取其地址。 这样多半会导致一次堆上的内存分配,编译器会保守地让 receiver 逃逸。 即使是标量类型,也不例外!
编译器会为值-receiver的方法提供对应的指针-receiver包装方法,因为interface的虚表里存储的是指针,后续通过interface虚表调用方法时,会用到这些包装方法。
eface是表示 runtime 中空接口的根类型
type eface struct { // 16 bytes on a 64bit arch _type *_type data unsafe.Pointer }
_type 持有 data 指针指向的数据类型的信息。
 
badge