0

If we use a map like map[string]string

Is it possible to get a actual memory use of map from the code below?

for i, v := range x.(map[string]string) {

  CacheBytes += len([]byte(i))
  CacheBytes += len([]byte(v))

}

When I trying to get memory bytes from variable, I did like

// get bytes from different types

        switch x.(type) {

        // string would be simple
        case string:
            s = x.(string)
            CacheBytes += len([]byte(s))

        // but in case of map...?
        case map[string]string:

            for i, v := range x.(map[string]string) {

                CacheBytes += len([]byte(i))
                CacheBytes += len([]byte(v))

            }

        // Or array...????
        case []string:

How can we get actual bytes memory that uses by map or array in Golang? I know it's bit complicated coz' of data structure, would be appreciated if anyone can help.

Thanks.

Dedenne_Cute
  • 65
  • 2
  • 6
  • 1
    https://stackoverflow.com/questions/31847549/golang-computing-the-memory-footprint-or-byte-length-of-a-map – Mark Nov 17 '17 at 02:30
  • The expression `len(s)` returns the same value as `len([]byte(s))`. The former is simpler and more efficient. Also, use `switch x := x.(type) {` to eliminate need for type assertions in the case statements. – Charlie Tumahai Nov 17 '17 at 02:45
  • default map size + (len(map) * 8) + (len(map) * 8 * sizeof(key)) + (len(map) * 8 * sizeof(val)) ??? – Dedenne_Cute Nov 17 '17 at 06:37

0 Answers0