Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24...

33
Lecture 24 Public Service Announcement: “Hackers@Berkeley is hosting BEARHACK this Saturday at 11AM in the Wozniak Lounge. It’s a 24 hour hackathon - there’ll be tons of good food (Cheeseboard, sushi & boba!), activities (including massages!), and prizes (including an Occulus Rift). Administrivia: There will be a homework assignment due next Wednesday night (but of course you can do it early!). Project 3 to be posted today or tomorrow. Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 1

Transcript of Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24...

Page 1: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Lecture 24

Public Service Announcement: “Hackers@Berkeley is hosting BEARHACKthis Saturday at 11AM in the Wozniak Lounge. It’s a 24 hour hackathon- there’ll be tons of good food (Cheeseboard, sushi & boba!), activities(including massages!), and prizes (including an Occulus Rift).

Administrivia:

• There will be a homework assignment due next Wednesday night(but of course you can do it early!).

• Project 3 to be posted today or tomorrow.

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 1

Page 2: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Storage Management

• Java has no means to free dynamic storage.

• However, when no expression in any thread can possibly be influ-enced by or change an object, it might as well not exist:

IntList wasteful ()

{

IntList c = new IntList (3, new IntList (4, null));

return c.tail;

// variable c now deallocated, so no way

// to get to first cell of list

}

• At this point, Java runtime, like Scheme’s, recycles the object c

pointed to: garbage collection.

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 2

Page 3: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Garbage Collection: Reference Counting

• Idea: Keep count of number of pointers to each object.

X: 1 1 1

1 A 1 B 1 C

Y:

X: 1 2 1

1 A 1 B 1 C

Y:

Y = X.tail;

X: 0 3 1

1 A 1 B 1 C

Y:

X = Y;

X: 2 1

0 A 1 B 1 C etc.

Y:

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 3

Page 4: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Garbage Collection: Mark and Sweep

Roots

5 E B G

D

7

C

42

A

F

42

A

D

B*

F

C

A

D*

7 G D

E* F

C

G*

B

• Start at roots (named variables, static and on stack)

• Perform graph traversal to find and mark all reachable storage.

• Sweep over memory, adding all unmarked storage to free list.

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 4

Page 5: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Garbage Collection: Mark and Sweep

Roots

5 E B G

D

7

C

42

A

F

42

A

D

B*

F

C

A

D*

7 G D

E* F

C

G*

B

• Start at roots (named variables, static and on stack)

• Perform graph traversal to find and mark all reachable storage.

• Sweep over memory, adding all unmarked storage to free list.

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 4

Page 6: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Garbage Collection: Mark and Sweep

Roots

5 E B G

D

7

C

42

A

F

42

A

D

B*

F

C

A

D*

7 G D

E* F

C

G*

B

• Start at roots (named variables, static and on stack)

• Perform graph traversal to find and mark all reachable storage.

• Sweep over memory, adding all unmarked storage to free list.

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 4

Page 7: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Garbage Collection: Mark and Sweep

Roots

5 E B G

D

7

C

42

A

F

42

A

D

B*

F

C

A

D*

7 G D

E* F

C

G*

B

• Start at roots (named variables, static and on stack)

• Perform graph traversal to find and mark all reachable storage.

• Sweep over memory, adding all unmarked storage to free list.

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 4

Page 8: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Garbage Collection: Mark and Sweep

Roots

5 E B G

D

7

C

42

A

F

42

A

D

B*

F

C

A

D*

7 G D

E* F

C

G*

B

• Start at roots (named variables, static and on stack)

• Perform graph traversal to find and mark all reachable storage.

• Sweep over memory, adding all unmarked storage to free list.

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 4

Page 9: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Garbage Collection: Mark and Sweep

Roots

5 E B G

D

7

C

42

A

F

42

A

D

B*

F

C

A

D*

7 G D

E* F

C

G*

B

• Start at roots (named variables, static and on stack)

• Perform graph traversal to find and mark all reachable storage.

• Sweep over memory, adding all unmarked storage to free list.

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 4

Page 10: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Garbage Collection: Mark and Sweep

Roots

5 E B G

D

7

C

42

A

F

42

A

D

B*

F

C

A

D*

7 G D

E* F

C

G*

B

• Start at roots (named variables, static and on stack)

• Perform graph traversal to find and mark all reachable storage.

• Sweep over memory, adding all unmarked storage to free list.

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 4

Page 11: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Garbage Collection: Mark and Sweep

Roots

5 E B G

D

7

D

B* D*

7 G D

E* G*

B

• Start at roots (named variables, static and on stack)

• Perform graph traversal to find and mark all reachable storage.

• Sweep over memory, adding all unmarked storage to free list.

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 4

Page 12: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Copying Garbage Collection

• Copy (and move) only reachable (useful) storage from ‘from’ spaceto ‘to’ space.

• The ‘from’ and ‘to’ areas are called semispaces. Need twice the vir-tual memory you actually use.

• As you copy, mark ‘from’ storage as moved, and leave behind a for-warding pointer that tells how to translate other references to theold storage.

• At end of algorithm, ‘from’ and ‘to’ swap roles, and the old ‘from’area is freed en masse.

• Copied storage is compacted (gaps squeezed out) with possible ad-vantages for memory access.

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 5

Page 13: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Copying Garbage Collection, Illustrated

Roots

B

5

E

from: 42

A

D

B

G F

C

A

D

7 G D

E F

C

G

B

to:

Roots

5 E B G

D

7

C

42

A

F

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 6

Page 14: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Copying Garbage Collection, Illustrated

Roots

B’

5

E’

from: 42

A B

G F

C

A

D

7 G

E F

C

G

B

to: D

B’

G D

E’

Roots

5 E B G

D

7

C

42

A

F

E’ B’

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 6

Page 15: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Copying Garbage Collection, Illustrated

Roots

B’

5

E’

from: 42

A B

G F

C

A

D

7 G

E F

C

G

B

to: D’

B’

G D

E’ D’

7 G

Roots

5 E B G

D

7

C

42

A

F

E’ B’

D’

7

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 6

Page 16: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Copying Garbage Collection, Illustrated

Roots

B’

5

E’

from: 42

A B

G F

C

A

D

7 G

E F

C

G

B

to: D’

B’

G’ D

E’ D’

7 G

G’

B

Roots

5 E B G

D

7

C

42

A

F

E’ B’ G’

D’

7

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 6

Page 17: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Copying Garbage Collection, Illustrated

Roots

B’

5

E’

from: 42

A B

G F

C

A

D

7 G

E F

C

G

B

to: D’

B’

G’ D’

E’ D’

7 G

G’

B

Roots

5 E B G

D

7

C

42

A

F

E’ B’ G’

D’

7

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 6

Page 18: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Copying Garbage Collection, Illustrated

Roots

B’

5

E’

from: 42

A B

G F

C

A

D

7 G

E F

C

G

B

to: D’

B’

G’ D’

E’ D’

7 G

G’

B

Roots

5 E B G

D

7

C

42

A

F

E’ B’ G’

D’

7

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 6

Page 19: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Copying Garbage Collection, Illustrated

Roots

B’

5

E’

from: 42

A B

G F

C

A

D

7 G

E F

C

G

B

to: D’

B’

G’ D’

E’ D’

7 G’

G’

B

Roots

5 E B G

D

7

C

42

A

F

E’ B’ G’

D’

7

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 6

Page 20: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Copying Garbage Collection, Illustrated

Roots

B’

5

E’

from: 42

A B

G F

C

A

D

7 G

E F

C

G

B

to: D’

B’

G’ D’

E’ D’

7 G’

G’

B’

Roots

5 E B G

D

7

C

42

A

F

E’ B’ G’

D’

7

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 6

Page 21: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Copying Garbage Collection, Illustrated

Roots

B’

5

E’

to:

from: D’

B’

G’ D’

E’ D’

7 G’

G’

B’

Roots

5

E’ B’ G’

D’

7

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 6

Page 22: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Roots and Other Pointers

• Above methods require that we know locations of roots and of pointerfields in objects.

• Positions of some roots change during execution.

• Compiler keeps tables mapping PC to where roots are.

• Runtime type information (virtual tables) keep information of wherepointer fields are.

• Implementation must guarantee that fields are initialized.

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 7

Page 23: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Conservative Garbage Collection

• With C, you have none of the needed information.

• But easy to know the addresses of allocated storage, and sizes ofallocated objects (allocator keeps them around).

• So, guess that any word that looks like an address of allocated stor-age is a valid address.

• Do mark-and-sweep on this assumption (look at whole stack and staticstorage for roots).

• Marks some garbage, but can be surprisingly effective.

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 8

Page 24: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Generational Garbage Collection

• Heap storage tends to “die young.”

• So divide memory into young and old storage, and do copying only onyoung storage.

• Must add old storage that points to young storage to roots.

• When young storage survives a GC (or two), move it to old storage.

• Every now and then, stop the world and do a full garbage collection.

• This technique significantly speeds up GC.

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 9

Page 25: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Region-Based Allocation

• Garbage collection (all forms) does incur overheads, which can beunpredictable,

• While manual freeing is prone to error and inconvenient.

• One compromise is region-based allocation.

• Idea:

– Create a data structure known as a region (or zone, or arena, orvarious other names).

– Provides two operations: allocate object, and free all objects.

• Thus, to perform calculation that creates lots of temporary heapobjects,

– Create region (a local variable).

– Allocate all the temporary storage in this region.

– Delete whole region at end.

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 10

Page 26: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Region Implementation

• Simple implementation: allocate storage in big blocks, and allocateobjects sequentially in the blocks.

• Freeing all blocks frees all the objects quickly.

aRegion:

Page 27: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Region Implementation

• Simple implementation: allocate storage in big blocks, and allocateobjects sequentially in the blocks.

• Freeing all blocks frees all the objects quickly.

aRegion:

x = aRegion.alloc (40);

Page 28: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Region Implementation

• Simple implementation: allocate storage in big blocks, and allocateobjects sequentially in the blocks.

• Freeing all blocks frees all the objects quickly.

aRegion:

x = aRegion.alloc (40);

y = aRegion.alloc (100);

Page 29: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Region Implementation

• Simple implementation: allocate storage in big blocks, and allocateobjects sequentially in the blocks.

• Freeing all blocks frees all the objects quickly.

aRegion:

x = aRegion.alloc (40);

y = aRegion.alloc (100);

z = aRegion.alloc (120);

Page 30: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Region Implementation

• Simple implementation: allocate storage in big blocks, and allocateobjects sequentially in the blocks.

• Freeing all blocks frees all the objects quickly.

aRegion:

x = aRegion.alloc (40);

y = aRegion.alloc (100);

z = aRegion.alloc (120);

v = aRegion.alloc (100);

Page 31: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Region Implementation

• Simple implementation: allocate storage in big blocks, and allocateobjects sequentially in the blocks.

• Freeing all blocks frees all the objects quickly.

aRegion:

x = aRegion.alloc (40);

y = aRegion.alloc (100);

z = aRegion.alloc (120);

v = aRegion.alloc (100);

w = aRegion.alloc (50);

Page 32: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Region Implementation

• Simple implementation: allocate storage in big blocks, and allocateobjects sequentially in the blocks.

• Freeing all blocks frees all the objects quickly.

aRegion:

x = aRegion.alloc (40);

y = aRegion.alloc (100);

z = aRegion.alloc (120);

v = aRegion.alloc (100);

w = aRegion.alloc (50);

aRegion.freeAll ();

Page 33: Lecture 24 - inst.eecs.berkeley.educs164/fa13/lectures/lecture24.pdf · Lecture 24 PublicServiceAnnouncement: “Hackers@BerkeleyishostingBEARHACK thisSaturdayat11AMintheWozniakLounge.It’sa24hourhackathon-there’llbetonsofgoodfood

Region Implementation

• Simple implementation: allocate storage in big blocks, and allocateobjects sequentially in the blocks.

• Freeing all blocks frees all the objects quickly.

x = aRegion.alloc (40);

y = aRegion.alloc (100);

z = aRegion.alloc (120);

v = aRegion.alloc (100);

w = aRegion.alloc (50);

aRegion.freeAll ();

• Potential problem: using x, y, z, . . . after freeAll.

Last modified: Thu Nov 21 16:49:25 2013 CS164: Lecture #24 11