Home > Backend Development > Golang > How Much Memory Does a Go Map Initially Reserve?

How Much Memory Does a Go Map Initially Reserve?

Patricia Arquette
Release: 2024-12-18 19:13:14
Original
484 people have browsed it

How Much Memory Does a Go Map Initially Reserve?

How Much Memory Do Go Maps Reserve?

In Go, maps are allocated dynamically, with the initial capacity left unspecified. This means that the memory allocation is implementation-specific. To determine the initial memory allocation, consult the map's internal structure.

Map Structure

A map consists of a header (type hmap) and an array of buckets (type bmap). When created without an initial capacity, a single bucket is allocated.

Header Structure and Size

The header comprises:

  • 1 * int
  • 2 * uint8
  • 1 * uint16
  • 1 * uint32
  • 2 * unsafe.Pointer
  • 1 * uintptr

The size of int, uintptr, and unsafe.Pointer is equal to the word size (8 bytes on 64-bit machines). Therefore, the header size is 40 bytes.

Bucket Structure and Size

Each bucket contains an array of 8 * uint8. Adding this to the header size, we get the total initial memory allocation:

Header Size (40 bytes) + Bucket Size (8 bytes) = **48 bytes (64-bit architecture)**
Copy after login

The above is the detailed content of How Much Memory Does a Go Map Initially Reserve?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template