dongqi7631 2019-08-03 08:12
浏览 194

Go vs Rust垃圾收集器的性能和类型[关闭]

I read in some stack-overflow about Go's GC.

Stack and heap models in Go Garbage collector I want to know that if a variable needs to be defined in heap or stack in Go and then if GC want to collect it, what algorithm is used for this?

If we assume that in languages that have GC that heap is more efficient, but what about Rust and how does rust handle this in compare to Go?

Especially about reference counter in Go that we have no choice in most of times to ask compiler but such a tool exist and it does its job on its own way!

I read this : Stack vs heap allocation of structs in Go, and how they relate to garbage collection

  • 写回答

1条回答 默认 最新

  • duanlvji4780 2019-08-03 08:30
    关注

    This question is based on wrong assumption that Rust has managed memory. It does not. Besides an allocator, and tools in std that you can use, compiler does not do anything without being asked to.

    Rust uses RAII (Resource acquisition is initialization) and unless you specifically create std::rc::Rc<T>, for example

    fn main() {
        let my_rc = std::rc::Rc::new(5);
        let my_rc_cloned = my_rc.clone();
    
        println!("original: {}, cloned: {}", my_rc, my_rc_cloned);
    } // both Rc's are dropped, refcount is 0, and 5 is dropped too
    

    There's no reference counting involved. Ever. Values are pushed and popped off current thread stack like in any low level programming language, such as C.

    let rc = Rc::new(value: T);, creates a new struct, on the stack, C++ uses RAII too, and std::shared_ptr<int> rc(new int(5)); is how you create reference counting pointer in it.

    Both rc's are on the stack and are dropped/destructed once particular block is popped off the stack. That's how RAII works, without going into any minor details.

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)