Tài liệu Module 9: Memory and Resource Management ppt

62 386 0
Tài liệu Module 9: Memory and Resource Management ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Contents Overview 1 Memory Management Basics 2 Non-Memory Resource Management 12 Implicit Resource Management 13 Explicit Resource Management 26 Optimizing Garbage Collection 36 Lab 9: Memory and Resource Management 48 Review 55 Module 9: Memory and Resource Management Information in this document, including URL and other Internet Web site references, is subject to change without notice. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, place or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation. Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.  2001-2002 Microsoft Corporation. All rights reserved. Microsoft, ActiveX, BizTalk, IntelliMirror, Jscript, MSDN, MS-DOS, MSN, PowerPoint, Visual Basic, Visual C++, Visual C#, Visual Studio, Win32, Windows, Windows Media, and Window NT are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries. The names of actual companies and products mentioned herein may be the trademarks of their respective owners. Module 9: Memory and Resource Management iii Instructor Notes After completing this module, students will be able to: ! Describe how garbage collection manages object memory. ! Implicitly manage non-memory resources by using a destructor’s finalize code. ! Explicitly manage non-memory resources by using client-controlled deterministic release of resources. ! Write code by using the temporary resource usage design pattern. ! Programmatically control the behavior of the garbage collection. ! Describe advanced garbage collection features. Materials and Preparation This section provides the materials and preparation tasks that you need to teach this module. Required Materials To teach this module, you need the Microsoft ® PowerPoint ® file 2349B_09.ppt. Preparation Tasks To prepare for this module, you should: ! Read all of the materials for this module. ! Review the animation. ! Practice the demonstrations. ! Complete the lab. Presentation: 125 Minutes Lab: 60 Minutes iv Module 9: Memory and Resource Management Demonstrations This section provides demonstration procedures that will not fit in the margin notes or are not appropriate for the student notes. The code for each of the following demonstrations is contained in one project and is located in <install folder>\Democode\Mod09\ GARBAGE COLLECTION. In addition, the code for the individual demonstrations is provided in the student notes. Use the debugger to step through the code while you point out features and ask students what they think will happen next. Finalization In this demonstration, you will show students how garbage collection handles finalization and resurrection. In this demonstration, run the Introduction and ResurrectionDemo methods. The IDisposable Interface In this demonstration, you will show students how to perform explicit resource management by using the IDisposable interface. In this demonstration, run the DisposeDemo method. Weak References In this demonstration, you will show students how garbage collection handles weak references. In this demonstration, run the WeakRefDemo method. Generations In this demonstration, you will show students how garbage collection handles generations. In this demonstration, run the GenerationDemo method. Multimedia This section lists the multimedia items that are part of this module. Instructions for launching and playing the multimedia are included with the relevant slides. Simple Garbage Collection This animation will show students the Microsoft .NET Framework common language runtime’s garbage collection process without finalization. Garbage Collection This animation will show students the .NET Framework common language runtime garbage collection process, including finalization. Module 9: Memory and Resource Management v Module Strategy Use the following strategy to present this module: ! Memory Management Basics Students in your classes will probably use different approaches to memory management. Begin with a brief review of different memory management techniques that you or the students may have learned from experience. Because students will need to adapt their programming practices to the automatic memory management that is provided by the common language runtime, it is important to mention other memory management techniques. Compare and contrast manual memory management with the automatic memory management that is provided by the common language runtime. Outline the simple garbage collection process without the finalization details and use the Simple Garbage Collection animation to help the students understand the concept of the garbage collection process more easily. Instructions for running the animations in this module are included in Instructor Margin Notes. ! Non-Memory Resource Management This topic is an introduction to handling non-memory resources implicitly and explicitly. Tell students that the next two sections cover these areas in detail. You should not spend much time on this slide. ! Implicit Resource Management Introduce the finalization phase of the garbage collection process. Emphasize that in C#, a destructor must be used for the finalization code. The second animation, Garbage Collection, is more complex than the first. It shows the garbage collection process with the finalization details. Spend time discussing the drawbacks that are associated with finalization and what to do if finalization is required. Show students how to deal with an object that has been resurrected. Use the Finalization demonstration to highlight how garbage collection deals with finalization and resurrection. ! Explicit Resource Management Show students how to perform explicit resource management by using the IDisposable interface and Dispose method. Discuss the temporary resource usage design pattern as an example of how to allocate resources for temporary use. ! Optimizing Garbage Collection Use the demonstrations that are provided to show how to optimize garbage collection through weak references and generations. In addition to discussing the programmatic optimizations that can be made to the garbage collection process, briefly mention the use of performance counters to monitor memory activity and the use of a multiprocessor system to scale applications where there are garbage collection bottlenecks. Module 9: Memory and Resource Management 1 Overview ! Memory Management Basics ! Non-Memory Resource Management ! Implicit Resource Management ! Explicit Resource Management ! Optimizing Garbage Collection ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** Objects in the Microsoft ® .NET Framework use memory resources and may use other resources, such as file handles. For software to run properly, these resources must be well managed. In other words, they must be properly allocated and released. After completing this module, you will be able to: ! Describe how garbage collection manages object memory. ! Implicitly manage non-memory resources by using a destructor’s finalize code. ! Explicitly manage non-memory resources by using client-controlled deterministic release of resources. ! Write code by using the temporary resource usage design pattern. ! Programmatically control the behavior of the garbage collection. ! Describe advanced garbage collection features. Topic Objective To provide an overview of the module topics and objectives. Lead-in Objects in the Microsoft .NET Framework use memory resources and may use other resources, such as file handles. For software to run properly, these resources must be well managed. 2 Module 9: Memory and Resource Management " "" " Memory Management Basics ! Developer Backgrounds ! Manual vs. Automatic Memory Management ! Memory Management of .NET Framework Types ! Simple Garbage Collection ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** A major feature of the .NET Framework common language runtime is that the runtime automatically handles the allocation and release of an object’s memory resources. In most cases, automatic memory management enhances code quality and developer productivity without negatively impacting expressiveness or performance. Understanding how the .NET Framework facilitates resource management is essential for writing correct and efficient code. In this section, you will learn about memory management in the .NET Framework, including simple garbage collection. Topic Objective To provide an overview of the section topics. Lead-in A major feature of the .NET Framework common language runtime is that the runtime automatically handles the allocation and release of an object’s memory resources. Module 9: Memory and Resource Management 3 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 ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** Your experience with memory management will vary depending upon your development background. In certain situations, you will need to adapt your programming practices to the automatic memory management that is provided by the common language runtime. COM Developers COM developers are accustomed to implementing reference counting as a manual memory management technique. Each time an object is referenced, a counter is incremented. When a reference to an object goes out of scope, the counter is decremented. When an object’s reference count reaches zero, the object is terminated and its memory is freed. The reference counting scheme is the source of many bugs. If the reference counting rules are not followed precisely, objects may be freed prematurely or unreferenced objects may accumulate in memory. Circular references are also a common source of bugs. A circular reference occurs when a child object has a reference to a parent object, and the parent object has a reference to the child object. Circular references prevent either object from being released or destroyed. The only solution is for the parent and child objects to agree on a fixed pattern of usage and destruction, such as where the parent always deletes the child first. When you develop applications in a managed language, the runtime’s garbage collector eliminates the need for reference counting and, as a result, the bugs that can arise from this manual memory management scheme. Topic Objective To discuss various developer backgrounds with regard to memory management. Lead-in Your experience with memory management will vary depending upon your development background. 4 Module 9: Memory and Resource Management C++ Developers C++ developers are accustomed to the tasks that are related to manual memory management. In C++, when you allocate memory for an object by using the new operator, you must release the object’s memory by using the delete operator. This can lead to errors such as forgetting to release an object and causing a memory leak, or attempting to access memory for an object that has already been released. When you develop applications by using the Managed Extensions for C++, or another managed language, you do not have to use the delete operator to release an object. The garbage collector does this for you automatically when the object is no longer being used by the application. C++ developers may be accustomed to avoiding the use of short-term objects because of the associated cost of manually managing the memory for these objects. For managed short-term objects that are created and then go out of scope between collections, the cost of allocating and releasing memory is extremely low. In the .NET Framework, the garbage collector is actually optimized to manage objects with short lifetimes. When you develop managed applications, it is appropriate to use short-term objects in situations where they simplify your code. Visual Basic Developers Microsoft Visual Basic ® developers are accustomed to automatic memory management. If you are a Visual Basic developer, the programming practices with which you are familiar apply to the majority of the managed objects that you create in the .NET Framework. However, you should take special note of the suggested design pattern for a Dispose method to use when you create or use objects that encapsulate unmanaged resources. [...].. .Module 9: Memory and Resource Management 5 Manual vs Automatic Memory Management Topic Objective To introduce the advantages of automatic memory management in the NET Framework ! Manual Memory Management # Lead-in ! Manual memory management requires that you manage the allocation and deallocation of blocks of memory Programmer manages memory Common Problems # # ! Failure to release memory Invalid... update all references to the moved objects 12 Module 9: Memory and Resource Management Non -Memory Resource Management Topic Objective To provide an overview of non -memory resource management, which is discussed in the following topics ! Implicit Resource Management ! Explicit Resource Management Lead-in Managed objects sometimes encapsulate control over resources that are not managed by the runtime... from this time-consuming and difficult task Manual Memory Management The following table provides examples of manual memory management in different programming languages, C and C++, and in COM Language or environment Example of manual memory management C malloc and free functions C++ new and delete operators COM AddRef and Release reference counting methods Automatic Memory Management in the NET Framework... ways to free these resources Garbage collection provides implicit resource management of an object by calling the object’s finalize code The client of an object provides explicit resource management by calling the Dispose method on the IDisposable interface of the object when the client is finished using the object Module 9: Memory and Resource Management 13 " Implicit Resource Management Topic Objective... nondeterministic release of memory and non -memory resources is another significant difference between NET Framework and C++ destructors 20 Module 9: Memory and Resource Management Finalization Guidelines Topic Objective To alert students to issues that are associated with finalization ! Avoid Finalization and Destructors If Possible # ! Complexity # This topic provides guidelines for handling finalization... explicit and an implicit way to free resources A class provides explicit control by having a method that a client of the object will call when the client has finished using the object In this section, you will learn how to perform explicit resource management by using the IDisposable interface and Dispose method, and how to allocate resources for temporary use Module 9: Memory and Resource Management. .. dangling references 6 Module 9: Memory and Resource Management Memory Management of NET Framework Types Topic Objective To explain how a value’s type affects how the value is managed in memory ! Lead-in Instances of Value Types Use Stack Memory # In the NET Framework, all values have a type, which may be a value type or a reference type ! Allocation and deallocation are automatic and safe Managed Objects... called Display(-1, "Demo stop: Object Resurrection.", 0); } 26 Module 9: Memory and Resource Management " Explicit Resource Management Topic Objective To provide an overview of the section topics Lead-in Your classes should provide an explicit and an implicit way to free resources ! The IDisposable Interface and the Dispose Method ! Temporary Resource Usage Design Pattern *****************************ILLEGAL... track outstanding references to a particular instance and to free that object’s heap memory when appropriate A managed object’s heap memory is only released through garbage collection when there are no reachable references to that object This mechanism ensures that there will be no invalid references to the object’s freed memory and thus no dangling references 8 Module 9: Memory and Resource Management. .. the ResourceWrapper design pattern: 28 Module 9: Memory and Resource Management // Design pattern for a base class public class BaseResource: IDisposable { // Pointer to an external resource private IntPtr handle; // Pointer to a managed object resource this class uses private Component Components; // To track whether Dispose has been called private bool disposed = false; // Constructor for the BaseResource . objects. 12 Module 9: Memory and Resource Management Non -Memory Resource Management ! Implicit Resource Management ! Explicit Resource Management . unmanaged resources. Module 9: Memory and Resource Management 5 Manual vs. Automatic Memory Management ! Manual Memory Management # Programmer manages memory ! Common

Ngày đăng: 17/01/2014, 08:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan