Top Banner
CS842: Automatic Memory Management and Garbage Collection Partitioning the Heap 1
48

Partitioning the Heap

Mar 01, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Partitioning the Heap

CS842: Automatic Memory Management and Garbage Collection

Partitioning the Heap

1

Page 2: Partitioning the Heap

ScheduleM W

Sept 14 Intro/Background Basics/ideas

Sept 21 Allocation/layout GGGGC

Sept 28 Mark/Sweep Copying GC

Octo 5 Details Ref C

Octo 12 Thanksgiving Mark/Compact

Octo 19 Partitioning/Gen Generational

Octo 26 Other part Runtime

Nove 2 Final/weak Conservative

Nove 9 Ownership Regions etc

Nove 16 Adv topics Adv topics

Nove 23 Presentations Presentations

Nove 30 Presentations Presentations

2

Page 3: Partitioning the Heap

Review

• Roots → objects → reachable objects →discard unreachable

• Moving vs. non-moving

• Copying vs. compacting

• Pause vs. live

3

Page 4: Partitioning the Heap

Tradeoffs

• Different strategies have different tradeoffs

• Mark-and-sweep: No moving, fragments

• Copying: Better locality, worst utilization

• Compacting: Good locality, very slow

4

Page 5: Partitioning the Heap

Tradeoffs

• Best strategy to use depends on program

• Every program is different…

• Instead, use multiple strategies

• Choose “intelligently” per object

5

Page 6: Partitioning the Heap

Partitioning

6

Stack Heap

Partition Partition

Page 7: Partitioning the Heap

Paritioning

• Generally:• Objects in partitions share some property

• Roots can point at any partition

• Cross-partition references allowed

• Partitions may hold dead cross-partition references

• May be false for some partitioning style

7

Page 8: Partitioning the Heap

Why partition?

• Usually: Use different GC schemes

• Often: GC only some partition(s) to reduce GC pause time

• Sometimes: Different allocation schemes, fragmentation avoidance, etc.

• Segregated blocks!

8

Page 9: Partitioning the Heap

Elephant in the room

• #1 partitioning scheme is generational GC

• Generational = partition by age

• We’ll get there, but others first

9

Page 10: Partitioning the Heap

Mobility

• Moving objects reduces fragmentation

• Moving objects means references must change: Burden on compiler to handle references properly

• “Normal” C code not so nice

10

Page 11: Partitioning the Heap

Mobility

• Non-moving: Must have at least onereference

• Java communicating with C:

• Keep a reference in Java’s roots

• Give reference to C (GC unaware)

• When C is done, discard Java root ref

11

Page 12: Partitioning the Heap

Partitioning for mobility

12

Stack Heap

Movable partition “Pinned” partition

Invisible C stack frame

Page 13: Partitioning the Heap

Partitioning for mobility

13

Stack Heap

Movable partition “Pinned” partition

Invisible C stack frame

Page 14: Partitioning the Heap

Partitioning for mobility

13

Stack Heap

Movable partition “Pinned” partition

Invisible C stack frame

We don’t see this

reference!

Page 15: Partitioning the Heap

Partitioning for mobility

13

Stack Heap

Movable partition “Pinned” partition

Invisible C stack frame

We don’t see this

reference!

This reference keeps crucial object alive

Page 16: Partitioning the Heap

Partitioning for mobility

13

Stack Heap

Movable partition “Pinned” partition

Invisible C stack frame

We don’t see this

reference!

This reference keeps crucial object alive

References which can

never go to C are movable

Page 17: Partitioning the Heap

Mobility necessities

• Must know all objects which might be immobile (e.g. only certain types go to C)

• Assure visible root reference stays alive

• Immobile heap mark-and-sweep

• Must GC whole heap, not one partition

14

Page 18: Partitioning the Heap

GC’ing with partitions

• Every GC strategy has a mark-like phase

• Collectively these are called “tracing”

• This phase broadly similar in each GC

• When scanning object, determine which kind of tracing based on which partition

15

Page 19: Partitioning the Heap

Partitioning algorithm sketchtrace():

worklist := new Queue()(add roots to worklist)while loc := worklist.pop():obj := *locif obj is in M&S or compacting partition:marked := mark(obj)

else if obj is in copying partition:obj, marked := copy(obj)

if !marked:(add obj’s references to worklist)

sweep():mandsSweep()compactingSweeps()

16

Page 20: Partitioning the Heap

Distinguishing partitions

• Partitions are separate sets of pools

• Pool remembers which partition it’s in(typically pool header)

17

Page 21: Partitioning the Heap

Distinguishing partitions

• Partitions are separate sets of pools

• Pool remembers which partition it’s in(typically pool header)

17

“Pool mask”: 0xFFFF0000(0x0104B0C8 & 0xFFFF0000) == 0x01040000

(struct Pool *) ((size_t) p & POOL_MASK)

Page 22: Partitioning the Heap

Distinguishing partitions

• Partitions are separate sets of pools

• Pool remembers which partition it’s in(typically pool header)

17

“Pool mask”: 0xFFFF0000(0x0104B0C8 & 0xFFFF0000) == 0x01040000

(struct Pool *) ((size_t) p & POOL_MASK)

Align pools to get nice pool mask

Page 23: Partitioning the Heap

Breather

• Partition to use best of multiple strategies

• Partitions just part of heap: References from roots and other partitions

• Algorithm mixes by checking partition

• Partition by mobility for C

18

Page 24: Partitioning the Heap

Partition by size• Segregated blocks: Different pools for different

object sizes, no fragmentation

• Copying: No fragmentation + improved locality, must copy objects

• Mix them:• Copy smallest objects• M&S + segregated blocks for larger• M&S + regular free-list for largest

19

Page 25: Partitioning the Heap

Partitioning for size

20

Stack Heap

Small obj. partition Large obj. partition

Page 26: Partitioning the Heap

Partitioning for size

20

Stack Heap

Small obj. partition Large obj. partition

Want to avoid time taken to copy big objects

Page 27: Partitioning the Heap

Partitioning for size

20

Stack Heap

Small obj. partition Large obj. partition

Want to avoid time taken to copy big objects

Locality less relevant to big objects: Cache fits

only one anyway!

Page 28: Partitioning the Heap

Partitioning for size

21

Stack Heap

Small obj. partition Large obj. partition

Page 29: Partitioning the Heap

Partition by size

• Natural extension of segregated blocks

• One size per pool [copy pool(s) flexible]

• Too-large objects have own pool(s)

• No fragmentation except last pool(set)

• Other benefits: Fast allocation, full-heap sweep only on large objects (faster)

22

Page 30: Partitioning the Heap

Partition by kind

• “Kind” may have many meanings:

• Type (e.g. language type annotations, mutability)

• GC-relevant category (e.g. references vs. no references)

• Runtime properties (e.g. owner, trust, source)

• Memory properties (e.g. alignment, executability)

23

Page 31: Partitioning the Heap

Partitioning by executability• JIT compilers generate code at runtime

• That code can die

• That code must be executable

• Making whole heap executable is a very bad idea™

• Place executable “objects” on own executable heap

24

Page 32: Partitioning the Heap

Partitioning by thread

• Threads cause problems:

• Allocation contention

• Stop-the-world (all threads must yield)

• Let’s not even talk about reference counting

• Can partitioning by threads solve them?

25

Page 33: Partitioning the Heap

Partitioning by thread

• Each thread gets own partition

• Collect just one thread!

26

Page 34: Partitioning the Heap

Partitioning by thread

27

T1 stack Heap

Thread #2 partition Thread #1 partition

T2 stack

Page 35: Partitioning the Heap

Partitioning by thread

28

T1 stack Heap

Thread #2 partition Thread #1 partition

T2 stack

Page 36: Partitioning the Heap

Partitioning by thread

28

T1 stack Heap

Thread #2 partition Thread #1 partition

T2 stack

Can only scan own thread’s roots:

Others may change during scan!

Page 37: Partitioning the Heap

Partitioning by thread

28

T1 stack Heap

Thread #2 partition Thread #1 partition

T2 stack

Can only scan own thread’s roots:

Others may change during scan!

Don’t follow pointers out of partition: Not our job

Page 38: Partitioning the Heap

Partitioning by thread

28

T1 stack Heap

Thread #2 partition Thread #1 partition

T2 stack

Can only scan own thread’s roots:

Others may change during scan!

Don’t follow pointers out of partition: Not our job

Our thread’s objects could be referenced or changed

by other threads!

Page 39: Partitioning the Heap

Inter-thread madness

• Inter-thread links/modification causing problems

• Write barriers to the rescue!

write(loc, obj):if threadOf(loc) != threadOf(obj) or

threadOf(loc) != myThread():markAsInterthread(obj)

*loc := obj

29

Page 40: Partitioning the Heap

Partitioning by thread

30

T1 stack Heap

Thread #2 partition Thread #1 partition

T2 stack

Page 41: Partitioning the Heap

Partitioning by thread

31

T1 stack Heap

Thread #2 partition Thread #1 partition

T2 stack

Page 42: Partitioning the Heap

Partitioning by thread

32

T1 stack Heap

Thread #2 partition Thread #1 partition

T2 stack

Page 43: Partitioning the Heap

Partitioning by thread

32

T1 stack Heap

Thread #2 partition Thread #1 partition

T2 stack

Cannot be collected!

Page 44: Partitioning the Heap

Inter-thread objects

• Inter-thread mark is a long-term mark

• Any object with inter-thread references cannot be collected in partial GC

• Still need occasional full GC to collect inter-thread objects

• Maybe move objects to other threads

33

Page 45: Partitioning the Heap

Thread-local allocation

• Without thread partitioning, allocation must lock

• Big lock, big contention!

• Partition per thread: No locking

• Partitioning by thread for allocation worthwhile even without per-thread GC

34

Page 46: Partitioning the Heap

When/what to GC

• With flat GC: When all pools full

• With partitioned GC: When a partition is full

• How to decide when to do a full GC?

• Depends on partitioning scheme…

• Threads: When large portion of objects are inter-thread marked

35

Page 47: Partitioning the Heap

Partitioning by age

• “Young” partition and “old” partition

• Called “generations”

• Objects allocated in young partition

• Move to old partition if they survive

• Usually collect only young (most objects die young)

36

Page 48: Partitioning the Heap

Partitioning by age

• “Young” partition and “old” partition

• Called “generations”

• Objects allocated in young partition

• Move to old partition if they survive

• Usually collect only young (most objects die young)

36