If the capacity of a slice has been modified by the unsafe.Pointer, can the rest memory reused by the garbage collection?

huangapple go评论81阅读模式
英文:

If the capacity of a slice has been modified by the unsafe.Pointer, can the rest memory reused by the garbage collection?

问题

请问您需要将以下内容翻译为中文吗?

"Think about this case:

s := make([]byte, 512, 1024)
(*reflect.SliceHeader)((unsafe.Pointer(&s))).Cap = 512

are the last 512 bytes memory can be collected by the GC?

Whether it is or not, Why?"

英文:

Think about this case:

s := make([]byte, 512, 1024)
(*reflect.SliceHeader)((unsafe.Pointer(&s))).Cap = 512

are the last 512 bytes memory can be collected by the GC?

Whether it is or not, Why?

答案1

得分: 1

据我所知,当前的垃圾收集器不会收集部分切片或字符串。对于以下情况也是如此:

s=s[:512:512] // 类似于你的示例,但在Go1.3中是惯用写法
s=s[128:] // 前128个元素不会被收集。
英文:

As far as I know, the current garbage collector will not collect partial slices or strings. The same is true for:

s=s[:512:512] // Like your example but idiomatically starting in Go1.3
s=s[128:] // first 128 elements are not collected.

huangapple
  • 本文由 发表于 2014年12月16日 17:42:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/27501614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定