memory management & garbage collection

42
.NET Memory Management KolkataGeeks 1

Upload: abhishek-sur

Post on 10-May-2015

1.656 views

Category:

Technology


1 download

DESCRIPTION

This demonstrates how to manage memory and Garbage Collection in .NET

TRANSCRIPT

Page 1: Memory Management & Garbage Collection

.NET Memory Management

KolkataGeeks 1

Page 2: Memory Management & Garbage Collection

• To perform efficient memory manipulation in performance critical system, we need to have a good understanding of memory management.

KolkataGeeks 2

Page 3: Memory Management & Garbage Collection

Value Data Types

• The mapping from memory address seen by our program to the actual location in hardware memory is fully managed by Windows using a system known as "Virtual Adressing".

• Each process on a 32-bit processor sees 4-GB of available memory.

KolkataGeeks 3

Page 4: Memory Management & Garbage Collection

• This 4GB of memory is called as “Virtual Address space or Virtual Memory”.

• Stack is an area, inside Virtual Memory.• 1. It stores values data types.• 2. Stack is used to hold a copy of any

parameters passed to the method.

KolkataGeeks 4

Page 5: Memory Management & Garbage Collection

KolkataGeeks 5

Page 6: Memory Management & Garbage Collection

KolkataGeeks 6

Page 7: Memory Management & Garbage Collection

• A stack pointer is a variable maintained by the Operating System.

• When our program first starts running, the stack pointer will point to just past the end of the block of memory that is reserved for the stack. The stack actually fills downward from high memory addresses to low memory addresses.

• The stack pointer is adjusted according to the data put on it.

• It always points to just past the next free location.

KolkataGeeks 7

Page 8: Memory Management & Garbage Collection

Reference Data Types

• Often we need to use a method to allocate memory for storing data and keeping that data available long after that method has exited.

• Whenever storage space is requested with the new operator i.e., for all reference types, such cases arises.

• Here before proceeding, I need to explain the term Managed Heap.

KolkataGeeks 8

Page 9: Memory Management & Garbage Collection

• The Managed Heap is another area of memory, from the processors available 4 GB.

• The new operator creates an object. This operator first confirms about the region present in the managed heap. If there is sufficient space, the pointer points to the object in the heap. And the new operator returns the address of the object.

KolkataGeeks 9

Page 10: Memory Management & Garbage Collection

• Suppose there are 2 objects in the managed heap. And the pointer is pointing NewSpacePtr. If a new request for allocation of memory comes, the heap adds the size of the new object to NewSpacePtr. If(Size ofnew object+NewSpacePtr)>end of address space, then the heap has no free space and the garbage collection must be started.

Object X

Object Y

NewSpacePtr

Enough Space

Managed Heap

KolkataGeeks 10

Page 11: Memory Management & Garbage Collection

Object X

Object Y

Object ZNewSpacePtr

100

• When the pointer NewSpacePtr is pointing to 300, if any new object creation request comes, it will calculate the size of the address space. And it will ultimately call GC.

310

200

300

Call GC!

Managed Heap

KolkataGeeks 11

Page 12: Memory Management & Garbage Collection

Large Objec

t Heap

Small Objec

t Heap

Managed Heap

Contains objects that are 85,000 bytes larger

<85,000 bytes

KolkataGeeks 12

Page 13: Memory Management & Garbage Collection

KolkataGeeks 13

• The following code explain the working of heap and memory allocation for reference data types:

Page 14: Memory Management & Garbage Collection

• Line 1At first I have declared a Speaker reference called sp1. Space for this is allocated in the stack. sp1 reference takes up 4 bytes, to hold the address at which a speaker object will be stored.

• Line 2• Allocates memory on the heap to store a Speaker

object.• It sets the value of the variable sp1 to the address of

the memory it has allocated to the new speaker object.

KolkataGeeks 14

Page 15: Memory Management & Garbage Collection

KolkataGeeks 15

Page 16: Memory Management & Garbage Collection

KolkataGeeks 16

• Line 3Declares a Speaker reference.Instantiates a MVPSpeaker object.

Page 17: Memory Management & Garbage Collection

Garbage Collection• Garbage Collector removes all those objects from the heap

that are no longer referenced. Hence, after GC runs, objects and free space are scattered in the heap.

• If this memory is allowed to be kept in this way, the runtime will have to search the heap, for a block of memory, in order to create a new object.

• Luckily, .Net GC doesn’t keep the heap like this. It compacts the heap by removing all remaining objects to form one continuous block of memory.

• When the objects have moved, their references are to be updated in the stack. This work is also handled by the garbage collector.

KolkataGeeks 17

Page 18: Memory Management & Garbage Collection

Advantages of Managed Heap

• In order to put data into the managed heap, the value of the heap pointer is needed to be read. Hence, under .NET, instantiating an object is much faster.

• Accessing objects tend to be faster too. The reason is the objects are compacted towards the same area of memory, on the heap.

KolkataGeeks 18

Page 19: Memory Management & Garbage Collection

GC Generations

KolkataGeeks 19

Page 20: Memory Management & Garbage Collection

• The Garbage Collector performs a collection on generation 0, only when generation 0 is full.

• This situation occurs when a request for creation of new object is made, and there is no sufficient memory to allocate for that object.

• This whole process is enhancing the performance of our application:

a. Younger objects are collected by the GC.b. If these younger objects reside next to each other in the

heap, then the garbage collection process will be faster.c. If the related objects are residing next to each other, in the

heap, the program execution will be faster.

KolkataGeeks 20

Page 21: Memory Management & Garbage Collection

How to free unmanaged resources• We don’t worry about the unused objects, because GC frees

memory as required. But unfortunately, GC does not know how to free unmanaged resources (ie., file handlers, database connections, network connections, etc).

• Hence, we are required to make provisions, so that unmanaged resources are released when an object of the class is destroyed.

• So while defining a class, we have to:a. Declare a destructor as a member of a class.b. Implement IDisposable Interface in our class.

KolkataGeeks 21

Page 22: Memory Management & Garbage Collection

Destructors

• GC call the destructor of an object, before destroying it. The syntax for a destructor is given beside.

• On compilation, the beside code gets translated into the equivalent of a Finalize() method, which ensures that the Finalize() is executed.

KolkataGeeks 22

Page 23: Memory Management & Garbage Collection

Code equivalent to the Finalize()

• The following code, shows the C# code equivalent to the IL generated by the compiler for the ~abc destructor.

• Here the method calls Finalize on its parent type and the parent type will call Finalize on its parent type.

• In this way, the whole hierarchy of our object is destroyed.

• So, we should have a destructor for our class, only if it is necessary, as on calling Finalize, GC puts the objects in the finalization queue to implement Finalize. And all these affects performance.

KolkataGeeks 23

Page 24: Memory Management & Garbage Collection

Pictorial representation of Finalization Process

KolkataGeeks 24

Page 25: Memory Management & Garbage Collection

Finalization takes more time• Objects that do not have a destructor are removed from

memory in one pass of the garbage collector.• Objects that have destructors requires 2 passes to be

destroyed.a. The first pass calls the destructor but doesn’t removes the

object.b. The second pass, destroys the object.c. The .NET runtime, uses a single thread to execute the

Finalize() method of all objects. This thread runs with high priority, and if many components need finalization, this thread will block threads with a lower priority from executing.

KolkataGeeks 25

Page 26: Memory Management & Garbage Collection

Disadvantages of Destructor

• Due to the way, the garbage collector works, it is not possible to know when an object’s destructor will actually execute.

• Just for this reason, we can’t place any code n the destructor, that needs to be run at a certain time.

• We also don’t know the order in which the objects will be destroyed.

• Final removal of an object from memory gets delayed due to the implementation of a destructor.

KolkataGeeks 26

Page 27: Memory Management & Garbage Collection

IDisposable Interface• The System.IDisposable interface is an alternative to a

destructor.• This interface provides a mechanism for freeing unmanaged

resources. It defines a single method named Dispose(), which takes no parameters and returns void. Dispose() explicitly frees all unmanaged resources used directly by an object.

KolkataGeeks 27

Page 28: Memory Management & Garbage Collection

• If an exception arises on the Open(), the Dispose() will never be called, in this case. So it is better to put it in try catch block.

• In the following example, the Dispose() will definitely be called, because this time, I have kept it within finally block.

KolkataGeeks 28

Page 29: Memory Management & Garbage Collection

• Using statement of C#, calls Dispose() on objects that implement IDisposable Interface.

• The using statement, followed in brackets by a reference variable declaration and instantiation, will cause that variable to be scoped to the accompanying statement block. When that variable goes out of scope, its Dispose() method will be called automatically, even if an exception occurs. Dispose() will be called on all objects, implementing IDisposable Interface, on exiting the using block.

KolkataGeeks 29

Page 30: Memory Management & Garbage Collection

IDisposable and Destructor used together

KolkataGeeks 30

Page 31: Memory Management & Garbage Collection

KolkataGeeks 31

Page 32: Memory Management & Garbage Collection

• As Dispose(), has already cleaned the unused objects(managed and unmanaged resources), there are nothing for the destructor to clean.

• On calling SupressFinalize() means, the garbage collector considers that this object has no destructor at all.

KolkataGeeks 32

Page 33: Memory Management & Garbage Collection

• An assembly that can access another assembly's members, is called a friend assembly.

• Using Friend Assembly, we can access internal type or internal member of an assembly.

• Private types and Private members are inaccessible.

• To access internal type or internal member of an assembly, we have to use InternalsVisibleTo attribute.

Friend Assembly

KolkataGeeks 33

Page 34: Memory Management & Garbage Collection

Friend Assembly

KolkataGeeks 34

Page 35: Memory Management & Garbage Collection

• Compile Class1.cs from the command prompt.• And build the ClassLibrary2 also.

KolkataGeeks 35

Page 36: Memory Management & Garbage Collection

Here we are referencing ClassLibrary2.

KolkataGeeks 36

Page 37: Memory Management & Garbage Collection

• As ClassLibrary2.Class1 has mentioned frnd_B within InternalsVisibleTo attribute, only this can access internal types and internals members of ClassLibrary2.Class1 .

KolkataGeeks 37

Page 38: Memory Management & Garbage Collection

• Frnd_B.exe is created in the D:\frnd\ClassLibrary2.

• This .exe is a friend of ClassLibrary2.dll

KolkataGeeks 38

Page 39: Memory Management & Garbage Collection

• On executing frnd_B.exe, we get this output:

KolkataGeeks 39

Page 40: Memory Management & Garbage Collection

• If we change an assembly’s AssemblyVersion, a new identity of it is created. Suppose I am writing an assembly, strongly signed and assigned in the GAC with its version “1.0.0.0”. After sometime, I am modifying it, now its version is “1.0.0.1”. Reinstalling it in GAC. Now GAC will store two different versions of assembly.

• The benefits are:• We can choose the version of the assembly. Which I

want to use.• Any existing application won’t get crushed, because

older version of assembly still exists.

GAC And Versioning

KolkataGeeks 40

Page 41: Memory Management & Garbage Collection

• This is called side-by-side execution. Side-by-Side execution prevents “DLL hell” that used to occur when any shared assembly gets updated and applications designed for the older version might stop working.

KolkataGeeks 41

Page 42: Memory Management & Garbage Collection

KolkataGeeks 42

Thank You All