garbage collection in .net (basic level)

32
GARBAGE COLLECTION IN .NET (BASIC LEVEL) Larry Nung

Upload: larry-nung

Post on 27-Jul-2015

54 views

Category:

Technology


0 download

TRANSCRIPT

GARBAGE COLLECTION IN .NET (BASIC LEVEL)

Larry Nung

AGENDA2

AGENDA

FunctionFeatureManaged HeapSmall Object Heap (SOH)Large Object Heap (LOH)Collection With Finalization QueueCollection TimingStrong reference & Week referenceGC ModeReferenceQ & A

3

FUNCTION4

FUNCTION

Automatic memory management Responsible for reclaiming the memory of no longer used

objects.

5

FEATURE6

FEATURE

Allow us to develop an application without having worry to free memory.

Allocates memory for objects efficiently on the managed heap.

Reclaims the memory for no longer used objects and keeps the free memory for future allocations.

Provides memory safety by making sure that an object cannot use the content of another object.

7

MANAGED HEAP8

MANAGED HEAP

A series of allocated memory segments (approx 16Mb in size each) to store and manage objects

9

SMALL OBJECT HEAP (SOH)10

GENERATIONS Generation 0

The youngest generation and contains short-lived objects.

About 256 KB Generation 1

Contains short-lived objects and serves as a buffer between short-lived objects and long-lived objects.

About 2MB Generation 2.

Contains long-lived objects. About 10MB

11

PROMOTIONS

Objects that are not reclaimed in a garbage collection are known as survivors, and are promoted to the next generation.

12

LARGE OBJECT HEAP (LOH)13

LARGE OBJECT HEAP (LOH)

Feature Never compacts

14

LARGE OBJECT HEAP (LOH)

15

COLLECTION WITH FINALIZATION QUEUE

16

COLLECTION WITH FINALIZATION QUEUE

17

COLLECTION WITH FINALIZATION QUEUE

18

COLLECTION WITH FINALIZATION QUEUE

19

COLLECTION WITH FINALIZATION QUEUE

20

COLLECTION TIMING21

COLLECTION TIMING

Low physical memory. The memory allocated on the managed heap

surpasses an acceptable threshold. Invoke System.GC.Collect.

22

STRONG REFERENCE & WEEK REFERENCE

23

STRONG REFERENCE & WEEK REFERENCE

Week reference A reference that does not protect the referenced

object from collection by a garbage collector.

24

STRONG REFERENCE & WEEK REFERENCEusing System;

public class Program {

public static void Main() {

var person1 = new Person("Larry");

var person2 = new WeakReference(null);

person2.Target = new Person("Jack");

GC.Collect();

var jack = person2.Target as Person;

Console.WriteLine(person1.Name);

Console.WriteLine(jack == null ? "(null)" : jack.Name);

}

}

public class Person {

public string Name { get; private set; }

public Person(string name) {

this.Name = name;

}

}

25

person1

person2

Larry

Heap

Jack

Strong Reference

Week Reference

person1 Larry

Heap

Strong Reference

GC

GC MODE26

GC MODE

Workstation GC Default GC mode.

Server GC Available only on multiprocessor computers. Creates a separate managed heap and thread for

each processor and performs collections in parallel.

ASP.NET and SQL Server enable.

27

REFERENCE28

REFERENCE

Understanding .NET Garbage Collection http://www.telerik.com/blogs/understanding-net-

garbage-collection

30

Q&A31

QUESTION & ANSWER

32