Golang 新的内存分配
Golang new memory allocation
我已经开始用 Go 编程,我想知道什么时候使用 new(Object)
它会根据该对象的大小分配内存,对吗?如果是这种情况,我如何在使用完该对象后释放该内存?
我问这个是因为在 C++ 中,当对对象使用 new
时,您可以 delete
对象,一旦不再需要存储该对象。
我一直在寻找 Go 是否有 delete
或类似于 C++ 的东西,但我一直找不到任何东西。
非常感谢任何帮助。
Go 有垃圾收集功能。这意味着 Go 运行时会在后台检查对象或任何其他变量是否不再使用,如果是这种情况,则会释放内存。
另请参阅 Go 常见问题解答:Why is the syntax so different from C? - Why do garbage collection? Won't it be too expensive?
如你所见here:
Go is fully garbage-collected and provides fundamental support for concurrent execution and communication.
所以你不必关心内存分配。
在 Go 中,与 C 和 C++ 不同,但与 Java 一样,内存由 garbage collector 自动管理。
没有delete
电话。
题外话:
in C++ when new
is used on an object you can delete
the object once there is no longer any need for the object to be stored.
你必须删除,否则内存泄漏。
我已经开始用 Go 编程,我想知道什么时候使用 new(Object)
它会根据该对象的大小分配内存,对吗?如果是这种情况,我如何在使用完该对象后释放该内存?
我问这个是因为在 C++ 中,当对对象使用 new
时,您可以 delete
对象,一旦不再需要存储该对象。
我一直在寻找 Go 是否有 delete
或类似于 C++ 的东西,但我一直找不到任何东西。
非常感谢任何帮助。
Go 有垃圾收集功能。这意味着 Go 运行时会在后台检查对象或任何其他变量是否不再使用,如果是这种情况,则会释放内存。
另请参阅 Go 常见问题解答:Why is the syntax so different from C? - Why do garbage collection? Won't it be too expensive?
如你所见here:
Go is fully garbage-collected and provides fundamental support for concurrent execution and communication.
所以你不必关心内存分配。
在 Go 中,与 C 和 C++ 不同,但与 Java 一样,内存由 garbage collector 自动管理。
没有delete
电话。
题外话:
in C++ when
new
is used on an object you candelete
the object once there is no longer any need for the object to be stored.
你必须删除,否则内存泄漏。