String memory usage in Golang

后端 未结 1 1280
长发绾君心
长发绾君心 2020-12-19 05:10

I was optimising a code using a map[string]string where the value of the map was only either \"A\" or \"B\". So I thought Obviously a map[string]boo

相关标签:
1条回答
  • 2020-12-19 05:27

    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 Go?

    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 be 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.

    0 讨论(0)
提交回复
热议问题