gpt4 book ai didi

go - 是否可以在 go 中手动释放内存?

转载 作者:IT王子 更新时间:2023-10-29 00:49:30 24 4
gpt4 key购买 nike

在与大学讨论后,我想知道是否有可能(即使完全没有任何意义)在 go 中手动释放内存(即通过使用不安全包)。是吗?

最佳答案

这是您可能感兴趣的话题:Add runtime.Free() for GOGC=off

有趣的部分:

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:是的,但不要这样做

关于go - 是否可以在 go 中手动释放内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37831477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com