Memory management COSC346

Similar documents
ITP 342 Advanced Mobile App Dev. Memory

StackVsHeap SPL/2010 SPL/20

Advanced Programming & C++ Language

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

In Java we have the keyword null, which is the value of an uninitialized reference type

Memory Management: The Details

MEMORY MANAGEMENT IN C++ AND JAVA

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

G Programming Languages - Fall 2012

Mobile Application Development

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

Run-time Environments

C++ (classes) Hwansoo Han

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Run-time Environments

C10: Garbage Collection and Constructors

C11: Garbage Collection and Constructors

Compiler Construction

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

Concepts Introduced in Chapter 7

Run-Time Environments/Garbage Collection

Outline. Dynamic Memory Classes Dynamic Memory Errors In-class Work. 1 Chapter 10: C++ Dynamic Memory

Memory Management In C++ And Java

CS61C : Machine Structures

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

CSI33 Data Structures

Compiler Construction

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

Programming Languages Third Edition. Chapter 7 Basic Semantics

Lecture 23: Object Lifetime and Garbage Collection

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

the gamedesigninitiative at cornell university Lecture 9 Memory Management

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

CS 11 C track: lecture 5

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

Name, Scope, and Binding. Outline [1]

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Reliable C++ development - session 1: From C to C++ (and some C++ features)

Programming Language Implementation

CS 241 Honors Memory

Praktikum: Entwicklung interaktiver eingebetteter Systeme

Mobile Application Programming. Objective-C Classes

Chapter 9 :: Subroutines and Control Abstraction

Ch. 11: References & the Copy-Constructor. - continued -

Inheritance, Polymorphism and the Object Memory Model

CMSC 330: Organization of Programming Languages

Object-Oriented Programming for Scientific Computing

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

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

Garbage Collection (1)

Lecture Notes on Memory Management

COMP327 Mobile Computing Session: Lecture Set 1a - Swift Introduction and the Foundation Framework Part 2

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

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Abstract Interpretation Continued

6.172 Performance Engineering of Software Systems Spring Lecture 9. P after. Figure 1: A diagram of the stack (Image by MIT OpenCourseWare.

Pointers and Terminal Control

Chapter 5. Names, Bindings, and Scopes

Object-Oriented Programming

G Programming Languages - Fall 2012

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

Implementing Abstractions

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

CMSC 330: Organization of Programming Languages

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Smart Pointers. Some slides from Internet

CS201 Some Important Definitions

Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Data Structures (list, dictionary, tuples, sets, strings)

Lecture Notes on Memory Management

CSE 12 Week Eight, Lecture One

2. Introducing Classes

Programming Languages

Lecture 15a Persistent Memory & Shared Pointers

Written by John Bell for CS 342, Spring 2018

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

G53CMP: Lecture 14. Run-Time Organisation I. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 14 p.1/37

Developing Applications for ios

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

Topic 7: Activation Records

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

This Lecture. G53CMP: Lecture 14 Run-Time Organisation I. Example: Lifetime (1) Storage Areas

Implementing Subprograms

Vector and Free Store (Pointers and Memory Allocation)

Run-time Environments - 2

CSC 1600 Memory Layout for Unix Processes"

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

CPS 506 Comparative Programming Languages. Programming Language

Motivation for Dynamic Memory. Dynamic Memory Allocation. Stack Organization. Stack Discussion. Questions answered in this lecture:

Structure of Programming Languages Lecture 10

Object Oriented Programming in C++

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Announcement. Final Project Proposal Presentations and Updates

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

CA341 - Comparative Programming Languages

Procedure and Object- Oriented Abstraction

Principles of Computer Science

Transcription:

Memory management COSC346

Life cycle of an object Create a reference pointer Allocate memory for the object Initialise internal data Do stuff Destroy the object Release memory 2

Constructors and destructors Constructor is a method that creates an object instance Allocates memory for instance data May initialise instance variables Usually can be overloaded to accept parameters for initialisation Associated with the new operator Destructor is a method that deletes an object instance Releases any memory allocated in the constructors (or during lifetime of the object) Associated with the delete operator 3

What is memory management Memory management is recycling for your program Each object takes memory in the computer When you finish with an object, you can remove it to re-use the space If objects are never removed, memory fills up and the program can crash 4

What is memory management These days memory management is done behind the scenes by the compiler or garbage collector Understanding memory management will allow you to write more efficient code, in terms of memory usage and execution time 5

The stack and the heap When your program starts, it gets two areas of memory that it can use: A stack, typically used for arguments and local variables within functions and methods. A heap, used for objects and large/persistent data structures. 6

The stack local scope stack pointer during execution local variables method arguments return value stack pointer after return The stack is a part of memory that is managed Last-In-First-Out. To allocate / deallocate memory you either push things onto the stack, or pop them off. The stack is most often used to contain local variables, arguments and return values for function/method calls. All of this is done automatically for you. The stack can be used up, in which case you have stack overflow, which generally crashes your program in unpredictable ways. 7

The heap global scope The heap is a part of memory that is managed independent of program flow You can add and remove things from the heap in any order from anywhere within your program Whenever you allocate a new object, it is added to the heap var x: Fraction = Fraction() adding a Fraction object 8

Memory Memory CPU registers Program Counter (PC) Stack Pointer (SP) Memory regions: Instructions read only, stores program code Data read/write, stores variable data Stack local scope data Heap global scope data CPU registers PC points to next instruction to execute SP points to the top of the stack Stack Heap Instructions Data 9

Short term vs. long term storage Stack is short term: Fast allocation Fast access but can be slow with large data structures (due to value copying) Cleared automatically on function exit Heap is long term Slow allocation have to go through memory management Slower access data might not be contiguous (cache misses) but no need to copy large chunks of data (only references get copied, data stays in place) Not obvious when to clear is the data still needed? 10

Options for heap memory management Manual e.g., C++ Programmer explicitly frees up (deallocates) any memory that has been allocated on the heap and is no longer needed Prone to bugs and memory leaks Automatic at run-time (garbage collector) e.g., Java A special thread on the system scans through your program and removes objects that are no longer being used Little chance of human error, but a bit of impact on execution the collector takes a bit of CPU time Automatic at compile-time e.g., Swift Compiler figures out when objects are not referenced by any part of the program and places release calls appropriately All these options require some method of keeping track of the number of references made to an object 11

Reference counting In reference counting, each object keeps a retain count The retain count tracks how many variables/objects hold a reference to that object If you want to keep a valid reference to an object, you send it a retain message Retain count increments If you no longer need an object, you send it a release message Retain count decrements When retain count reaches 0, the object is deallocated Since no one wants to reference the object, there is no point keeping it in memory 12

Automatic Reference Counting (ARC) These days compilers are so smart that: They can figure out where to place retain and release calls It s all done automatically at compile time, so there is no need for the programmer to explicitly send retain and release messages It works extremely well, except in one scenario 13

Retain cycles A retain cycle occurs when objects reference each other Typically this occurs when a child references a parent In this situation the retain count will never go to zero on these objects they will never get deallocated, even if they are not being used 14

Memory management All objects are allocated on the heap Value variables such as integers, strings, etc., are allocated on the stack Variables defined as some class type are references References are stored on the stack, but the object data which they refer to is allocated on the heap Swift Compiler uses Automatic Reference Counting (ARC) No need for the programmer to send retain and release messages, but it is the programmer s responsibility to ensure that there are no retain cycles in the program! 15

Initialisers & deinitialisers Initialisers are required for object instantiation and stored property initialisation Deinitialisers are optional, as memory is released by ARC but sometimes useful for manually allocated resources class Shape { var pos: CGPoint; } init(pos: CGPoint) { self.pos = pos; } Swift deinit { //Shape specific de-initalisation } var s = Shape(pos: CGPoint(x: 0, y: 1)) 16

Initialiser & deinitialiser hierarchy In Swift, all properties of the child must be initialised before the call to parent s initialiser Opposite of the convention in most languages, where the parent has to be initialised first No explicit calls from child to parent s deinit Deinitialisers for child and parent will be invoked by the compiler class Circle : Shape { var radius: Float } init(pos: CGPoint, radius: Float) { self.radius = radius super.init(pos: pos) } deinit { //Circle specific de-initalisation } var c = Circle(pos: CGPoint(x: 0, y: 1), radius: 3.0) Swift 17

Weak and strong references Designed to combat retain cycle problems Strong reference affects object s retain count Retain count is incremented when strong reference is pointed to an object Retain count is decremented when reference is destroyed or pointed elsewhere Swift references are strong by default class Person { let name: String var apartment: Apartment? } init(name: String) { self.name = name } class Apartment { let number: Int var tenant: Person? } init(number: Int) { self.number = number } var john = Person(name: "John Appleseed") var number73 = Apartment(number: 73) john.apartment = number73 number73.tenant = john 18 Swift

Weak and strong references Code on previous slide effects the graph of references illustrated below When the john and number73 references are changed we get Swift 19

Weak and strong references a situation in which the interconnected Person instance and Apartment instance form a retain cycle Neither will be deallocated a memory leak Swift 20

Weak and strong references Weak reference does not affect object s class Person { retain count let name: String var apartment: Apartment? The most common use of a weak } reference is when class Apartment { let number: Int a child references weak var tenant: Person? a parent } init(name: String) { self.name = name } init(number: Int) { self.number = number } Swift var john = Person(name: "John Appleseed") var number73 = Apartment(number: 73) john.apartment = number73 number73.tenant = john The graph of references for this code 21

Weak and strong references means that the Person instance only has a reference count of one Thus if we change the john reference Swift 22

Weak and strong references The reference count of the Person instance will drop to zero and will be deallocated Swift 23

Weak and strong references Once deallocated the weak reference in the Apartment instance will become nil and when the number73 reference is changed, the Apartment will be deallocated Swift 24