Category: Data

In past projects at Qvault we had an application that typically ran with ~2GB in memory at any given time. By simply changing the order of some uint variables we managed to drop the memory usage to less than 4GB. The vast majority of this allocated memory is due to an enormous slice of stats structs.

Each uint8 uses 1 byte, and the single uint16 needs We began to suspect that we had some wasteful memory issues, so I built the following little program to show how memory is being used by our struct: On my MacBook using Go 1.14.1 the above program prints: Notice that even though NumPosts only has a size of 1 byte, the next field, Reach, still starts at offset 2.

In our updated struct however, we have grouped the smaller fields, and since they add up to the same amount of memory as the larger Reach field we can save some space!

Related Articles