Chapter 4: Memory. Taylor & Francis Adair Dingle All Rights Reserved

Similar documents
Opera&ng Systems CMPSCI 377 Garbage Collec&on. Emery Berger and Mark Corner University of Massachuse9s Amherst

CSE P 501 Compilers. Memory Management and Garbage Collec<on Hal Perkins Winter UW CSE P 501 Winter 2016 W-1

NAMES, SCOPES AND BINDING A REVIEW OF THE CONCEPTS

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

Managed runtimes & garbage collection. CSE 6341 Some slides by Kathryn McKinley

CMSC 330: Organization of Programming Languages

G Programming Languages - Fall 2012

Principles of Programming Languages

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

CMSC 330: Organization of Programming Languages

CS 241 Honors Memory

Run-Time Environments/Garbage Collection

Lecture 15 Garbage Collection

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

Memory Management. CS449 Fall 2017

Java's Memory Management

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

Programming Language Implementation

Managed runtimes & garbage collection

Object-Oriented Principles and Practice / C++

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

CA341 - Comparative Programming Languages

Lecture Notes on Garbage Collection

Heap Management. Heap Allocation

CMSC 132: Object-Oriented Programming II

Run-time Environments - 3

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

COMP6771 Advanced C++ Programming

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

CMSC 330: Organization of Programming Languages. Ownership, References, and Lifetimes in Rust

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Assignment of Structs

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers

Garbage Collection. Steven R. Bagley

Garbage collec,on Parameter passing in Java. Sept 21, 2016 Sprenkle - CSCI Assignment 2 Review. public Assign2(int par) { onevar = par; }

G Programming Languages - Fall 2012

CS 101: Computer Programming and Utilization

CS61C : Machine Structures

C11: Garbage Collection and Constructors

Garbage Collection. Lecture Compilers SS Dr.-Ing. Ina Schaefer. Software Technology Group TU Kaiserslautern. Ina Schaefer Garbage Collection 1

CS61C : Machine Structures

Object-Oriented Programming for Scientific Computing

Garbage Collection. Akim D le, Etienne Renault, Roland Levillain. May 15, CCMP2 Garbage Collection May 15, / 35

CMSC 132: Object-Oriented Programming II

Memory management COSC346

CS61C : Machine Structures

CS61C : Machine Structures

Lecture 15a Persistent Memory & Shared Pointers

Automatic Memory Management

Implementation Garbage Collection

Memory Management: The Details

Research opportuni/es with me

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

EDAN65: Compilers, Lecture 13 Run;me systems for object- oriented languages. Görel Hedin Revised:

Structure of Programming Languages Lecture 10

Run-time Environments

Run-time Environments

CSCI-1200 Data Structures Spring 2017 Lecture 27 Garbage Collection & Smart Pointers

CS32 - Week 1. Umut Oztok. June 24, Umut Oztok CS32 - Week 1

Contents. 8-1 Copyright (c) N. Afshartous

04-19 Discussion Notes

Compiler Construction

Effec%ve So*ware. Lecture 9: JVM - Memory Analysis, Data Structures, Object Alloca=on. David Šišlák

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Compiler Construction

Reference Counting. Reference counting: a way to know whether a record has other users

Lecture 2: Memory in C

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

Lecture Notes on Garbage Collection

Pointers. Developed By Ms. K.M.Sanghavi

Lecture 13: Garbage Collection

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Performance of Non-Moving Garbage Collectors. Hans-J. Boehm HP Labs

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

CPSC 427: Object-Oriented Programming

Architecture of so-ware systems

the gamedesigninitiative at cornell university Lecture 9 Memory Management

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Special Member Functions

C10: Garbage Collection and Constructors

Lecture 23: Object Lifetime and Garbage Collection

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Compiler construction 2009

Dynamic Storage Allocation

Vector and Free Store (Pointers and Memory Allocation)

Short Notes of CS201

Concepts Introduced in Chapter 7

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Memory Leak. C++: Memory Problems. Memory Leak. Memory Leak. Pointer Ownership. Memory Leak

Mobile Application Development

CS201 - Introduction to Programming Glossary By

CSC1322 Object-Oriented Programming Concepts

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Transcription:

Chapter 4: Memory Program Memory Overview Analysis of the impact of design on memory use Memory Abstrac9on Heap Memory Memory Overhead Memory Management Programmer s Perspec9ve

Standard Views of Memory Memory management is a background process handled by Opera9ng System Not a concern for developer Abstrac9on frees the programmer from tedious, low- level plaporm- dependent details Managing specific memory loca9ons => Portable code, developed quickly

SoUware designer perspec9ve Memory is uniform: no dis9nc9on made between the cache and secondary store Memory is unlimited resource virtual memory Need worry only about unbounded recursion Memory cheap/free No cost for alloca9on/access/dealloca9on Memory safe Data corrup9on not an9cipated

Program Memory Data segment: Run- 9me stack sta9c alloca9on Holds ac9va9on records associated with func9on calls Program counter Local data (passed parameters, local declara9ons) main() is a (the first ) func9on Efficient!! (but rigid) Register aids in handling stack frames (ac9va9on records) Stack frames fixed size (with possible variable por9on) Data segment: Heap dynamic alloca9on Memory for run- 9me alloca9ons Less efficient, more flexible

Table 4.1 Common Difficul9es with Program Memory Memory Problem Cause Consequence Effect Data corruption Hidden aliases Performance Fragmented Degradation heap Memory Leak Heap memory not collected C++ Memory Leak Handle to memory lost Ownership undermined Data values overwritten Allocator: more time to Software slowed find free memory Poor Scalability Heap memory unusable Lost resource Memory inaccessible Memory allocated but unusable

Heap Memory Flexible memory alloca9on size and frequency of alloca9on may vary from run to run Supports alloca9on dependent on environmental factors Run- 9me overhead (=> performance hit) Explicit alloca9on may slow down if the heap is fragmented May be rejected if free blocks of heap memory too small Explicit dealloca9on C++ model: programmer responsible for releasing memory Implicit dealloca9on C#/Java: execu9on suspended if the garbage collector runs

C++: Explicit dealloca9on C++ designed to be backward compa9ble with C Emphases on efficiency and control retained Explicit dealloca9on means of retaining control of heap management efficient amor9za9on of the cost of memory dealloca9on With NO implicit dealloca9on, C++ programs not subject to suspension for a run of the garbage collector suitable for real- 9me systems BUT,, Memory leaks due to souware design

Example 4.1 C++ Alloca9on of (Heap) Memory at Run- Time // ptr is a pointer variable allocated on the stack // ptr holds the address of the heap object return by new MyType* ptr = new MyType; // #A: MyType object allocated // deallocate heap memory via call to delete operator delete ptr; // #B: MyType object deallocated // null out pointer to indicate it points to nothing ptr = 0; // #C: programmer must reset pointer // pointers can also hold the address of an array ptr = new MyType[10]; // #D: 10 MyType objects allocated // must use delete[] when deallocating an array on heap delete[] ptr; // #E: 10 MyType objects deallocated

Example 4.2 C++ primi9ves: alloca9on/dealloca9on clear // function code: primitives used, no class objects // application programmer ERROR: NEW not matched with DELETE void leakmemory() { int* heapdata; heapdata = new int[100]; return; } // memory leak obvious: no explicit deallocation

Example 4.2 con9nued // function code ok: NEW matched with DELETE void nomemoryleak() { int* heapdata; } heapdata = new int[100]; // heap memory explicitly deallocated: delete matches new delete[] heapdata; return; // function code ok: access to heap memory passed back to caller // CALLER MUST ASSUME RESPONSIBILITY (ownership) FOR HEAP MEMORY int* passmemory() { int* heapdata; heapdata = new int[100]; return heapdata; }

C#/Java: Implicit dealloca9on Java designed 10 years auer C++ memory errors in C++ code widely known premature dealloca9ons, data corrup9on, leaks Java designers decided to remove memory management responsibili9es from programmer rely on garbage collec9on for reclaiming heap memory C# ( MicrosoU s Java )followed suit BUT, data corrup9on and memory leaks s9ll occur garbage collec9on is not a perfect process. C#/Java programmer should zero out references => SoUware designers should track memory

Tracking memory ownership DIFFICULT Who owns this data? Who deallocates it? Mul9ple handles to the same data Explicit aliases and call by reference difficult to track all handles, especially if scope differs. Class construct hides data not obvious to client when heap memory is allocated Constructors can assume memory ownership Other class methods can transfer memory ownership.

Example 4.3 C++: Why Memory Leaks? // function code looks correct void whyleakmemory1() { hiddenleak* naive; } naive = new hiddenleak[100]; delete[] naive; // delete[] matches new[] void whyleakmemory2(hiddenleak localvar) { } void whyleakmemory3() { hiddenleak steal; } hiddenleak share; steal = share; return;

Example 4.4 C++ class without proper memory management //IMPROPERLY DESIGNED: heap memory allocated (constructor) // MISSING: destructor, copy constructor, overloaded = class hiddenleak{ }; private: public: int* heapdata; int size; hiddenleak(unsigned s = 100) { size = s; heapdata = new int[size]; }

Example 4.5 C++ class with proper memory management // class definition,.h file, memory managed properly class noleak{ }; private: public: int* heapdata; int size; noleak(unsigned s = 100) { size = s; heapdata = new int[size]; } // copy constructor supports call by value noleak(const noleak&); // overloaded = supports deep copying void operator=(const noleak&); // destructor deallocates heapdata ~noleak() { delete[] heapdata; }

C++ deep copying // copy constructor supports call by value noleak::noleak(const noleak& src) { size = src.size; } heapdata = new int[size]; for (int k = 0; k < size; k++) heapdata[k] = src.heapdata[k]; // overloaded = supports deep copying; check for self-assignment void noleak::operator=(const noleak& src) { if (this!= &src) } { delete[] heapdata; } size = src.size; heapdata = new int[size]; for (int k = 0; k < size; k++) heapdata[k] = src.heapdata[k];

Example 4.6 C++ class with copying suppressed class nocopy{ private: public: int* heapdata; int size; // copying suppressed! nocopy(const nocopy&); void operator=(const nocopy&); nocopy(unsigned s = 100) { size = s; heapdata = new int[size]; } }; // destructor deallocates heapdata ~nocopy () { delete[] heapdata; }

Shallow vs Deep Copying Shallow copying performs first- level bitwise copy shallow copy of an address establishes an alias Deep copying performs two- 9er copy with objects For an object with heap memory the address is not just copied a new copy of the heap memory is allocated and ini9alized the address of this replica is then copied Requires C++ copy constructor or C#/Java Cloneable

Example 4.8 C# Cloning public class ucopy: ICloneable { private anotherclass refd; } // anotherclass instance allocated on heap // address of subobject held in reference ref public ucopy() { refd = new anotherclass(); } // deep copy: more heap memory allocated via clone // subobject copied into new memory public object Clone() { ucopy local = this.memberwiseclone() as ucopy; } local.refd = this.refd.clone() as anotherclass; return local;

Example 4.8 C# Cloning.. con9nued // application code // #A: ucopy object allocated ucopy u1 = new ucopy(); // intuitive but yields shallow copy // #B: shallow copy of ucopy object ucopy u2 = u1; // deep copy: must cast to retrieve type information // #C: clone of ucopy object u2 = u1.clone() as ucopy;

Suppress Copying Copying may be suppressed when undesirable Copying large registries (hash tables, etc. ) Genera9on of temporaries C++ supports move seman9cs Deep copying is more expensive than shallow copying. Shallow copying may lead to data corrup9on.

Circumvent Copying: Move Seman9cs C++11 Enhance Performance by avoiding copying Acquire heap memory of passed argument (object) Applicable for temporaries Save constructor/destructor pairing Compiler decides whether to invoke Copy constructor or move constructor Overloaded assignment or move assignment Class designer need only define addi9onal methods

C++ 11 move seman9cs: denoted by && // move constructor supports efficient call by value noleak::noleak(const noleak&& src) { size = src.size; } heapdata = src.heapdata; src.size = 0; src.heapdata = nullptr; // move assignment exchanges ownership // return reference to support chained assignment noleak& noleak::operator=(const noleak&& src) { swap( size, src.size); swap( heapdata, src.heapdata); } return *this;

Garbage Collec9on implicit dealloca9on triggered by run- 9me evalua9on of heap Heap too fragmented or insufficient memory available running program must be suspended EXPENSIVE, not feasible for many real- 9me systems No overhead if garbage collector does NOT run All ac9ve data is marked remaining garbage is removed allocated but unused blocks of heap memory released. Insufficient heap memory remedied by the reclama9on of garbage data allocated but no longer used is released

Example 4.9 Classic Mark and Sweep Algorithm for Iden9fying Garbage // start with direct references, the root set: // all visible variables (active memory) at time of sweep // trace out to all variable indirectly referenced void marksweep() { for each Object r in rootset } mark(r); // if heap object marked: KEEP // clear marked status in preparation for subsequent sweeps // if heap object unmarked: RECLAIM (garbage) void sweep() { for each Object x on heap } if (x.marked) x.marked = false; else release(x);

Example 4.9 con9nued // recursive depth-first marking // terminates when all reachable objects marked void mark(object x) { } if (!x.marked) { x.marked = true; for each Object y referenced by x } mark(y);

Reference Coun9ng Another form of Implicit dealloca9on Alloca9on sets (ini9al) reference count to 1 for a data item Each subsequent alias (handle) increments reference count Reference count decremented when handle goes out of scope (heap) Data deallocated when reference count is 0 Drawbacks Unavoidable overhead for all run- 9me alloca9ons and dealloca9ons Cycles cannot be detected

Implicit Dealloca9on Flaws Garbage collec9on cannot accurately detect all garbage Lingering references prevent memory from being collected Live reference to dead object keeps dead object allocated Garbage collector cannot accurately discern valid references Reference coun9ng cannot detect cycles Objects with references to other objects keep object alive Problem when cycle of objects is not referenced externally - > garbage, missed, not deallocated because reference count not zero Weak references may ameliorate the occurrence of cycles.

Compac9on ShiU allocated blocks auer garbage collector runs Non- trivial, expensive! Allocated memory blocks moved to one end of heap free memory is available in large, con9guous blocks Repairs fragmented heap An excessively fragmented heap may cause severe performance degrada9on may trigger compac9on

Figure 4.5 C++ Design Guidelines to prevent memory leaks Match every new with a delete Class design: DEFINE constructor & destructor Class design: DEFINE (or suppress) copy constructor & overloaded assignment operator Use Reference Count to track aliases Increment with each added reference Decrement when alias goes out of scope Deallocate when count is zero Explicitly transfer ownership Pass pointer by reference Assume ownership Reset passed (old owner s) pointer to null

C++ class design C++ classes with internally allocated heap memory MUST make explicit design decisions with respect to copying suppress copying, employ move seman9cs, support deep copying MUST define a destructor To deallocate heap memory when an object goes out of scope A constructor should be defined set the object in an ini9al state Including ini9al configura9on of heap memory usage. C++ objects for efficiency, allocate on the stack for polymorphic behavior, allocate on the heap (see chapter 7)

Common Best Prac9ces Make explicit design decision wrt copying C++: suppress, support deep copying, use move seman9cs C#/Java: rely on shallow copying, implement Cloning Minimize and track aliases Clarify ownership of memory Data corrup9on errors hard to trace Zero out references when access no longer needed Reduce poten9al for data corrup9on Improve performance of garbage collector

Common Best Prac9ces Understand design impact on program memory Stack versus heap memory: efficiency versus flexibility Store versus compute (see sec9on 4.4) Note language differences Use of stack & heap Compile- 9me versus Run- 9me layouts Methods of alloca9on, dealloca9on, copying (cloning) BUT REMEMBER DESIGN NOT enforced by compiler!