24
Garbage Collection and Garbage Collection and Memory Management Memory Management 480/680 – Comparative Languages 480/680 – Comparative Languages

Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Embed Size (px)

Citation preview

Page 1: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection and Memory Garbage Collection and Memory ManagementManagement

CS 480/680 – Comparative LanguagesCS 480/680 – Comparative Languages

Page 2: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 2

Motivation for GCMotivation for GC

int mysub(times) {myClass *myObject;myObject = new myClass;for(int i=0; i<times; i++) {

int a = myObject.firstMethod();int b = myObject.secondMethod();

}return(a/b);

}

What’s wrong with this code?What’s wrong with this code?

Page 3: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 3

Memory ManagementMemory Management Explicit memory management

• C++ new/delete• C malloc/free

Garbage collection• Java new Class• Ruby Class.new• Perl• Smalltalk• Etc…

Page 4: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 4

What is Garbage CollectionWhat is Garbage Collection Ideally, the garbage collector will find all objects that

will not be accessed in the future. Of course, this is impossible, but a good guess is those

objects that have no references to them remaining in scope.

When to invoke GC?• When memory is low

• When the local scope changes

• When the system is idle

• When a reference is destroyed

• Others?

Page 5: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 5

Advantages of Garbage CollectionAdvantages of Garbage Collection Easier on the programer

• Create objects when needed• No need to de-allocate space• No need for destructors and other special memory

considerations

Less errors• Less memory leaks• No dangling pointers

Page 6: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 6

GC StrategiesGC Strategies Two of the most common strategies are:

• Tracing Includes the very common Mark & Sweep method

• Reference Counting

Page 7: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 7

Tracing Garbage CollectorsTracing Garbage Collectors Start with all objects accessible to the main

program (called roots)• Registers• Stack• Instruction pointer• Global variables

Find all objects reachable from the roots

Page 8: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 8

RootsRoots

D0

D1

D2

A0

A1

A7

……

$7000 ?

rtrnrtrn

$6FFE$6FFC

Old A6Old A6var1 var2

paramparam

$6FFA$6FF8$6FF6$6FF4$6FF2$6FF0$6FEE$6FEC$6FEA$6FE8

ptr1ptr2

Globalvariable

list

PC

Page 9: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 9

Reference ChainsReference Chains

$7000 ?

rtrnrtrn

$6FFE$6FFC

Old A6Old A6var1 var2

paramparam

$6FFA$6FF8$6FF6$6FF4$6FF2$6FF0$6FEE$6FEC$6FEA$6FE8

ptr1ptr2

myObject

acctList

Value

Next

Page 10: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 10

Tri-color AlgorithmTri-color Algorithm Every object is the system is “marked” in one

of three colors:• White – candidate for recycling (condemned)• Black – Safe from recycling; No references to

objects in the white set• Grey – Safe from recycling; May refer to objects in

the white set

Page 11: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 11

InitializationInitialization White set – everything except the roots Black set – empty Grey set – the roots (already safe from

recycling)

Page 12: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 12

TracingTracing

Repeat:

1. Pick an object from the grey set

2. Grey all white objects that this object refers to

3. Move the object to the black set

Until: The grey set is empty

Now recycle all objects remainingin the white set.

Page 13: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 13

Example Step (1)Example Step (1)

$7000 ?

rtrnrtrn

$6FFE$6FFC

Old A6Old A6var1 var2

paramparam

$6FFA$6FF8$6FF6$6FF4$6FF2$6FF0$6FEE$6FEC$6FEA$6FE8

ptr1ptr2

Value

Next

Value

Next

Page 14: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 14

Example Step (2)Example Step (2)

$7000 ?

rtrnrtrn

$6FFE$6FFC

Old A6Old A6var1 var2

paramparam

$6FFA$6FF8$6FF6$6FF4$6FF2$6FF0$6FEE$6FEC$6FEA$6FE8

ptr1ptr2

Value

Next

Value

Next

Page 15: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 15

Example Step (3)Example Step (3)

$7000 ?

rtrnrtrn

$6FFE$6FFC

Old A6Old A6var1 var2

paramparam

$6FFA$6FF8$6FF6$6FF4$6FF2$6FF0$6FEE$6FEC$6FEA$6FE8

ptr1ptr2

Value

Next

Value

Next

Page 16: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 16

Example Step (4)Example Step (4)

$7000 ?

rtrnrtrn

$6FFE$6FFC

Old A6Old A6var1 var2

paramparam

$6FFA$6FF8$6FF6$6FF4$6FF2$6FF0$6FEE$6FEC$6FEA$6FE8

ptr1ptr2

Value

Next

Value

Next

Page 17: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 17

An Important ObservationAn Important Observation Note that, by following this method, an object

in the black set can never refer to an object in the white set

When an object is moved to the black set, all the objects it points to are moved to the grey set.

At the end, all of the objects in the white set have no live references to them, and can safely be removed.

Page 18: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 18

Variations of Tracing GCVariations of Tracing GC To move or not to move…

• Mark & Sweep – keep a few bits with each object to indicate if it is in the black, grey, or white sets. After marking, recycle every object in memory in the white set.

• Copying GC – relocate objects in the black set to a “safe” area of memory, then recycle everything else

This is nice for creating good locality of reference! Also reduces fragmentation. Can be slower.

Page 19: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 19

More VariationsMore Variations Can the collector

identify which parts of objects are references (pointers) and which are not?• Yes: “precice” GC

• No: “conservative” GC What if pointers are

encrypted, scrambled, or stored in some other “funny” way?

Value

Next

Value

Next

Page 20: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 20

Still More VariationsStill More Variations Can the GC mechanism run incrementally?

• No: stop the rest of the system, do GC, then start up again

• Yes: interleave their work with work units from the rest of the system

Can the GC mechanism run in a parallel thread?

Note: All methods must at least scan the root set all at once• Why?

Page 21: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 21

Generational GCGenerational GC It is not strictly necessary to place everything

(except the root objects) in the white set• Faster GC can be performed by limiting the size of

the white set• What should go there?

Statistically speaking, the newest objects are also the most likely to go out of scope soon• Referred to as infant mortality or the generational

hypothesis• Divide run time into generations, only put objects

created in the current generation into the white set

Page 22: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 22

Disadvantages of Tracing GCDisadvantages of Tracing GC Can be invoked at any time, and can be slow

• Not a good thing for Real Time (RT) computing

The GC thread violates locality of reference by intentionally looking at memory areas that haven’t been accessed recently• So what?

Page 23: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 23

Open ResearchOpen Research Is all memory that is reachable still in use? Consider ARGV & ARGC. Usually read,

parsed, and then ignored for the rest of the process. Do we really need to keep it around?

Research area: finding objects that will never be used again…

Page 24: Garbage Collection and Memory Management CS 480/680 – Comparative Languages

Garbage Collection 24

Reference CountingReference Counting Keep a reference count with every object Advantages:

• Easy to implement• Fast and incremental GC

Disadvantages:• Must update reference count whenever a reference

is created or deleted: SLOW• Need extra space in every object• Does not reclaim space if there are reference cycles

(as in a doubly linked list)