String memory usage in Golang

倖福魔咒の 提交于 2019-12-05 11:39:24

unsafe.Sizeof() does not recursively go into data structures, it just reports the "shallow" size of the value passed. Quoting from its doc:

The size does not include any memory possibly referenced by x. For instance, if x is a slice, Sizeof returns the size of the slice descriptor, not the size of the memory referenced by the slice.

Maps in Go are implemented as pointers, so unsafe.Sizeof(somemap) will report the size of that pointer.

Strings in Go are just headers containing a pointer and a length. See reflect.StringHeader:

type StringHeader struct {
        Data uintptr
        Len  int
}

So unsafe.Sizeof(somestring) will report the size of the above struct, which is independent of the length of the string value (which is the value of the Len field).

To get the actual memory requirement of a map ("deeply"), see How much memory do golang maps reserve? and also How to get memory size of variable in Golang?

Go stores the UTF-8 encoded byte sequences of string values in memory. The builtin function len() reports the byte-length of a string, so basically the memory required to store a string value in memory is:

var str string = "some string"

stringSize := len(str) + unsafe.Sizeof(str)

Also don't forget that a string value may be constructed by slicing another, bigger string, and thus even if the original string is no longer referenced (and thus no longer needed), the bigger backing array will still be required to kept in memory for the smaller string slice.

For example:

s := "some loooooooong string"
s2 := s[:2]

Here, even though memory requirement for s2 would be len(s2) + unsafe.Sizeof(str) = 2 + unsafe.Sizeof(str), still, the whole backing array of s will be retained.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!