Smart Pointers.

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

Garbage Collection CS Introduction to Operating Systems.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
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.
CPSC 388 – Compiler Design and Construction
Specialized Reference Counting Garbage Collection using Data Structure Annotations By Eric Watkins and Dzin Avots for CS 343 Spring 2002.
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Age-Oriented Concurrent Garbage Collection Harel Paz, Erez Petrank – Technion, Israel Steve Blackburn – ANU, Australia April 05 Compiler Construction Scotland.
Garbage collection (& Midterm Topics) David Walker COS 320.
Linked lists and memory allocation Prof. Noah Snavely CS1114
1 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
Chapter 3.5 Memory and I/O Systems. 2 Memory Management Memory problems are one of the leading causes of bugs in programs (60-80%) MUCH worse in languages.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
C++ Memory Overview 4 major memory segments Key differences from Java
 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.
Heap storage & Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001.
Garbage Collection and Memory Management CS 480/680 – Comparative Languages.
University of Washington Wouldn’t it be nice… If we never had to free memory? Do you free objects in Java? 1.
Memory Management -Memory allocation -Garbage collection.
2/4/20161 GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques.
Smart Pointers. Dumb Pointers Pointers Necessary – Dynamic memory – Sharing memory.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 12: Automating Memory Management
CSC 533: Programming Languages Spring 2016
Object Lifetime and Pointers
Garbage Collection What is garbage and how can we deal with it?
Lecture 6 of Computer Science II
CSC 533: Programming Languages Spring 2015
Storage 18-May-18.
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Dynamic Memory Allocation
Storage Management.
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
Automatic Memory Management/GC
C++ Memory Management Idioms
Making Dynamic Memory Allocation Safer
Automatic Memory Management/GC
Advanced Programming Behnam Hatami Fall 2017.
Simulated Pointers.
Arrays and Linked Lists
Chapter 9: Pointers.
Simulated Pointers.
Strategies for automatic memory management
Memory Allocation CS 217.
Pointer Data Type and Pointer Variables III
Dynamic Memory A whole heap of fun….
Chapter 12 Memory Management
Dynamic Memory A whole heap of fun….
Heap storage & Garbage collection
List Allocation and Garbage Collection
Dynamic Memory Allocation
Linked List Functions.
Heap storage & Garbage collection
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Automating Memory Management
CSC 533: Programming Languages Spring 2019
Reference Counting.
CMPE 152: Compiler Design May 2 Class Meeting
SPL – PS2 C++ Memory Handling.
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Smart Pointers

Dumb Pointers Pointers Necessary Dynamic memory Sharing memory

Dumb Pointers Pointers Necessary But they are tricky Dynamic memory Sharing memory But they are tricky Memory leaks Bad pointers

References References : safer pointers C++ : Non-nullable

References Java : All objects are on heap Stack has Numeric Variables References References are nullable Cannot delete memory

References Python Everything is a reference… even numbers x = 1 1 x

References Python x = 1 x = 2 Everything is a reference… even numbers

References Python x = 1 x = 2 y = x Everything is a reference… even numbers x = 1 x = 2 y = x 1 x 2 y

References Python x = 1 x = 2 y = x x = 4 Everything is a reference… even numbers x = 1 x = 2 y = x x = 4 1 x 2 y 4

References Python x = 1 x = 2 y = x x = 4 x = x + 1 Everything is a reference… even numbers x = 1 x = 2 y = x x = 4 x = x + 1 1 x 2 y 4 5

Garbage Collection Garbage collection: Automatically reclaim unused memory from heap Various strategies Reference counting Mark and Sweep

Reference Counting Store count of references to each heap allocation 0 references = garbage Once removed, things they point to may become 0 count

Reference Counting Problem: Cycles are not identified

Mark & Sweep Mark all heap allocations as dead Start from stack based variables Traverse pointers and mark hit allocations live Delete dead memory

Compaction Mark & Sweep may leave fragmented memory:

Compaction Mark & Sweep may leave fragmented memory: Allocations must be contiguous Allocation might fail even with enough space

Compaction Compaction : copy allocations so they are packed tightly:

Compaction Compaction : copy allocations so they are packed tightly: Expensive Need to “freeze” user code while addresses updated

Time Memory tends to be short lived or long lived:

Time Advanced GC uses different pools for: Young allocations – checked frequently Old allocatins – checked less frequently

Garbage Collection Pros Cons No need to worry about deleting memory System decides when to schedule cleanup work Less predictable lifespan for objects

Smart Pointers C++11 brought smart pointers Pointers do reference counting Memory released when last reference is released

Shared Ptr shared_ptr : counts number sharing the object When shared_ptr leaves scope, count-- Memory deleted when count == 0

Weak Ptr weak_ptr : keeps track of object without "counting" Can not be used directly Use to ask for a real shared ptr when needed Used to avoid cycles in shared_ptr

Unique Ptr unique_ptr : unshared pointer Can't copy can only move Deletes memory when pointer leaves scope

Courses With Strong/Weak Course Manager responsible for loading from DB

Courses With Strong/Weak Student asks for courses to be loaded Course manager creates objects, stores weak_ptr AND…

Courses With Strong/Weak Student asks for courses to be loaded Course manager creates objects, stores weak_ptr AND… Student given shared_ptrs refCount = 1 refCount = 1

Courses With Strong/Weak Student2 is keeping the Courses in memory courseManager knows where they are, can access if needed refCount = 1 refCount = 1

Courses With Strong/Weak New student asks for a new class Same process refCount = 1 refCount = 1 refCount = 1

Courses With Strong/Weak New student asks for an existing class CS162 courseManager checks its array, realizes it exists, gives Student1 a shared_ptr CS162 now has refcount of 2 refCount = 2 refCount = 1 refCount = 1

Courses With Strong/Weak Student2 destroyed… decrement refcount for CS271 and CS162 refCount = 1 refCount = 1 refCount = 0

Courses With Strong/Weak Student2 destroyed… decrement refcount for CS271 and CS162 shared_ptrs with refcount == 0 automatically deleted refCount = 1 refCount = 1

Courses With Strong/Weak courseManager has no way of knowing CS271 is deleted… Must check status of weak_ptr before using refCount = 1 refCount = 1

Objective C Can pick: Manual release Garbage collection (some platforms) ARC : Automatic Reference Counting Like shared_ptrs and weak_ptrs