63
Comp-4400: Garbage Collection November 25, 2019 1

Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Comp-4400: Garbage Collection

November 25, 2019

1

Page 2: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

2

Page 3: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Outline

Overview of Garbage Collection

Reference counting

3

Page 4: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Motivating example

What will happen if we run the following program?

public static void main(String[] args) throws Exception{int m=100000;int n=10000;Double [][] A=new Double [m][n];

}

We can check the Heap size of your computer using the following command:

ianguos-MBP:code jianguolu$ java -XX:+PrintFlagsFinal -version | grep -i ’HeapSize’uintx ErgoHeapSizeLimit = 0

{product}uintx HeapSizePerGCThread = 87,241,520

{product}uintx InitialHeapSize := 268,435,456

{product}uintx LargePageHeapSizeThreshold = 134,217,728

{product}uintx MaxHeapSize := 4,294,967,296

{product}java version "1.8.0_71"Java(TM) SE Runtime Environment (build 1.8.0_71-b15)Java HotSpot(TM) 64-Bit Server VM (build 25.71-b15, mixed mode)

The Heapsize is 4GB, big enough for this program. What is really happening behind?

4

Page 5: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Motivating example

What will happen if we run the following program?

public static void main(String[] args) throws Exception{int m=100000;int n=10000;Double [][] A=new Double [m][n];

}

We can check the Heap size of your computer using the following command:

ianguos-MBP:code jianguolu$ java -XX:+PrintFlagsFinal -version | grep -i ’HeapSize’uintx ErgoHeapSizeLimit = 0

{product}uintx HeapSizePerGCThread = 87,241,520

{product}uintx InitialHeapSize := 268,435,456

{product}uintx LargePageHeapSizeThreshold = 134,217,728

{product}uintx MaxHeapSize := 4,294,967,296

{product}java version "1.8.0_71"Java(TM) SE Runtime Environment (build 1.8.0_71-b15)Java HotSpot(TM) 64-Bit Server VM (build 25.71-b15, mixed mode)

The Heapsize is 4GB, big enough for this program. What is really happening behind?

5

Page 6: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Motivating example

What will happen if we run the following program?

public static void main(String[] args) throws Exception{int m=100000;int n=10000;Double [][] A=new Double [m][n];

}

We can check the Heap size of your computer using the following command:

ianguos-MBP:code jianguolu$ java -XX:+PrintFlagsFinal -version | grep -i ’HeapSize’uintx ErgoHeapSizeLimit = 0

{product}uintx HeapSizePerGCThread = 87,241,520

{product}uintx InitialHeapSize := 268,435,456

{product}uintx LargePageHeapSizeThreshold = 134,217,728

{product}uintx MaxHeapSize := 4,294,967,296

{product}java version "1.8.0_71"Java(TM) SE Runtime Environment (build 1.8.0_71-b15)Java HotSpot(TM) 64-Bit Server VM (build 25.71-b15, mixed mode)

The Heapsize is 4GB, big enough for this program. What is really happening behind?

6

Page 7: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

What is garbage collection

I Memory Management technique.I Process of freeing objects no longer referenced by the program.

Fundamental Garbage Collection Property“When an object becomes garbage, it stays garbage”

7

Page 8: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Dynamic Memory Allocation

I In Java, “new" allocates an object for a given class.

1 Account account = new SavingsAccount("1234567");

I But there is no instruction for manually deleting the object.I Objects reclaimed by a garbage collector when the program “does not need it"

anymore.

1 account=null;

I Cases that objects are no longer used:I All references to that object explicitly set to null e.g. object = nullI The object is created inside a block and reference goes out scope once control exit that

block.I Parent object set to null if an object holds the reference to another object and when you

set container object’s reference null, child or contained object automatically becomeseligible for garbage collection.

I “The Garbage Collection Handbook: The Art of Automatic Memory Management"by Richard Jones, Anthony Hosking, and Eliot Moss.

8

Page 9: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Manual vs automated memory management

Manual allocation and de-allocation1 ptr=malloc(256 bytes);2 /* use ptr */3 free(Ptr);

I manual memory management: let programmer decide when objects are deleted.I automatic memory management: let a garbage collector delete objects.

Problems of manual managementI Manual memory management creates severe debugging problems

I Memory leaks,I Dangling pointers.

I In large projects where objects are shared between various components it isometimes difficult to tell when an object is not needed anymore.

I Considered the big debugging problem of the 80’s

9

Page 10: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Solution: Automatic Memory Reclamation

users allocate space for an object1 Account account = new SavingsAccount("1234567");23 for (File f : files) {4 String s = f.getName();5 }

system collects the space when not usedI When the system “knows" the object will not be used anymore, it reclaims its

space.I Telling whether an object will be used after a given line of code is undecidable.I Therefore, a conservative approximation is used.I An object is reclaimed when the program has “no way of accessing it".I Formally, when it is unreachable by a path of pointers from the “root" pointers, to

which the program has direct access.I Local variables, pointers on stack, global (class) pointers, JNI pointers, etc.I It is also possible to use code analysis to be more accurate sometimes.

10

Page 11: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

What’s good about automatic “garbage collection"?

Software engineeringI Relieves users of the book-keeping burden.I Stronger reliability, fewer bugs, faster debugging.I Code understandable and reliable. (Less interaction between modules.)

Security (Java)Program never gets a pointer to ”play with”.

11

Page 12: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

GC and languages

Sometimes it’s built in:I LISP, Java, C #.I The user cannot free an object.

Sometimes it’s an added feature:I C, C++.I User can choose to free objects or not. The collector frees all objects not freed by

the user.

Most modern languages are supported by garbage collection.

12

Page 13: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

what is bad about automated GC

It has a cost:I Old Lisp systems 40%.I Today’s Java program (if the collection is done “right") 5-15%.I Considered a major factor determining program efficiency.I Techniques have evolved since the 60’sI We will only go over basic techniques.

How to evaluate GCI Throughput: Overall collection overheads.I Responsiveness: Pauses in program run.I Space overhead.I Cache Locality (efficiency and energy).

13

Page 14: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Performance

I Responsiveness: refers to how quickly an application or system responds with arequested piece of data.

I How quickly a desktop UI responds to an eventI How fast a website returns a pageI How fast a database query is returnedI For applications that focus on responsiveness, large pause times are not acceptable.

The focus is on responding in short periods of time.I Throughput: focuses on maximizing the amount of work by an application in a

specific period of time. Examples of how throughput might be measured include:I The number of transactions completed in a given time.I The number of jobs that a batch program can complete in an hour.I The number of database queries that can be completed in an hour.I High pause times are acceptable for applications that focus on throughput. Since high

throughput applications focus on benchmarks over longer periods of time, quickresponse time is not a consideration.

14

Page 15: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Three classic GC algorithms

I Reference countingI Mark and sweep (and mark-compact)I Copying

The last two are also called tracing algorithms because they go over (trace) allreachable objects.

15

Page 16: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Outline

Overview of Garbage Collection

Reference counting

16

Page 17: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Reference counting (Collins 1960)

Reference Counting Algorithm

Each object has an RC field;new objects get o.RC:=1 ;if p that points to o1 is modified to point to o2 then

o1.RC–;o2.RC++. ;

endif o1.RC==0 then

Delete o1;Decrement o.RC for all children of o1 ;Recursively delete objects whose RC is decremented to 0 ;

end

1 Account x, y;2 x = new SavingsAccount("1234567");3 y = x;4 x = null;5 y = x;

rc for the object: 1 → 2 → 1 → 0

17

Page 18: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Reference counting

I Recall that we would like to know if an object is reachable from the roots.I Associate a reference count field with each object: how many pointers reference

this object.I When nothing points to an object, it can be deleted.I Very simple, used in many systems.

18

Page 19: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

class LinkedList {LinkedList next;

}int main() {

LinkedList A = new LinkedList;LinkedList B = new LinkedList;LinkedList C = new LinkedList;A.next = B;B.next = C;B = C = null;A.next.next = null;A = null;

}

19

Page 20: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Tracing the Reference-count algorithm

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

B = C = null;

A.next.next = null;

A = null;

varA

objArc=1

varB objBrc=

varC

objCrc=

20

Page 21: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Tracing the Reference-count algorithm

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

B = C = null;

A.next.next = null;

A = null;

varA

objArc=1

varB objBrc=1

varC

objCrc=

21

Page 22: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Tracing the Reference-count algorithm

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

B = C = null;

A.next.next = null;

A = null;

varA

objArc=1

varB objBrc=1

varC

objCrc=1

22

Page 23: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Tracing the Reference-count algorithm

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

B = C = null;

A.next.next = null;

A = null;

varA

objArc=1

varB objBrc=2

varC

objCrc=1

23

Page 24: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Tracing the Reference-count algorithm

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

B = C = null;

A.next.next = null;

A = null;

varA

objArc=1

varB objBrc=2

varC

objCrc=2

24

Page 25: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Tracing the Reference-count algorithm

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

B = C = null;

A.next.next = null;

A = null;

varA

objArc=1

varB objBrc=1

varC

objCrc=1

25

Page 26: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Tracing the Reference-count algorithm

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

B = C = null;

A.next.next = null;

A = null;

varA

objArc=1

varB objBrc=1

varC

objCrc=0

26

Page 27: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Tracing the Reference-count algorithm

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

B = C = null;

A.next.next = null;

A = null;

varA

objArc=0

varB objBrc=1

varC

objCrc=0

27

Page 28: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Tracing the Reference-count algorithm

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

B = C = null;

A.next.next = null;

A = null;

varA

objArc=0

varB objBrc=0

varC

objCrc=0

28

Page 29: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

The problem of Reference-counting

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

C.next = A;

A = null;

B= null;

C = null;

varA

objArc=1

varB objBrc=

varC

objCrc=

29

Page 30: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

The problem of Reference-counting

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

C.next = A;

A = null;

B= null;

C = null;

varA

objArc=1

varB objBrc=1

varC

objCrc=

30

Page 31: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

The problem of Reference-counting

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

C.next = A;

A = null;

B= null;

C = null;

varA

objArc=1

varB objBrc=1

varC

objCrc=1

31

Page 32: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

The problem of Reference-counting

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

C.next = A;

A = null;

B= null;

C = null;

varA

objArc=1

varB objBrc=2

varC

objCrc=1

32

Page 33: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

The problem of Reference-counting

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

C.next = A;

A = null;

B= null;

C = null;

varA

objArc=1

varB objBrc=2

varC

objCrc=2

33

Page 34: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

The problem of Reference-counting

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

C.next = A;

A = null;

B= null;

C = null;

varA

objArc=2

varB objBrc=2

varC

objCrc=2

34

Page 35: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

The problem of Reference-counting

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

C.next = A;

A = null;

B= null;

C = null;

varA

objArc=1

varB objBrc=2

varC

objCrc=2

35

Page 36: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

The problem of Reference-counting

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

C.next = A;

A = null;

B= null;

C = null;

varA

objArc=1

varB objBrc=1

varC

objCrc=2

36

Page 37: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

The problem of Reference-counting

LinkedList A = new LinkedList;

LinkedList B = new LinkedList;

LinkedList C = new LinkedList;

A.next = B;

B.next = C;

C.next = A;

A = null;

B= null;

C = null;

varA

objArc=1

varB objBrc=1

varC

objCrc=1

37

Page 38: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

A problem for cycles

I The Reference counting algorithm does not reclaim cycles.I Solution 1: ignore cycles, they do not appear frequently in modern programs;I Solution 2: run tracing algorithms (that can reclaim cycles) infrequently;I Solution 3: designated algorithms for cycle collection;

I Other algorithms?I Observation: rather than explicitly keep track of the number of references to each

object we can traverse all reachable objects and discard unreachable objects

38

Page 39: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Outline

Overview of Garbage Collection

Reference counting

39

Page 40: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

(Naive) Mark-and-Sweep Algorithm [McCarthy 1960]

Mark phase:I Start from roots and traverse all objects reachable by a path of pointers.I Mark all traversed objects.

Sweep phase:I Go over all objects in the heap.I Reclaim objects that are not marked.

(animated figure from Wikipedia)

40

Page 41: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

When GC starts

Garbage collection is triggered by allocation.

TriggeringAlgorithm 1: New(A)

if freeList is empty thenmarkSweep() ;if freeList is empty then

return (“out-of-memory")endpointer = allocate(A);return (pointer);

end

I

41

Page 42: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Properties of Mark-and-Sweep

Complexity:I Mark phase: live objectsI Sweep phase: heap size.I Termination: each pointer traversed once.

I Most popular method today (at a more advanced form). Simple.I Does not move objects, and so heap may fragment.I Various engineering tricks are used to improve performance.

42

Page 43: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Mark and compact

I During the run objects are allocated and reclaimed.I Gradually, the heap gets fragmented.I When space is too fragmented to allocate, a compaction algorithm is used.I Move all live objects to the beginning of the heap and update all pointers to

reference the new locations.I Compaction is considered very costly and we usually attempt to run it infrequently,

or only partially.

43

Page 44: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Compacting is a costly operation

44

Page 45: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Outline

Overview of Garbage Collection

Reference counting

45

Page 46: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Copying

I Heap partitioned into two.I Part 1 takes all allocations.I Part 2 is reserved.I During GC, the collector traces all reachable objects and copies them to the

reserved part.I After copying the parts roles are reversed:

I Allocation activity goes to part 2, which was previously reserved.I Part 1, which was active, is reserved till next collection.

46

Page 47: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Properties of Copying

I Compaction for freeI Major disadvantage: half of the heap is not used.I “Touch" only the live objectsI Good when most objects are dead.I Usually most new objects are dead, and so there are methods that use a small

space for young objects and collect this space using copying garbage collection.

47

Page 48: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

copying vs. mark-sweep

I Copying is good when live space is small (time) and heap is small (space).I A popular choice: Copying for the (small) young generation. Mark-and-sweep for

the full collection.I A small waste in space, high efficiency.

48

Page 49: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Comparison

Reference Counting Mark & Sweep CopyingComplexity Pointer updates +

dead objectsSize of heap (Liveobjects)

Live objects

Space overhead Count/object Bit/object Half heap wastedCompaction Additional work Additional work For freePause time Mostly short long Medium

More issues Cycle collection

49

Page 50: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Outline

Overview of Garbage Collection

Reference counting

50

Page 51: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Generational Garbage Collection

“The weak generational hypothesis":most objects die young.

51

Page 52: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Generational GC

I Using the hypothesis: separate objects according to their ages and collect thearea of the young objects more frequently.

I The heap is divided into two or more areas (generations).I Objects allocated in 1st (youngest) generation.I The youngest generation is collected frequently.I Objects that survive in the young generation “long enough" are promoted to the

old generation.

52

Page 53: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Advantages

I Short pauses: the young generation is kept small and so most pauses are short.I Efficiency: collection efforts are concentrated where many dead objects exists.I Less fragments:

I young and small objects might cause fragments. They are cleaned out in the younggeneration area.

I old space becomes more compactI Locality:

I Collector: mostly concentrated on a small part of the heap;I Program: allocates (and mostly uses) young objects in a small part of the memory.

53

Page 54: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

GC in JavaI New objects are allocated using a modified stop-and-copy collector in the Eden

space.I When Eden runs out of space, the stop-and-copy collector moves its elements to

the survivor space.I Objects that survive long enough in the survivor space become tenured and are

moved to the tenured space.I When memory fills up, a full garbage collection (perhaps mark-and-sweep) is used

to garbage-collect the tenured objects.

54

Page 55: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Java GC Tuning

55

Page 56: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

-Xms, -Xmx, -Xmn

public class gc{public static void main(String[] args) throws Exception{

int m=100000;int n=10000;long start = System.currentTimeMillis();Double [][] A=new Double [n][m];long end = System.currentTimeMillis();System.out.println(end-start);

I What heap memory we should request?I -Xms is the minimal heap size

jianguos-MBP:code jianguolu$ java -Xms1g gcException in thread "main" java.lang.OutOfMemoryError: Java heap space

at gc.main(gc.java:7)

Jianguos-MBP:code jianguolu$ java -Xmn1g gc15817

56

Page 57: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

-Xms, -Xmx, -Xmn

public class gc{public static void main(String[] args) throws Exception{

int m=100000;int n=10000;long start = System.currentTimeMillis();Double [][] A=new Double [n][m];long end = System.currentTimeMillis();System.out.println(end-start);

I What heap memory we should request?I -Xms is the minimal heap size

jianguos-MBP:code jianguolu$ java -Xms1g gcException in thread "main" java.lang.OutOfMemoryError: Java heap space

at gc.main(gc.java:7)

Jianguos-MBP:code jianguolu$ java -Xmn1g gc15817

57

Page 58: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

-Xms, -Xmx, -Xmn

public class gc{public static void main(String[] args) throws Exception{

int m=100000;int n=10000;long start = System.currentTimeMillis();Double [][] A=new Double [n][m];long end = System.currentTimeMillis();System.out.println(end-start);

I What heap memory we should request?I -Xms is the minimal heap size

jianguos-MBP:code jianguolu$ java -Xms1g gcException in thread "main" java.lang.OutOfMemoryError: Java heap space

at gc.main(gc.java:7)

Jianguos-MBP:code jianguolu$ java -Xmn1g gc15817

58

Page 59: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Excessive GC Time

I Why OutOfMemoryError ?I The parallel collector throws an OutOfMemoryError if too much time is being spent

in garbage collection (GC)I If more than 98% of the total time is spent in garbage collection and less than 2%

of the heap is recovered, then an OutOfMemoryError is thrown.I This feature is designed to prevent applications from running for an extended

period of time while making little or no progress because the heap is too small.I If necessary, this feature can be disabled by adding the option

-XX:-UseGCOverheadLimit to the command line.

59

Page 60: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Outline

Overview of Garbage Collection

Reference counting

60

Page 61: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Concurrent GC

Problem: Long pauses disturb the userI An important measure for the collection: length of pauses.I average pause time for compacting 1GB live data: one second

I Can we just run the program while the collector runs on a different thread?I Not so simple!I The heap changes while we collect.I For example, we look at an object B, but before we have a chance to mark its

children, the program changes them.

61

Page 62: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Memory Management with Parallel Processors

I Multiple threads are used to speed up garbage collection.I Processor cores run GC in parallelI Normally uses Stop the world GC for younger generations

I -XX:-UseParallelGC

I Use parallel garbage collection, mostly for younger generations

I -XX:-UseParallelOldGC

I Use parallel garbage collection for the full collections.I Enabling this option automatically sets -XX:+UseParallelGC.

I Different from Concurrent GC.

62

Page 63: Comp-4400: Garbage Collectionjlu.myweb.cs.uwindsor.ca/440/440gc2019.pdf · What is garbage collection I Memory Management technique. ... I Today’s Java program (if the collection

Takeaways

I What is the run time management, stack and heap.I How to manage the memory, automated GCI Classic GC algorithms: Reference counting, Mark and Sweep, Copying.

I Reference counting: cyclic reference problem.I Mark and Sweep: needs to stop the machine; entire heap needs to be visited;

compacting is costly.I Copying: half of the heap is wasted.

I Generational GCI GC in java

63