20201014

@ShiKaiWi

Plan

  • today: malloc.go
  • tomorrow: mheap.go learning about the mheap struct and mheap_.init()

Notes

Procedure of malloc_init():

  1. check parameters concerning the memory.
  2. mheap_.init().
  3. allocate mcache0(defined by proc.go).
  4. create initial arena growth hints.

More

Q1: Why does the code look like this:

const (
    pageSize  = _PageSize
    _PageSize = 1 << _PageShift
)

Q2: Why does the heap in golang use two level arenas? Check the following comments and why windows 64bit has a 4MB arena size?

// The number of bits in a heap address, the size of heap
// arenas, and the L1 and L2 arena map sizes are related by
//
//   (1 << addr bits) = arena size * L1 entries * L2 entries
//
// Currently, we balance these as follows:
//
//       Platform  Addr bits  Arena size  L1 entries   L2 entries
// --------------  ---------  ----------  ----------  -----------
//       */64-bit         48        64MB           1    4M (32MB)
// windows/64-bit         48         4MB          64    1M  (8MB)
//       */32-bit         32         4MB           1  1024  (4KB)
//     */mips(le)         31         4MB           1   512  (2KB)

It seems the comments provide the reason why windows has a smaller arena size:

// This is currently 64MB on 64-bit non-Windows and 4MB on
// 32-bit and on Windows. We use smaller arenas on Windows
// because all committed memory is charged to the process,
// even if it's not touched. Hence, for processes with small
// heaps, the mapped arena space needs to be commensurate.
// This is particularly important with the race detector,
// since it significantly amplifies the cost of committed
// memory.

Q3: What does the procedure of the golang app startup?

Q4: What proc.go