Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 9: Memory and Resource Management

Similar presentations


Presentation on theme: "Module 9: Memory and Resource Management"— Presentation transcript:

1 Module 9: Memory and Resource Management

2 Overview Memory Management Basics Non-Memory Resource Management
Implicit Resource Management Explicit Resource Management Optimizing Garbage Collection

3 Memory Management Basics
Developer Backgrounds Manual vs. Automatic Memory Management Memory Management of .NET Framework Types Simple Garbage Collection

4 Developer Backgrounds
COM Manually implement reference counting and handle circular references C++ Manually use the new operator and delete operator Visual Basic Accustomed to automatic memory management

5 Manual vs. Automatic Memory Management
Manual Memory Management Programmer manages memory Common Problems Failure to release memory Invalid references to freed memory .NET Runtime Provides Automatic Memory Management Eases programming task Eliminates a potential source of bugs

6 Memory Management of .NET Framework Types
Instances of Value Types Use Stack Memory Allocation and deallocation are automatic and safe Managed Objects Are Reference Types and Use Heap Memory Created by calls to the new operator Freed by garbage collection

7 Simple Garbage Collection
Simple Garbage Collection Algorithm Wait until managed code threads are in a safe state Build a graph of all reachable objects Move reachable objects to compact heap - Unreachable objects’ memory is reclaimed Update references to all moved objects Reference Cycles Are Handled Automatically

8 Multimedia: Simple Garbage Collection

9 Non-Memory Resource Management
Implicit Resource Management Explicit Resource Management

10 Implicit Resource Management
Finalization Garbage Collection with Finalization Finalization Guidelines Controlling Garbage Collection

11 Finalization Finalize Code Called by Garbage Collection
In C#, the Finalize Code Is Provided by a Destructor Use C# Destructor to Implicitly Close a FileStream class Foo { private System.IO.FileStream fs; //... public Foo() { fs = new System.IO.FileStream( “bar”, FileMode.CreateNew); } ~Foo() { fs.Close(); }

12 Garbage Collection with Finalization
Runtime Maintains a List of Objects That Require Finalization Finalization queue Garbage Collection Process Invoked Unreachable Objects Requiring Finalization References added to freachable queue Objects are now reachable and not garbage Move Reachable Objects to Compact the Heap Unreachable objects' memory is reclaimed Update References to All Moved Objects

13 Garbage Collection with Finalization (Continued)
Finalize Thread Runs Executes freachable objects' Finalize methods References removed from freachable queue Unless resurrected, objects are now garbage May be reclaimed next time garbage collection occurs

14 Multimedia: Garbage Collection

15 Finalization Guidelines
Avoid Finalization and Destructors If Possible Performance costs Complexity Delay of memory resource release If You Require Finalization, Finalize Code Should: Avoid calling other objects Avoid making assumptions about thread ID Classes with Finalization Should: Avoid making references to other objects

16 Controlling Garbage Collection
To Force Garbage Collection To Suspend Calling Thread Until Thread’s Queue of Finalizers Is Empty To Allow a Finalized Resurrected Object to Have Its Finalizer Called Again To Request the System Not to Call the Finalizer Method void System.GC.Collect(); void System.GC.WaitForPendingFinalizers(); void System.GC.ReRegisterForFinalize(object obj); void System.GC.SuppressFinalize(object obj);

17 Demonstration: Finalization

18 Explicit Resource Management
The IDisposable Interface and the Dispose Method Temporary Resource Usage Design Pattern

19 The IDisposable Interface and the Dispose Method
Inherit from the IDisposable Interface Implement the Dispose Method Follow the .NET Framework SDK’s Design Pattern class ResourceWrapper : IDisposable { // see code example for details }

20 Demonstration: The IDisposable Interface

21 Temporary Resource Usage Design Pattern
Temporary Resource Use Allocate a resource, use it, and dispose of it Try and Finally Using Statement void DoSomething() { Resource r = new Resource(...); try { r.Foo(); } finally { if (r != null) ((IDisposable)r).Dispose(); } } using (Resource r1 = new Resource()) { r1.Foo(); }

22 Optimizing Garbage Collection
Weak References Generations Additional Performance Features

23 Weak References A Weak Reference Allows an Object to Be Collected If Memory Is Low Object obj = new Object(); // create strong reference WeakReference wr = new WeakReference(obj); obj = null; // remove strong reference // ... obj = (Object) wr.Target; if (obj != null) {//garbage collection hasn’t occurred } else {// object was collected, reference is null //...

24 Demonstration: Weak References

25 Generations To Force Garbage Collection of Generation 0 Through a Specified Generation: To Determine the Generation of an Object: To Return the Maximum Number of Generations That the System Currently Supports: void System.GC.Collect(int Generation); Int32 System.GC.GetGeneration(Object obj); Int32 System.GC.MaxGeneration;

26 Demonstration: Generations

27 Additional Performance Features
Performance Monitoring Large Object Heap Multiprocessor Support Unsafe Code

28 Lab 9: Memory and Resource Management

29 Review Memory Management Basics Non-Memory Resource Management
Implicit Resource Management Explicit Resource Management Optimizing Garbage Collection


Download ppt "Module 9: Memory and Resource Management"

Similar presentations


Ads by Google