Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go memory allocation

Deepu K Sasidharan
February 14, 2020
1.5k

Go memory allocation

Deepu K Sasidharan

February 14, 2020
Tweet

Transcript

  1. Process Virtual Memory
    Resident set
    mheap
    P1(Logical Processor/Hardware Thread)
    Goroutine 1
    stack
    arena
    arena ...
    mcentral mcentral mcentral
    ..
    mcache mspan
    8b
    mspan
    16b
    mspan
    32b
    mspan
    ..
    mspan
    ..
    mspan
    ..
    mspan
    ..
    mspan
    ..
    mspan
    32768b
    mspan 8b (non empty)
    mspan 8b (empty)
    mspan 16b
    mspan 16b
    mspan
    mspan
    P2
    G6
    stack
    mcache
    ..
    ..
    ..
    ..
    Pn
    Gn
    stack
    mcache
    mspan 8b
    mspan 8b
    mspan 8b
    mspan 8b
    mspan 8b
    mspan 8b

    View full-size slide

  2. mspan
    mspan 8b 8b 8b 8b …. 8b 8b 8b 1024 objects
    mspan 16b 16b 16b 16b …. 16b 16b 16b 512 objects
    mspan 32b 32b 32b 32b …. 32b 32b 32b 256 objects
    mspan 48b 48b 48b 48b …. 48b 48b 48b 170 objects
    mspan 32768 bytes 1 object
    mspan .. .. .. .. …. .. .. .. ...

    View full-size slide

  3. Go Scheduler
    P
    M
    G
    Processor
    Core
    G G
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    LRQ = Local Run Queue
    LRQ

    View full-size slide

  4. Tiny allocation

    View full-size slide

  5. Tiny allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan 16b (non
    empty)
    mspan 16b
    (empty)
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    8b
    mspan
    16b
    mspan
    16b
    Tiny allocation slot
    12b

    View full-size slide

  6. Tiny allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan 16b (non
    empty)
    mspan 16b
    (empty)
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    8b
    mspan
    16b
    mspan
    16b
    Tiny allocation slot
    12b
    4b

    View full-size slide

  7. Tiny allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan 16b (non
    empty)
    mspan 16b
    (empty)
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    8b
    mspan
    16b
    mspan
    16b
    Tiny allocation slot
    12b 4b

    View full-size slide

  8. Tiny allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan 16b (non
    empty)
    mspan 16b
    (empty)
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    8b
    mspan
    16b
    mspan
    16b
    Tiny allocation slot
    12b 4b
    10b

    View full-size slide

  9. Tiny allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan 16b (non
    empty)
    mspan 16b
    (empty)
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    8b
    mspan
    16b
    mspan
    16b
    Tiny allocation slot
    12b 4b
    10b

    View full-size slide

  10. Tiny allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan 16b (non
    empty)
    mspan 16b
    (empty)
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    8b
    mspan
    16b
    mspan
    16b
    Tiny allocation slot
    12b 4b 10b

    View full-size slide

  11. Tiny allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan 16b (non
    empty)
    mspan 16b
    (empty)
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    8b
    mspan
    16b
    mspan
    16b
    Tiny allocation slot
    12b 4b 10b
    12b

    View full-size slide

  12. Tiny allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan 16b (non
    empty)
    mspan 16b
    (empty)
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    8b
    mspan
    16b
    mspan
    16b
    Tiny allocation slot
    12b 4b 10b
    12b
    No
    free
    mspan
    16b

    View full-size slide

  13. Tiny allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan 16b (non
    empty)
    mspan 16b
    (empty)
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    8b
    mspan
    16b
    mspan
    16b
    Tiny allocation slot
    12b 4b 10b
    12b

    View full-size slide

  14. Tiny allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan 16b (non
    empty)
    mspan 16b
    (empty)
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    8b
    mspan
    16b
    mspan
    16b
    Tiny allocation slot
    12b 4b 10b
    12b

    View full-size slide

  15. Tiny allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan 16b (non
    empty)
    mspan 16b
    (empty)
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    16b
    mspan
    8b
    mspan
    16b
    mspan
    16b
    Tiny allocation slot
    12b 4b 10b 12b

    View full-size slide

  16. Small allocation

    View full-size slide

  17. Small allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan 32b (non
    empty)
    mspan 32b
    (empty)
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    8b
    mspan
    16b
    mspan
    32b
    Tiny allocation slot
    12b

    View full-size slide

  18. Small allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan
    8b
    mspan
    16b
    mspan
    32b
    Tiny allocation slot
    12b
    20b
    mspan 32b (non
    empty)
    mspan 32b
    (empty)
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b

    View full-size slide

  19. Small allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan
    8b
    mspan
    16b
    mspan
    32b
    Tiny allocation slot
    12b 20b
    mspan 32b (non
    empty)
    mspan 32b
    (empty)
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b

    View full-size slide

  20. Small allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan
    8b
    mspan
    16b
    mspan
    32b
    Tiny allocation slot
    12b 20b
    32b
    No
    free
    mspan
    32b
    mspan 32b (non
    empty)
    mspan 32b
    (empty)
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b

    View full-size slide

  21. Small allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan
    8b
    mspan
    16b
    mspan
    32b
    Tiny allocation slot
    12b 20b
    32b
    No
    free
    mspan
    32b
    mspan 32b (non
    empty)
    mspan 32b
    (empty)
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b

    View full-size slide

  22. Small allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan
    8b
    mspan
    16b
    mspan
    32b
    Tiny allocation slot
    12b 20b
    32b
    mspan 32b (non
    empty)
    mspan 32b
    (empty)
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b

    View full-size slide

  23. Small allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan
    8b
    mspan
    16b
    mspan
    32b
    Tiny allocation slot
    12b 20b
    mspan 32b (non
    empty)
    mspan 32b
    (empty)
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b
    mspan
    32b
    32b

    View full-size slide

  24. Large allocation

    View full-size slide

  25. Large allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan
    8b
    mspan
    16b
    mspan
    32b
    Tiny allocation slot
    12b 20b
    mspan
    32b
    32b

    View full-size slide

  26. Large allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan
    8b
    mspan
    16b
    mspan
    32b
    Tiny allocation slot
    12b 20b
    mspan
    32b
    32b
    40Kb

    View full-size slide

  27. Large allocation
    P1
    M
    G2
    Processor
    Core
    P = Logical Processor per Hardware Thread
    M = Machine per OS Thread
    G = Goroutine (Coroutine)
    mcache
    mheap
    arena
    mcentral
    mspan 8b (non
    empty)
    mspan 8b
    (empty)
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mspan
    8b
    mcentral
    mspan
    8b
    mspan
    16b
    mspan
    32b
    Tiny allocation slot
    12b 20b
    mspan
    32b
    32b
    mspan
    40Kb

    View full-size slide