Memory Management: The Details

Similar documents
the gamedesigninitiative at cornell university Lecture 9 Memory Management

the gamedesigninitiative at cornell university Lecture 10 Memory Management

Mobile Application Development

a data type is Types

CS 11 C track: lecture 5

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

Memory management COSC346

CA341 - Comparative Programming Languages

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

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

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

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CPSC 213. Introduction to Computer Systems. Winter Session 2017, Term 2. Unit 1c Jan 24, 26, 29, 31, and Feb 2

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

CS 241 Honors Memory

Lecture Notes on Garbage Collection

Garbage Collection. CS 351: Systems Programming Michael Saelee

Pointers. Developed By Ms. K.M.Sanghavi

CMSC 330: Organization of Programming Languages

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

Intermediate Programming, Spring 2017*

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

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

Programming Language Implementation

ITP 342 Advanced Mobile App Dev. Memory

Pointers and References

Short Notes of CS201

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

CPSC 213. Introduction to Computer Systems. Instance Variables and Dynamic Allocation. Unit 1c

CMSC 330: Organization of Programming Languages

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

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

CS201 - Introduction to Programming Glossary By

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

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

Compiler Construction

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

C#.Net. Course Contents. Course contents VT BizTalk. No exam, but laborations

Run-time Environments - 3

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CSE351 Spring 2018, Final Exam June 6, 2018

Advanced Programming & C++ Language

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

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

1 Dynamic Memory continued: Memory Leaks

CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

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

Lecture Notes on Memory Layout

Process s Address Space. Dynamic Memory. Backing the Heap. Dynamic memory allocation 3/29/2013. When a process starts the heap is empty

COMP 2355 Introduction to Systems Programming

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

Lectures 13 & 14. memory management

Object-Oriented Programming for Scientific Computing

Structure of Programming Languages Lecture 10

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

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

CS Programming In C

Compiler Construction

Heap Management. Heap Allocation

Run-Time Environments/Garbage Collection

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

Introduction to C: Pointers

CS193E Lecture #3 Categories and Protocols Cocoa Memory Management

Managed runtimes & garbage collection

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

the gamedesigninitiative at cornell university Lecture 13 Memory Management

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

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

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

Shenandoah: An ultra-low pause time garbage collector for OpenJDK. Christine Flood Roman Kennke Principal Software Engineers Red Hat

Dynamic Memory Allocation I Nov 5, 2002

Run-time Environments -Part 3

the gamedesigninitiative at cornell university Lecture 6 C++: Basics

OVERVIEW. Why learn ios programming? Share first-hand experience. Identify platform differences. Identify similarities with.net

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

Dynamic memory allocation (malloc)

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Limitations of the stack

Engineering Robust Server Software

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Lecture 15a Persistent Memory & Shared Pointers

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

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

Lecture 2, September 4

G52CPP C++ Programming Lecture 20

StackVsHeap SPL/2010 SPL/20

Objective-C ICT/7421ICTNathan. René Hexel. School of Information and Communication Technology Griffith University.

CS61C : Machine Structures

Processes. Johan Montelius KTH

Dynamic Memory Management

Memory and C++ Pointers

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

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

A process. the stack

Advances in Programming Languages: Regions

Run-time Environments

CS193P - Lecture 3. iphone Application Development. Custom Classes Object Lifecycle Autorelease Properties

Run-time Environments

Transcription:

Lecture 10 Memory Management: The Details

Sizing Up Memory Primitive Data Types Complex Data Types byte: char: short: basic value (8 bits) 1 byte 2 bytes Pointer: platform dependent 4 bytes on 32 bit machine 8 bytes on 64 bit machine Java reference is a pointer int: long: 4 bytes 8 bytes Not standard May change Array: data size * length Strings similar (w/ trailing null) float: 4 bytes double: 8 bytes IEEE standard Won t change Struct: sum of struct fields Same rule for classes Structs = classes w/o methods 2

Memory Example class Date { short year; byte day; byte month; 2 byte 1 byte 1 bytes 4 bytes class Student { int id; Date birthdate; Student* roommate; 4 bytes 4 bytes 4 or 8 bytes (32 or 64 bit) 3 12 or 16 bytes

Memory Alignment All data types should align Type starts at multiple of size class Date { short year; Shorts at even addresses Ints/words at multiple of 4 Longs at multiple of 8 byte day; byte month; Structs may require padding Field order matters! Pad between fields to align Worse on 64 bit machines Rule: Order large to small 4

Memory Alignment Struct w/ 3 shorts and an int: short short short int short int short short All data types should align Type starts at multiple of size Shorts at even addresses Ints/words at multiple of 4 Longs at multiple of 8 Structs may require padding short short int short Field order matters! Pad between fields to align int short short short Worse on 64 bit machines Rule: Order large to small 5

Related Topic: Cache Lines All CPUs have caches A6 (ios): 32k L1, 1Mb L2 Snapdragon (Nexus): 4k L0, 16k L2, 2Mb L2 Populate with cache lines Data block of fixed size Relative to cache size Fetch pulls in whole line Can affect performance Accessing neighbors is fast! Example: array scanning CPU Fetches whole line into cache 6

Data Structures and Memory Collection types are costly Even null pointers use memory Common for pointers to use as much memory as pointees Unbalanced trees are very bad Even true of (pointer) arrays Array uses additional memory Not so in array of structs Objects stored directly in array But memory alignment! 7

Data Structures and Memory Collection types are costly Even null pointers use memory Common for pointers to use as much memory as pointees Unbalanced trees are very bad Even true of (pointer) arrays Array uses additional memory Not so in array of structs Objects stored directly in array But memory alignment! 8

Two Main Concerns with Memory Allocating Memory With OS support: standard allocation Reserved memory: memory pools Getting rid of memory you no longer want Doing it yourself: deallocation Runtime support: garbage collection 9

C/C++: Allocation Process malloc Based on memory size Give it number of bytes Typecast result to assign it No initialization at all Example: char* p = (char*)malloc(4) Stack Heap new Based on data type Give it a data type If a class, calls constructor Else no default initialization Example: Point* p = new Point(); Stack Heap 10??? n bytes 1 0 1 sizeof(class)

C/C++: Allocation Process malloc Based on memory size Give it number of bytes Typecast result to assign it No initialization at all Example: char* p = (char*)malloc(4) Stack Preferred in C Heap new Based on data type Give it a data type If a class, calls constructor Else no default initialization Preferred in C++ Example: Point* p = new Point(); Stack Heap 11??? n bytes 1 0 1 sizeof(class)

C/C++: Allocation Process malloc Based on memory size Give it number of bytes Typecast result to assign it No initialization at all Example: char* p = (char*)malloc(4) Stack Heap new Can emulate malloc Create a char (byte) array Arrays not initialized Typecast after creation Example: Point* p = (Point*)(new char[8]) Stack Heap??? n bytes??? n bytes 12

Recall: Custom Allocators Pre-allocated Array (called Object Pool) Start Free End Instead of new, get object from array Just reassign all of fields Use Factory pattern for constructor Delete all objects at frame end Just reset free pointer to start Do not worry about freeing mid frame Easy if only one object type to allocate 13

Anatomy of an Objective-C Constructor NSDate* d1 = [NSDate datewithstring:"2014-03-05"]; Static Method Allocates & Initializes 14

Anatomy of an Objective-C Constructor NSDate* d1 = [NSDate datewithstring:"2014-03-05"]; Static Method Allocates & Initializes NSDate* d2 = [[NSDate alloc] initwithstring:"2014-03-05"]]; Static Method Allocates Instance Method Initializes 15

Custom Allocation in Objective-C @implementation GObject static char* mempool = malloc(sizeof(gobject)*amount); static int pointer = 0; + (id)alloc { if (pointer >= AMOUNT) { return Nil; pointer += sizeof(gobject); return (id)mempool[pointer-sizeof(gobject)]; 16

Custom Allocation in Objective-C @implementation GObject static char* mempool = malloc(sizeof(gobject)*amount); static int pointer = 0; + (id)alloc { if (pointer >= AMOUNT) { return Nil; Fail gracefully pointer += sizeof(gobject); return (id)mempool[pointer-sizeof(gobject)]; 17

public class GObjectFactory { Object Pools In Java private Gobject mempool = new Gobject[AMOUNT]; private int pointer = 0; public GObjectFactory() { for(int ii = 0; ii < AMOUNT; ii++) { mempool[ii] = new GObject(); Initialize Pool 18

public class GObjectFactory { Object Pools In Java 19 public MakeGObject() { if (pointer >= AMOUNT) { return null; GObject o = mempool[pointer++]; // Initialize object here return o; Initialization & Allocation Combined

C++: Objects vs. Allocation Stack Based Object Call with () after variable Calls constructor with args Puts object entirely on stack Deleted when stack popped Example: Point p(1,2); Stack 1 2 Heap Based Object Call with new syntax Pointer on stack Object in heap Must be manually deleted Example: Point p* = new Point(1,2) Stack Heap 1 2 20

C++: Objects vs. Allocation Stack Based Object Call with () after variable Calls constructor with args Not in Java, C#, Obj-C Puts object entirely on stack But Deleted C#/Obj-C when have stack structs popped Classes without any methods Can exist on stack Example: Point p(1,2); Stack 1 2 Heap Based Object Call with new syntax Pointer on stack Object in heap Must be manually deleted Example: Point p* = new Point(1,2) Stack Heap 1 2 21

Two Main Concerns with Memory Allocating Memory With OS support: standard allocation Reserved memory: memory pools Getting rid of memory you no longer want Doing it yourself: deallocation Runtime support: garbage collection 22

Manual Deletion in C/C++ Depends on allocation malloc: free new: delete What does deletion do? Marks memory as available Does not erase contents Does not reset pointer int main() { cout << "Program started" << endl; int* a = new int[length]; delete a; for(int ii = 0; ii < LENGTH; ii++) { cout << "a[" << ii << "]=" << a[ii] << endl; Only crashes if pointer bad Pointer is currently NULL Pointer is illegal address cout << "Program done" << endl; 23

Memory Leaks Leak: Cannot release memory Object allocated on heap Only reference is moved No way to reference object Consumes memory fast! Can even happen in Java memoryarea = newarea; JNI supports native libraries Method may allocate memory Need anor method to free Example: dispose() in JOGL 24

A Question of Ownership void foo() { MyObject* o = " new MyObject(); o.dosomething(); o = null; return; Memory Leak void foo(int key) { MyObject* o =" table.get(key); o.dosomething(); o = null; return; Not a Leak 25

A Question of Ownership void foo() { MyObject* o =" table.get(key); table.remove(key); o = null; return; Memory Leak? void foo(int key) { MyObject* o =" table.get(key); table.remove(key); ntable.put(key,o); o = null; return; Not a Leak 26

A Question of Ownership Thread 1 Thread 2 Owners of obj void run() { o.dosomething1(); void run() { o.dosomething2(); Who deletes obj? 27

Understanding Ownership Function-Based Object owned by a function Function allocated object Can delete when function done Ownership never transferred May pass to or functions But always returns to owner Really a stack-based object Active as long as allocator is But allocated on heap (why?) Object-Based Owned by anor object Referenced by a field Stored in a data structure Allows multiple ownership No guaranteed relationship between owning objects Call each owner a reference When can we deallocate? No more references References unimportant 28

Understanding Ownership Function-Based Object owned by a function Function allocated object Can delete when function done Ownership never transferred May pass to or functions But always returns to owner Easy: Will ignore Really a stack-based object Active as long as allocator is But allocated on heap (why?) Object-Based Owned by anor object Referenced by a field Stored in a data structure Allows multiple ownership No guaranteed relationship between owning objects Call each owner a reference When can we deallocate? No more references References unimportant 29

Reference Strength Strong Reference Reference asserts ownership Cannot delete referred object Assign to NULL to release Else assign to anor object Can use reference directly No need to copy reference Treat like a normal object Standard type of reference Weak Reference Reference!= ownership Object can be deleted anytime Often for performance caching Only use indirect references Copy to local variable first Compute on local variable Be prepared for NULL Reconstruct object? Abort computation? 30

Weak Reference Example in Java public class CombatComponent { WeakReference<NPC> target; public void attacktarget(aicontroller ai) { NPC Target = target.get(); if (Target == null) { // Be prepared for NULL Target = ai.pickcombattargetfor(this); target = new WeakReference<NPC>(Target); // Do stuff with Target Class in package java.lang.ref 31

Weak Reference Example in Java public class CombatComponent { WeakReference<NPC> target; public void attacktarget(aicontroller ai) { NPC Target = target.get(); if (Target == null) { // Be prepared for NULL WeakReference Target = ai.pickcombattargetfor(this); GC if no standard references left target = new WeakReference<NPC>(Target); Encourages eager GC policies // Do stuff with Target Class in package java.lang.ref Reference Managers in Java SoftReference GC possible if no references left But only if space is needed 32

Reference Counting Every object has a counter Tracks number of owners No owners = memory leak Increment when assign reference Can be explicit method call can we do it automatically? Decrement when remove reference Can be explicit or automatic If makes count 0, delete it Ref 1 Ref 2 Object Count = 12 33

When to Adjust Count? On object allocation Initial allocator is an owner Even if in a local variable When added to an object Often handled by setter Part of class invariant When removed from object Also handled by setter Release before reassign Any or time? public class Container { RCObject object; public Container() { // Initial allocation; ownership Object = new RCObject(); public void setobject(rcobject o) { if (object!= null) { object.decrement(); o.increment(); object = o; 34

Reference Counting in Obj-C // create a new instance" Fraction *frac = [[Fraction alloc] init];! [frac retain]; Reference count 2 // set values and print " [frac setnumerator: 1]; " [frac setdenominator: 3]; Reference count 1 printf( "The fraction is: " ); " [frac print]; " printf( "\n" ); 35 // free memory " [frac release];! [frac release]; Reference count 1 Reference count 0 frac is deleted

Reference Counting in Classic Obj-C // create a new instance" Fraction *frac = [[Fraction alloc] init];! [frac retain]; Reference count 2 // set values and print " [frac setnumerator: 1]; " [frac setdenominator: 3]; Reference count 1 printf( "The fraction is: " ); " [frac print]; " printf( "\n" ); // free memory " [frac release];! Reference count 1 Memory Leak! 36

Which Is Correct? Fraction* foo(int n, int d) { // create a new instance" Fraction *frac =" [[Fraction alloc] init]; // set values " [frac setnumerator: n]; " [frac setdenominator: d]; // free memory " [frac release]; // return it " return frac; Fraction* foo(int n, int d) { // create a new instance" Fraction *frac =" [[Fraction alloc] init]; // set values " [frac setnumerator: n]; " [frac setdenominator: d]; // Do nothing" // return it " return frac; 37

Which Is Correct? Fraction* foo(int n, int d) { // create a new instance" Fraction *frac =" [[Fraction alloc] init]; Fraction* foo(int n, int d) { // create a new instance" Fraction *frac =" [[Fraction alloc] init]; // set values " [frac setnumerator: n]; " [frac setdenominator: d]; // free memory " [frac release]; Trick Question! // set values " [frac setnumerator: n]; " [frac setdenominator: d]; // Do nothing" // return it " return frac; // return it " return frac; 38

Neir is Optimal Fraction* foo(int n, int d) { // create a new instance" Fraction *frac =" [[Fraction alloc] init]; // set values " [frac setnumerator: n]; " [frac setdenominator: d]; Fraction* foo(int n, int d) { // create a new instance" Fraction One *frac possibility: =" [[Fraction alloc] init]; make ownership transfer part of // set values " [frac setnumerator: specification n]; " [frac setdenominator: d]; // free memory " [frac release]; // return it " return frac; Object freed. Nothing left to return. // Do nothing" // return it " return frac; Reference kept. Who will release this reference? 39

Objective-C: An Alternate Solution Fraction* foo(int n, int d) { Autorelease // create a new instance" Fraction *frac =" [[Fraction alloc] init]; // set values " [frac setnumerator: n]; " [frac setdenominator: d]; // free memory " [frac autorelease]; // return it " return frac; Delay release until later. Handled by OS. Places object in a pool OS releases all in pool At end of event handler Games are not event driven! Game loop runs continuously Pool is not released You can release it manually At end of loop iteration? 40

Objective-C: An Alternate Solution Fraction* foo(int n, int d) { // create a new instance" Fraction *frac =" [[Fraction alloc] init]; // set values " [frac setnumerator: n]; " [frac setdenominator: d]; // free memory " [frac autorelease]; // return it " return frac; Delay release until later. Handled by OS. Autorelease Al Demers says: Places This object is a hack. in a pool OS releases all in pool Bad Apple, bad. At end of event handler Games are not event driven! Game loop runs continuously Pool is not released You can release it manually At end of loop iteration? 41

Reference Counting in ios 5 // create a new instance" Fraction *frac = [[Fraction alloc] init];! [frac retain]; // set values and print " [frac setnumerator: 1]; " [frac setdenominator: 3]; printf( "The fraction is: " ); " [frac print]; " printf( "\n" ); // free memory " [frac release];! No-op (does nothing) No-op (does nothing) Automated Reference Counting Handled by compiler Inserts retain/release for you Still reference counting, not GC Old methods are deprecated Backwards compatibility only No-ops if ARC is turned on 42

C++ Analogue: Smart Pointers C++ can override anything Assignment operator = Dereference operator -> Use special object as pointer A field to reference object Also a reference counter Assignment increments What about decrementing? When smart pointer deleted Delete object if count is 0 void foo(){ // Create smart pointer" smart_ptr<myobject> p(); // Allocate & assign it" p = new MyObject(); p->dosomething(); // NO LEAK 43

C++ Analogue: Smart Pointers C++ can override anything Assignment operator = Dereference operator -> Use special object as pointer Stack object; not in heap. A field to reference object Also a reference counter Assignment increments void foo(){ // Create smart pointer" smart_ptr<myobject> p(); // Allocate & assign it" p = new MyObject(); p->dosomething(); What about decrementing? 44 When smart pointer deleted Delete object if count is 0 // NO LEAK Stack released; Smart pointer is disposed.

Where Does Count Go? Non-Intruisive Pointers Count inside smart pointer Advantage: Works with any class Disadvantage: Combining with raw pointers (and hence any stdlib code) Intruisive Pointers Count inside referred object Advantage: Easy to mix with raw pointers Disadvantage: Requires custom base object [Images courtesy of Kosmas Karadimitriou] 45

References vs. Garbage Collectors Reference Counting Advantages Deallocation is immediate Works on non-memory objects Ideal for real-time systems Disadvantages Overhead on every assignment Cannot easily handle cycles (e.g. object points to itself) Requires training to use Mark-and-Sweep Advantages No assignment overhead Can handle reference cycles No specialized training to use Disadvantages Collection can be expensive Hurts performance when runs Usually triggered whenever memory is close to full 46

Incremental Mark-and-Sweep Objects have multiple colors Indicate where in GC process At GC, change some colors Free objects of certain color Natural for game loops Give GC a time budget Do at end of game loop Stop when done or time up See online reading for more Lua 3.0: Quad-Color M&S 47

Incremental Mark-and-Sweep Objects have multiple colors Indicate where in GC process At GC, change some colors Free objects of certain color Natural for game loops Give GC a time budget Al Demers says: Unless memory is always close to full, incremental mark and sweep is better than all or options (even manual deallocation) Do at end of game loop Stop when done or time up See online reading for more Lua 3.0: Quad-Color M&S 48

Incremental Mark-and-Sweep Problem is availability Not available in Java Objective-C uses ARC No good C/C++ libraries You need to implement! Create a singleton allocator Has (weak) references to all objects that it allocates Only for complex games Lua 3.0: Quad-Color M&S 49

Summary Memory management a major challenge Manual deallocation is difficult Garbage collection is better on paper But not all techniques easily available Custom allocators common in AAA games Organize memory in pools and budget use Could oretically support GC as well 50