dongzhuo1880 2016-01-12 17:58
浏览 20

替换结构的指针时,GC的行为如何

I'm learning Go and I'm reading examples from libraries. I found that some examples are using:

type MyType struct {
  Code string
  //...
}


func main() {
  myType := &MyType{...}
  //...
  myType = &MyType{...}
}

Basically they are reusing variables. I understand that &MyType{..} returns a pointer, later I can replace that pointer. What happens with the previous pointed memory. Will the GC reclaim that memory or will I waste that memory. Maybe this is a silly question and I'm concerned for nothing but I'm trying to learn Go to build performance APIs :)

  • 写回答

1条回答 默认 最新

  • dongyu8664 2016-01-13 13:41
    关注

    The memory will be reclaimed by the garbage collector.

    If you want to replace the struct you can do it this way:

    func main() {
        myType := &MyType{...}
        //...
        *myType = MyType{...}
    }
    

    The difference will probably be negligible though.

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂