takeaway


This series is based on 64-bit platform, 1Page=8KBCopy the code

Today we kick off the second chapter of the Go Easy series, “Memory and Garbage Collection.”

The chapter on memory and garbage Collection is divided into three parts:

  • Knowledge preparation: Do some knowledge preparation for the following content, knowledge preparation includes
    • Pointer size
    • Tcmalloc memory allocation principle
  • Go memory design and implementation
  • Go garbage collection principle

This preface


The first part is the size of the pointer of the first point of knowledge preparation.

Why is the size of a pointer a point of knowledge?

Because the subsequent memory management content will involve some data structures, these data structures use Pointers, and the storage of pointer values requires memory space, so we need to understand the size of Pointers, so that we can understand the intention of some design; Second, this is something that bothers me because I see that the underlying type of Pointers on 64-bit platforms is defined as uint64.

To figure this out, we need to know two things:

  1. Storage unit
  2. CPU bus

What is a storage unit?


A memory unit is a basic unit of memory. Each memory unit is 8 bits, or 1Byte, as shown in the figure below:

Also, as we can see from the figure above, each storage unit is numbered. What is this number?

  • That’s what we call “memory location.”
  • That’s the value of the pointer

Conclusion: The value of the pointer is the number of the storage unit.

Then, we just need to know what the maximum number of this number is to know the size needed to store the value of the pointer. To find this maximum, you need to know something about the CPU bus.

CPU bus concept


The CPU bus consists of the system bus, and so on.

Bus composition
The system bus
Etc. Other buses…

The system bus consists of a series of buses.

Composition of the system bus
The address bus
The data bus
Signal bus

The address of memory (the number of storage cells) is passed through the address bus, and “each line” in the address bus passes a binary 0 or 1, as shown in the figure below.

The width of the address bus determines how many zeros or ones can be passed at a time. Since 64-bit cpus can process 64 bits of data at a time, the width of the address bus can theoretically be supported up to 64, or 2^64 combinations representing numbers ranging from 0 to 2^64-1.

Conclusion: In theory, 64-bit CPU address bus can transmit the range of 10 base numbers from 0 to 2^64-1.

The address bus of a 64-bit CPU is addressable in the range 0 to 2^64-1. A type is required to store the value of this pointer, expressed in sectors. Is it 8 bytes? So: on 64-bit platforms, a pointer is 8 bytes in size.

By the way, to expand the question:

Why is addressable space 4GB on 32-bit platforms?

Note: 64-bit is too large, so we use 32-bit for this problemCopy the code

Let’s break it down:

  • Since 32-bit platforms can support the address bus with a maximum width of 32, and the range of storage unit numbers that represent it: 0 to 2^32-1
  • A maximum of 2^32 storage units can be found
  • The size of the storage unit is 8 bits (1Byte).

32-bit platforms can find up to 2^32 memory units.

2^32 storage units == 2^32 1Byte == 2^32 byte == 4GByte == 4GB

Just to sum up


Let’s take a look at the following:

  • The basic unit of memory is a storage unit
  • The storage unit is 8 bits
  • The value of the pointer is the number of the storage unit
  • The width of the CPU address bus determines the maximum range of pointer values

Check out the Go Easy series for more


Link tigerb. Cn/go / # / kernal…