Module 9: Memory and Resource Management

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Dynamic Memory Management
Deadlock and Starvation
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
. Smart Pointers. Memory Management u One of the major issues in writing C/C++ code is managing dynamically allocated memory u Biggest question is how.
Bounding Space Usage of Conservative Garbage Collectors Ohad Shacham December 2002 Based on work by Hans-J. Boehm.
From Theory to Practice 2 OOP Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
Memory Management Tom Roeder CS fa. Motivation Recall unmanaged code eg C: { double* A = malloc(sizeof(double)*M*N); for(int i = 0; i < M*N; i++)
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Run-Time Storage Organization
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Reference Types. 2 Objectives Introduce reference types –class –array Discuss details of use –declaration –allocation –assignment –null –parameter –aggregation.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
7: Deadlocks1 DEADLOCKS EXAMPLES: "It takes money to make money". You can't get a job without experience; you can't get experience without a job. BACKGROUND:
1 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
Ch 4. Memory Management Timothy Budd Oregon State University.
OOP Languages: Java vs C++
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
Programming Languages and Paradigms Object-Oriented Programming.
File I/O Applied Component-Based Software Engineering File I/O CSE 668 / ECE 668 Prof. Roger Crawfis.
SPL/2010 StackVsHeap. SPL/2010 Objectives ● Memory management ● central shared resource in multiprocessing RTE ● memory models that are used in Java and.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
Week 9 Part 1 Kyle Dewey. Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Advanced Java Programming CS 537 – Data Structures and Algorithms.
C++ Memory Overview 4 major memory segments Key differences from Java
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
G ARBAGE C OLLECTION CSCE-531 Ankur Jain Neeraj Agrawal 1.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
® July 21, 2004GC Summer School1 Cycles to Recycle: Copy GC Without Stopping the World The Sapphire Collector Richard L. Hudson J. Eliot B. Moss Originally.
Dynamic Memory Management Jennifer Rexford 1. 2 Goals of this Lecture Dynamic memory management techniques Garbage collection by the run-time system (Java)
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Process Management Deadlocks.
Garbage Collection What is garbage and how can we deal with it?
Core Java Garbage Collection LEVEL – PRACTITIONER.
Names and Attributes Names are a key programming language feature
Overview 4 major memory segments Key differences from Java stack
Jim Fawcett CSE681 – SW Modeling & Analysis Fall 2014
Static data members Constructors and Destructors
Compositional Pointer and Escape Analysis for Java Programs
Lecture 2 Memory management.
Concepts of programming languages
Structs.
Programming Models for Distributed Application
Dynamically Allocated Memory
Storage.
OPERATING SYSTEMS DEADLOCKS
Arrays and Linked Lists
Overview 4 major memory segments Key differences from Java stack
Linked List (Part I) Data structure.
Overview of Memory Layout in C++
Finalization 17: Finalization
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
9-10 Classes: A Deeper Look.
IT3002 Computer Architecture
OPERATING SYSTEMS DEADLOCKS.
DEADLOCKS.
Lecture 2 Memory management.
9-10 Classes: A Deeper Look.
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Module 9: Memory and Resource Management

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

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

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

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

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

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

Multimedia: Simple Garbage Collection

Non-Memory Resource Management Implicit Resource Management Explicit Resource Management

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

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(); }

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

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

Multimedia: Garbage Collection

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

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);

Demonstration: Finalization

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

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 }

Demonstration: The IDisposable Interface

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(); }

Optimizing Garbage Collection Weak References Generations Additional Performance Features

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 //...

Demonstration: Weak References

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;

Demonstration: Generations

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

Lab 9: Memory and Resource Management

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