How to organize the go struct, in order to save memory.

In this article I want to explain you how the memory works internally in a 64 bit architecture, when you instantiate a struct. (You can play with the code )

How memory works internally :

When you have a struct like this one :

type myStruct struct {
myBool bool // 1 byte
myFloat float64 // 8 bytes
myInt int32 // 4 bytes

As you see a boolean takes 1 byte, a float64 8 bytes, and an int32 4 bytes.

But the memory allocates consecutive packet of 8 bytes. So instead of taking 1 + 8 + 4 = 13 bytes. This struct will takes : 24 bytes

a := myStruct{}
fmt.Println(unsafe.Sizeof(a)) // 24 bytes

Because in memory we will have :

So 8 + 8 + 8 = 24 bytes

How to optimize :

It’s possible to optimize, ordering the struct by bytes taken :

type myStructOptimized struct {
myFloat float64 // 8 bytes
myBool bool // 1 byte
myInt int32 // 4 bytes

This new struct ordered will take now :

b := myStructOptimized{}
fmt.Println(unsafe.Sizeof(b)) // ordered 16 bytes

16 bytes, because in memory it will be allocated like that ( :

8 + 8 = 16 bytes

As you can see, there are a padding to respect the data alignment, (, So it means that in some case the data is not optimally word-aligned (depending on many things processor, cache memory, virtual memory) and it can sometimes slower the application, so benchmark everything :D

Links :

Special Thanks for the review to the gophers of the #performance channel on the Go slack



Lead engineer at Mindgeek

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store