Home > Backend Development > Golang > How Much Memory Does an Uninitialized Go Map Allocate?

How Much Memory Does an Uninitialized Go Map Allocate?

Linda Hamilton
Release: 2024-12-23 04:36:14
Original
725 people have browsed it

How Much Memory Does an Uninitialized Go Map Allocate?

Determining Memory Allocation for Uninitialized Go Maps

In JavaScript, when creating a map, the initial capacity is not explicitly specified. The allocated memory is implementation-dependent. This article explores how to determine memory allocation for such maps in Go.

Initial Memory Allocation

When a map is created without specifying the initial capacity, Go allocates a small amount of memory. This includes a header and an array of buckets.

Understanding Map Structure

According to Go's map type source code, a map comprises:

  • A header (type hmap) with fields occupying 40 bytes
  • An array of buckets (type bmap) with each bucket occupying 8 bytes

Calculation

For a 64-bit architecture, the initial memory allocation is as follows:

Header Fields: 40 bytes
Bucket Array (1 bucket): 8 bytes
---------------------------
Total: 48 bytes
Copy after login

Note:

The exact memory allocation may vary depending on factors such as the operating system and architecture.

The above is the detailed content of How Much Memory Does an Uninitialized Go Map Allocate?. 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