doutan2456 2016-06-15 09:30
浏览 999
已采纳

可以在go中手动释放内存吗?

After discussion with college, I wonder if it would be possible (even if completely does not make any sense) to deallocate memory manually in go (ie. by using unsafe package). Is it?

  • 写回答

1条回答 默认 最新

  • donjd86266 2016-06-15 10:00
    关注

    Here is a thread that may interest you: Add runtime.Free() for GOGC=off

    Interesting part:

    The Go GC does not have the ability to manually deallocate blocks anymore. And besides, runtime. Free is unsafe (people might free still in use pointers or double free) and then all sorts of C memory problem that Go tries hard to get rid of will come back. The other reason is that runtime sometimes allocates behind your back and there is no way for the program to explicitly free memory.

    If you really want to manually manage memory with Go, implement your own memory allocator based on syscall.Mmap or cgo malloc/free.

    Disabling GC for extended period of time is generally a bad solution for a concurrent language like Go. And Go's GC will only be better down the road.

    TL;DR: Yes, but don't do it

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页
  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信