21
Garbage Collection 1 C# 30 C# 3.0 C# 3.0 Chapter 5 - Garbage Collection © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel In This Chapter Garbage Collection 2 C# 30 In This ChapterA tomatic Memor Management Automatic Memory Management Mark and Sweep Mark and Sweep Finalization (Cleanup Code) Deterministic Finalization G ti l GC Generational GC Weak References Weak References • Interacting with the GC © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Chapter 5 - Garbage Collection

Embed Size (px)

DESCRIPTION

a

Citation preview

Page 1: Chapter 5 - Garbage Collection

Garbage Collection 1C# 30

C# 3.0C# 3.0

Chapter 5 - Garbage Collection

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

In This ChapterGarbage Collection 2C# 30

In This Chapter…

A tomatic Memor Management• Automatic Memory Management

• Mark and SweepMark and Sweep

• Finalization (Cleanup Code)

• Deterministic Finalization

G ti l GC• Generational GC

• Weak References• Weak References

• Interacting with the GCg

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 2: Chapter 5 - Garbage Collection

Is There a Destructor in C# ?Garbage Collection 3C# 30

Is There a Destructor in C# ?

In attempt to ans er this q estion let’s• In attempt to answer this question, let’s start with a simple experiment:p p

• We will define a (pseudo) file class whereWe will define a (pseudo) file class where its objects represent text files

N t ll fil ill b d i th l t t– Naturally, files will be opened in the class constructor

– To make sure files will be closed as soon as objects are out of scope, you must provide the class with a destructor that closes the file

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Is There a Destructor in C# ?Garbage Collection 4C# 30

Is There a Destructor in C# ? introduction.csintroduction.cs

1. class TestFile {

bli il ( i )2. public TestFile(string name)

3. {

4. Console.WriteLine("Opening " + name);( p g );

5. this.name = name;

6. }

7 bli   id P Titl (){// }7. public void ProcessTitle(){//…}

8. public void ProcessAParagraph(){//…}

9. ~TestFile()()

10. {

11. Console.WriteLine("Closing " + name);

12 }12. }

13. private string name;

14.}

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

}

Page 3: Chapter 5 - Garbage Collection

Is There a Destructor in C# ?Garbage Collection 5C# 30

Is There a Destructor in C# ?

We are er pleased to find o t that o r• We are very pleased to find out that our first short definition compiles.p– It seems that C# supports destructors!

• Following is the application code that uses our TestFile class:our TestFile class:

Introduction.cs – cont’d

1. using System;2. class FileApp3 {3. {4. static void Main()5. {6 F 1()

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

6. Func1();7. }

Is There a Destructor in C# ?Garbage Collection 6C# 30

Is There a Destructor in C# ?

bli i id () {1. public static void Func1() {

2. Console.WriteLine("Enter function Func1()");

3. TestFile f1 = new TestFile("file1.txt");3. est e e est e( e .t t );

4. f1.ProcessTitle();

5. f1.ProcessParagraph();

6 F 2()6. Func2();

7. Console.WriteLine("Leave function Func1()");

8. }}

9. public static void Func2() {

10. Console.WriteLine("Enter function Func2()");

11 T tFil f2     T tFil ("fil 2 t t")11. TestFile f2 = new TestFile("file2.txt");

12. f1.ProcessTitle();

13. f1.ProcessParagraph();g p ();

14. Console.WriteLine("Leave function Func2()");

15. }

16 }

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

16.}

Page 4: Chapter 5 - Garbage Collection

Is There a Destructor in C# ?Garbage Collection 7C# 30

Is There a Destructor in C# ?

• Now we execute our FileApp application• Now we execute our FileApp application with the following results:g

Enter Main()Enter function Func1() Enter function Func1() Opening file1.txtEnter function Func2()Enter function Func2()Opening file2.txtLeave function Func2()Leave function Func2()Leave function Func1()Leave Main()Leave Main()Closing file2.txtClosing file1.txt

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C os g e .t t

Is There a Destructor in C# ?Garbage Collection 8C# 30

Is There a Destructor in C# ?

What’s going on here?• What’s going on here?– Does C# support destructors, or doesn’t it?

• Why were the files not closed on the functions’ exit?functions’ exit?– Why were they closed at the end of the program?

• This is one of the fundamental differences between C++ and C# the issue of thebetween C++ and C# - the issue of the object’s finalizationj

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 5: Chapter 5 - Garbage Collection

NET as a Managed EnvironmentGarbage Collection 9C# 30

.NET as a Managed Environment

• Memory management has always been a source for bugs such as:source for bugs, such as:– Forgetting to free memory (memory leaks)

Trying to access memory that was already freed– Trying to access memory that was already freed

• The major problem with these type of bugsThe major problem with these type of bugs is that they cause your application to break:break:– In an unpredictable way

At di t bl ti– At an unpredictable time

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

NET as a Managed EnvironmentGarbage Collection 10C# 30

.NET as a Managed Environment

All (reference t pe) objects are created on• All (reference type) objects are created on the heapp– But developers are absolved of tracking memory

usage as well as freeing memory when no longerusage as well as freeing memory when no longer used

• .NET provides a mechanism that is in p o des a ec a s a scharge of memory management

It i ibl f f i bj t ’ hi h i– It is responsible for freeing objects’ memory which is no longer used

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 6: Chapter 5 - Garbage Collection

NET as a Managed EnvironmentGarbage Collection 11C# 30

.NET as a Managed Environment

This mechanism is the NET Garbage• This mechanism is the .NET Garbage Collector (GC)( )

– Let’s start with a general garbage collection discussion, leading to the .NET garbage collector d i tidescription

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Garbage Collector Design GoalsGarbage Collection 12C# 30

Garbage Collector Design Goals

Man implementations of GC e ist• Many implementations of GC exist– Copying, tracing, reference-counting, …

• Their primary design goals are:– Performance:Performance:

• Garbage collected environments must be at least as fast a manually-managed environments

– Executing resource cleanup code:• When an object becomes unreferenced, we want cleanup j p

code to be executed before it is removed from memory

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 7: Chapter 5 - Garbage Collection

Object FinalizationGarbage Collection 13C# 30

Object Finalization

• When an object is terminated its memory• When an object is terminated, its memory should be released– This is automatically performed by the GC

• Other resources that should be releasedOther resources that should be released on an object’s termination:

Exclusive access files data base connections– Exclusive-access files, data-base connections, network connections, etc.

We usually require the object’s finalization• We usually require the object’s finalization code to be executed as soon as the object is no longer in use– Do we get this deterministic finalization in C++?

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

g

Object FinalizationGarbage Collection 14C# 30

Object Finalization

Can a collection algorithm can promise• Can a collection algorithm can promise anything regarding the object’s finalization y g g g jtiming?

Will the finalization code be executed immediately– Will the finalization code be executed immediately after an object is no longer in use?

– Having these collection topics in mind, let’s examine g p ,collecting algorithms and see how they handle these issues

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 8: Chapter 5 - Garbage Collection

Reference Counting CollectionGarbage Collection 15C# 30

Reference Counting Collection

Each object has a reference co nt field• Each object has a reference count field:– When a reference is added, the RC is incremented

– When a reference is removed, the RC is decremented

– An object is no longer in use, (and therefore can beAn object is no longer in use, (and therefore can be collected), when its RC reaches 0

Promises deterministic object finalization:• Promises deterministic object finalization:– An object will be collected the moment it is no longer

in use

– If an object has finalization code defined it will be jexecuted before it is collected

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Reference Counting CollectionGarbage Collection 16C# 30

Primary Disadvantages

C cles are not collected• Cycles are not collected:– Object A refers to object B, which in turn, refers to A

• Storing a reference count for each and every object badly affects performanceevery object badly affects performance– Every object must contain an additional field

E i l i t ti i th– Every simple assignment operation in the program turns into a series of machine operations

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 9: Chapter 5 - Garbage Collection

The .NET Garbage CollectorGarbage Collection 17C# 30

Tracing Garbage Collection• Objects no longer used are collected when• Objects no longer used are collected when

the GC sees fit– When memory runs short, when a threshold is

reached, when …

• However no promise is made on the• However, no promise is made on the object’s collection timing– Less appropriate for resources that need to be

released in a timely manner and in a predictable order

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

The NET Garbage CollectorGarbage Collection 18C# 30

The .NET Garbage Collector

In the follo ing NET GC disc ssion e• In the following .NET GC discussion, we will:

– Provide a short description of the NET garbage– Provide a short description of the .NET garbage collector’s inner workings

– Dig deeper into the object’s finalization and cleanup iissues

– Examine .NET GC optimizations

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 10: Chapter 5 - Garbage Collection

Object AllocationGarbage Collection 19C# 30

Object Allocation

• On application initialization:• On application initialization:– The runtime reserves a region of space called the

managed heapmanaged heap– The runtime initializes a pointer to the next object

allocation (next object pointer)allocation (next object pointer)

• Whenever an object is created:– The new operator checks if there is enough space in

the heap for the new object– Its constructor is executed– The new operator returns the object’s address– The pointer is incremented past the new object’s end

address

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Object Allocation and CollectionGarbage Collection 20C# 30

Object Allocation and Collection

• So far object’s allocation requires only• So far, object s allocation requires only incrementing the pointer value!– Can you compare it to an object allocation on the C-

runtime heap?

• At some point the GC decides that a• At some point, the GC decides that a collection is necessary– It checks whether there are objects that are no longer

in-use by the applicationS h bj t b ll t d th i b– Such objects can be collected: their memory can be reclaimed

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 11: Chapter 5 - Garbage Collection

The Mark PhaseGarbage Collection 21C# 30

The Mark Phase

• How does the GC find the non used• How does the GC find the non-used objects?

• Any application has a set of active roots– Static object references– Static object references

– Local objects in “currently active methods”

• The GC starts traversing the set of active roots building a graph of all reachableroots, building a graph of all reachable objects from the roots– All objects that are not in the graph can be considered

“garbage”

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

The Sweep PhaseGarbage Collection 22C# 30

The Sweep Phase

The GC finds gro ps of dead objects and• The GC finds groups of dead objects and compacts live objects togetherp j g

• This requires correcting all object references to point to their object’s newreferences to point to their object s new location

The next object pointer is updated• The next object pointer is updated

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 12: Chapter 5 - Garbage Collection

FinalizationGarbage Collection 23C# 30

Finalization

A class can define a finali ation method• A class can define a finalization method– This finalization method can be used for resource

cleanup operations

– Will be called at some point before the object’s p jmemory is reclaimed

• Finalization code should be defined in a ~ClassName method

Mapped to an override of the Finalize()protected– Mapped to an override of the Finalize()protected method defined in Object

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Finalization CaveatsGarbage Collection 24C# 30

Finalization Caveats

The C# finali er is different from the C++• The C# finalizer is different from the C++ destructor– The C++ destructor is automatically executed on an

object’s terminationobject s termination

– But you can never know when the C# finalizer will be executedexecuted

– You cannot rely on the C# destructor for releasing limited resourceslimited resources

• Finalization is also a performance hit– It increases allocation time and prolongs object

lifetime

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 13: Chapter 5 - Garbage Collection

Finalization InternalsGarbage Collection 25C# 30

Finalization Internals

Finali able objects are inserted into the• Finalizable objects are inserted into the finalization queue when createdq

• When the object is no longer referenced, the GC moves it to the f-reachable queuethe GC moves it to the f reachable queue

• The finalizer thread executes the finalizer

• Eventually the object is collected© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

• Eventually, the object is collected

Beyond FinalizationGarbage Collection 26C# 30

Beyond Finalization

Be caref l hen sing finali ers• Be careful when using finalizers– The finalization method should be as simple as

possible, to avoid memory leaks, data races and other problems

• How should we handle objects that require• How should we handle objects that require their finalization code to be executed as soon as the object is no longer in use?

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 14: Chapter 5 - Garbage Collection

Deterministic FinalizationGarbage Collection 27C# 30

Deterministic Finalization

Add a method called Dispose and• Add a method called Dispose, and perform cleanup in itp p– Contractual support: The client must remember to call

the methodthe method

– Implementing the IDisposable interface

Th fi li i l ll Di• The finalizer can simply call Dispose– But this isn’t enough!g

• Call GC.SuppressFinalize to prevent d bl di d f ltdouble dispose and a perf penalty

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Deterministic FinalizationGarbage Collection 28C# 30

Deterministic Finalization

Follo s an enhanced ersion of o r• Follows an enhanced version of our TestFile class, implementing the dispose pattern

It’s not complete: It doesn’t call the base class for– It s not complete: It doesn t call the base class, for example (consult MSDN for the complete pattern)

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 15: Chapter 5 - Garbage Collection

Deterministic FinalizationGarbage Collection 29C# 30

Sample

class TestFile : IDisposable {public TestFile(string name) {

C l W it Li ("O i  {0}\ "   )Console.WriteLine("Opening {0}\n", name);this.name = name;

}}//… Methods omitted for brevitypublic void Dispose() {

C l W it Li ("Cl i  {0}\ "    )Console.WriteLine("Closing {0}\n",  name);GC.SuppressFinalize(this);

}}~TestFile() {

Dispose();}}private string name;

}

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

}

Deterministic FinalizationGarbage Collection 30C# 30

Client Code

As sers of the class e m st remember• As users of the class, we must remember to call the Dispose method:p

void Func1(){

Console.WriteLine("Enter function Func1()");TestFile f1 = new TestFile("file1 txt");TestFile f1 = new TestFile( file1.txt );f1.ProcessTitle();f1.ProcessAParagraph();Func2();f1.Dispose();Console WriteLine("Leave function func1()");Console.WriteLine( Leave function func1() );

}

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 16: Chapter 5 - Garbage Collection

Deterministic FinalizationGarbage Collection 31C# 30

Client Code• To ensure that the object is always• To ensure that the object is always

disposed (including exceptions thrown), th i t t tuse the using statement:

bli   t ti   id F 2() {public static void Func2() {Console.WriteLine("Enter function Func2()");using (TestFile f2 = new TestFile("file2.txt")) {g ( ( )) {

f2.ProcessTitle();f2.ProcessAParagraph();

}}Console.WriteLine("Leave function Func2()");

}

Applicable to IDisposable objects only

}

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

– Applicable to IDisposable objects only

Finalization and Value TypesGarbage Collection 32C# 30

Finalization and Value Types

GC is irrele ant for al e t pes• GC is irrelevant for value types– Value-type variables are removed from the stack

when leaving their scope

• The C# compiler does not allow d t t l tdestructors on value types– Pro: It is never collected and therefore its finalization

method can never be invoked

– Con: The CLR actually allows it, because boxed value y ,types can be collected

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 17: Chapter 5 - Garbage Collection

Generational Garbage CollectionGarbage Collection 33C# 30

Generational Garbage Collection

When a GC occ rs the application takes a• When a GC occurs, the application takes a major performance hitj p

• Traversing the entire heap for dead objects is inefficientobjects is inefficient– Therefore, the heap is divided into generations. This

enables a partial collection process with higherenables a partial collection process with higher collection efficiency

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Generational ModelGarbage Collection 34C# 30

Generational Model

The generational model is based on the• The generational model is based on the following assumptions:g p

– The newer an object is the shorter its lifetime will– The newer an object is, the shorter its lifetime will be (e.g., a utility method’s local objects)

– The older an object is, the longer its lifetime will b ( t ti i l t bj t )be (e.g., static singleton objects)

– Collecting a portion of the heap is faster than collecting the whole heap

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 18: Chapter 5 - Garbage Collection

GenerationsGarbage Collection 35C# 30

Generations

• New objects are added to generation 0• New objects are added to generation 0– A small memory region (~1MB)

• When generation 0 fills, a garbage collection occurs within generation 0collection occurs within generation 0– Objects that “survive” the garbage collection are

promoted to generation 1promoted to generation 1

• Generation 0 always contains objects younger than those of generation 1younger than those of generation 1– Partitioning objects by age enables the GC to use the

ti bassumptions above

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

GenerationsGarbage Collection 36C# 30

Generations

• A gen 0 collection is very efficient:• A gen 0 collection is very efficient:– Most objects to be collected will be located in this part

of the heap (young)of the heap (young)– Gen 0 is very small, so collection takes only a few ms

on modern hardwareon modern hardware

Wh ti 1 i f ll th GC ll t• When generation 1 is full, the GC collects generation 1 and promotes live objects g p jinto generation 2– Generation 2 holds the oldest program’s objects andGeneration 2 holds the oldest program s objects, and

will probably be rarely collected

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 19: Chapter 5 - Garbage Collection

Weak ReferencesGarbage Collection 37C# 30

Weak References

An application rarel ses a large object• An application rarely uses a large object– However, there is always a chance that it will be

needed at some point

• The question is: Sh ld h bj f b k if i i d– Should the object’s reference be kept, if it is reused later on?

– Should it be released, to improve memory management performance?

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Weak ReferencesGarbage Collection 38C# 30

Weak References

Setting a eak reference to an object• Setting a weak reference to an object means it can be collected if a GC occurs

• If a GC hasn’t occurred, the weak reference can be converted back into areference can be converted back into a regular reference

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 20: Chapter 5 - Garbage Collection

Weak Reference ExampleGarbage Collection 39C# 30

Weak Reference Example

HugeClass hugeObj = new HugeClass();HugeClass hugeObj = new HugeClass();//...Manipulate hugeObj. // User switched to a different application part.// h bj t b i d i// hugeObj may or may not be required again, // Sets a weak reference to it:WeakReference weakRef = new WeakReference(hugeObj);( g j)hugeObj = null;// Now it can be collected, // however, be there if needed and no GC occurred …// however, be there if needed and no GC occurred …// hugeObj is again required by the application:hugeObj = (HugeClass)weakRef.Target;if (hugeObj == null) {if (hugeObj == null) {

// A GC occurred, hugeObj was collectedhugeObj = new HugeClass();

}}// Using hugeObj again Note:

IsAlive is not safe for

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

IsAlive is not safe for use! (race condition)

Interacting with the GCGarbage Collection 40C# 30

Interacting with the GC

S stem GC is the static class which• System.GC is the static class which represents the GCp– Provides a set of static methods for diagnostics (e.g.

memory usage) and control (e.g. forcing a collection)memory usage) and control (e.g. forcing a collection)

– Control methods are not recommended!

The GC has advanced heuristics of its own for– The GC has advanced heuristics of its own for managing memory

M t f th ti ’ll d h th d– Most of the time, you’ll do more harm than good

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Page 21: Chapter 5 - Garbage Collection

Some of the GC Class MethodsGarbage Collection 41C# 30

Some of the GC Class Methods

GC Method Description

C ll t() P f ll tiCollect() Performs a collection.

An overloaded version enables specifying the generation to collectgeneration to collect.

GetGeneration() Returns an object’s current generation.

M G tiMaxGeneration Returns the highest generation number.

SuppressFinalize() Sets a flag indicating that the Finalize() method should not be called for a given objectshould not be called for a given object.

GetTotalMemory() Returns the amount of memory (in bytes) currently being used by all objects in the heapcurrently being used by all objects in the heap.

A Boolean parameter indicates whether or not to perform a collection.

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

p

SummaryGarbage Collection 42C# 30

Summary

A tomatic Memor Management• Automatic Memory Management

• Mark and SweepMark and Sweep

• Finalization (Cleanup Code)

• Deterministic Finalization

G ti l GC• Generational GC

• Weak References• Weak References

• Interacting with the GCg

© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel