Golang memory usage doesnt match with runtime.MemStats

Hi guys,

I’m running a go program which loads the data from mysql into a map for long living cache purpose. Data will be queried from db in a loop and update the struct querying few records each time. The amount of data loaded into the struct will be around 2gb when written to disk in json format.

After the loading is complete and gc is triggered, I see the following memory stats using runtime.readMemStats()

"Alloc": 11107201856,
  "TotalAlloc": 438869045528,
  "Sys": 54348311616,
  "Lookups": 0,
  "Mallocs": 6589668524,
  "Frees": 6448030009,
  "HeapAlloc": 11107201856,
  "HeapSys": 51584040960,
  "HeapIdle": 38203949056,
  "HeapInuse": 13380091904,
  "HeapReleased": 37270716416,
  "HeapObjects": 141638515,
  "StackInuse": 1703936,
  "StackSys": 1703936,
  "MSpanInuse": 224454240,
  "MSpanSys": 658674288,
  "MCacheInuse": 34800,
  "MCacheSys": 46800,
  "BuckHashSys": 2067482,
  "GCSys": 2037865776,
  "OtherSys": 63912374,
  "NextGC": 22214904160,
  "LastGC": 1688029114272696800,
  "PauseTotalNs": 20106946,

and the memory consumed by pod

$k top po  test-pod --containers=true
POD         NAME            CPU(cores)   MEMORY(bytes)   
test-pod    app-container   1m           17367Mi         

The struct definition is like below.

type AppCache struct {
	FieldA    map[string]map[string][]map[string]string 
}

and data in struct looks like this

{
  "fieldA": {
      "xyz": [
         {"123": "abc"}, {"456": "def"}
      ],
      ...
   }
}

I would like to know where the remaining memory is used apart from the heapAllocation. Is there a way optimize this to reduce memory footprint?

Another instance of same app taking 25gb of pod memory

"Alloc": 11107035408,
  "TotalAlloc": 135557988112,
  "Sys": 42268687144,
  "Lookups": 0,
  "Mallocs": 2201319532,
  "Frees": 2059681913,
  "HeapAlloc": 11107035408,
  "HeapSys": 40125399040,
  "HeapIdle": 27178049536,
  "HeapInuse": 12947349504,
  "HeapReleased": 27172528128,
  "HeapObjects": 141637619,
  "StackInuse": 1507328,
  "StackSys": 1507328,
  "MSpanInuse": 216770976,
  "MSpanSys": 490551984,
  "MCacheInuse": 31200,
  "MCacheSys": 31200,
  "BuckHashSys": 1957090,
  "GCSys": 1604467408,
  "OtherSys": 44773094,
  "NextGC": 22214518256,
  "LastGC": 1688032834651951600,
  "PauseTotalNs": 8150878,
$k top po  test-pod --containers=true
POD         NAME            CPU(cores)   MEMORY(bytes)   
test-pod    app-container   1m           25897Mi     

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.