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

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

CPSC 213. Introduction to Computer Systems. Static Scalars and Arrays. Unit 1b

CPSC 213. Introduction to Computer Systems. Scalars and Arrays. Unit 1b

CPSC 213, Winter 2016, Term 2 Final Exam Date: April 19, 2017; Instructor: Mike Feeley and Alan Wagner

CPSC 213, Summer 2017, Term 2 Midterm Exam Date: July 27, 2017; Instructor: Anthony Estey

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

CS558 Programming Languages

Lecture 13: Garbage Collection

CS61C : Machine Structures

CSE 307: Principles of Programming Languages

CS558 Programming Languages

G Programming Languages - Fall 2012

Reading. Companion. Text. - yup, 3.10 again - mainly "Function pointers" box. 1ed: 3.6.6, in Java. static methods are class methods

CS61C : Machine Structures

CS558 Programming Languages

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

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

CPSC 213, Winter 2013, Term 2 Final Exam Date: April 14, 2014; Instructor: Mike Feeley

CMSC 330: Organization of Programming Languages

Types II. Hwansoo Han

Dynamic Memory Management

Dynamic Memory Management! Goals of this Lecture!

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

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

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

CS61C : Machine Structures

Dynamic Memory Management

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

Run-time Environments -Part 3

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

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

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CMSC 330: Organization of Programming Languages

CPSC 213, Winter 2009, Term 2 Midterm Exam Date: March 12, 2010; Instructor: Mike Feeley

Run-time Environments

Run-time Environments

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

CPSC 213, Summer 2017, Term 2 Midterm Exam Solution Date: July 27, 2017; Instructor: Anthony Estey


ECE 15B COMPUTER ORGANIZATION

CS 241 Honors Memory

Lecture Notes on Memory Management

Dynamic Data Structures. CSCI 112: Programming in C

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

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

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

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

Run-Time Environments/Garbage Collection

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Class Information ANNOUCEMENTS

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

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

HW1 due Monday by 9:30am Assignment online, submission details to come

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

Memory Management: The Details

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

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

Lecture Notes on Memory Management

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CPSC 213. Introduction to Computer Systems. Static Control Flow. Unit 1d

CS558 Programming Languages. Winter 2013 Lecture 3

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

CS558 Programming Languages

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

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

(notes modified from Noah Snavely, Spring 2009) Memory allocation

Lecture 13: Complex Types and Garbage Collection

Object-Oriented Programming for Scientific Computing

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

Cling: A Memory Allocator to Mitigate Dangling Pointers. Periklis Akritidis

CPSC 213. Introduction to Computer Systems. Dynamic Control Flow Polymorphism and Switch Statements. Unit 1f

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

Garbage Collection. CS 351: Systems Programming Michael Saelee

Lecture Notes on Queues

CS61C : Machine Structures

CSC 1600 Memory Layout for Unix Processes"

Structure of Programming Languages Lecture 10

Compiler Construction D7011E

Memory management. Johan Montelius KTH

Name, Scope, and Binding. Outline [1]

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

Lecture 7: Binding Time and Storage

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

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

Programming Language Implementation

Vector and Free Store (Pointers and Memory Allocation)

Managed runtimes & garbage collection

Run-time Environments - 3

CS24 Week 2 Lecture 1

Habanero Extreme Scale Software Research Project

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Compiler Construction

Lecture Notes on Garbage Collection

Compiler Construction

Lectures 13 & 14. memory management

CS61C Midterm Review on C & Memory Management

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e Feb 11, 13, 15, and 25. Winter Session 2018, Term 2

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

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

Transcription:

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

Reading For Next 3 Lectures Companion 2.4.4-2.4.5 Textbook Structures, Dynamic Memory Allocation, Understanding Pointers 2nd edition: 3.9.1, 9.9, 3.10 1st edition: 3.9.1, 10.9, 3.11 2

Instance Variables Class X static int i; int j; X.i Object instance of XX Object instance of Object instance of XX Object instance of int j; Object instance of X int j;j; int X anx int intj;j; anx.j Variables that are an instance of a class or struct created dynamically many instances of the same variable can co-exist Java vs C Java: objects are instances of non-static variables of a class C: structs are named variable groups, instance is also called a struct Accessing an instance variable requires a reference to a particular object (pointer to a struct) then variable name chooses a variable in that object (struct) 3

Structs in C (S4-instance-var) struct D { int e; int f; ; class D { public int e; public int f; A struct is a collection of variables of arbitrary type, allocated and accessed together Declaration similar to declaring a Java class without methods name is struct plus name provided by programer static dynamic Access static dynamic struct D d0; struct D* d1; d0.e = d0.f; d1->e = d1->f; 4

Struct Allocation struct D { int e; int f; ; Static structs are allocated by the compiler Static Memory Layout struct D d0; 0x1000: value of d0.e 0x1004: value of d0.f Dynamic structs are allocated at runtime the variable that stores the struct pointer may be static or dynamic the struct itself is allocated when the program calls malloc Static Memory Layout struct D* d1; 0x1000: value of d1 5

struct D { int e; int f; ; runtime allocation of dynamic struct void foo () { d1 = (struct D*) malloc (sizeof(struct D)); assume that this code allocates the struct at address 0x2000 0x1000: 0x2000 0x2000: value of d1->e 0x2004: value of d1->f 6

Struct Access struct D { int e; int f; ; Static and dynamic differ by an extra memory access dynamic structs have dynamic address that must be read from memory in both cases the offset to variable from base of struct is static d0.e = d0.f; d1->e = d1->f; m[0x1000] m[0x1004] m[m[0x1000]+0] m[m[0x1000]+4] r[0] 0x1000 r[1] m[r[0]+4] m[r[0]] r[1] r[0] 0x1000 r[1] m[r[0]] r[2] m[r[1]+4] m[r[1]] r[2] load d1 7

struct D { int e; int f; ; d0.e = d0.f; d1->e = d1->f; r[0] 0x1000 r[1] m[r[0]+4] m[r[0]] r[1] r[0] 0x1000 r[1] m[r[0]] r[2] m[r[1]+4] m[r[1]] r[2] load d1 ld $0x1000, r0 # r0 = address of d0 ld 4(r0), r1 # r0 = d0.f st r1, (r0) # d0.e = d0.f ld $0x1000, r0 # r0 = address of d1 ld (r0), r1 # r1 = d1 ld 4(r1), r2 # r2 = d1->f st r2, (r1) # d1->e = d1->f The revised load/store base plus offset instructions dynamic base address in a register plus a static offset (displacement) ld 4(r1), r2 8

The Revised Load-Store ISA Machine format for base + offset note that the offset will in our case always be a multiple of 4 also note that we only have a single instruction byte to store it and so, we will store offset / 4 in the instruction The Revised ISA Name Semantics Assembly Machine load immediate r[d] v ld $v, rd 0d-- vvvvvvvv load base+offset r[d] m[r[s]+(o=p*4)] ld o(rs), rd 1psd load indexed r[d] m[r[s]+4*r[i]] ld (rs,ri,4), rd 2sid store base+offset m[r[d]+(o=p*4)] r[s] st rs, o(rd) 3spd store indexed m[r[d]+4*r[i]] r[s] st rs, (rd,ri,4) 4sdi 9

Dynamic Allocation 10

Dynamic Allocation in C and Java Programs can allocate memory dynamically allocation reserves a range of memory for a purpose in Java, instances of classes are allocated by the new statement in C, byte ranges are allocated by call to malloc procedure Wise management of memory requires deallocation memory is a scare resource deallocation frees previously allocated memory for later re-use Java and C take different approaches to deallocation How is memory deallocated in Java? Deallocation in C programs must explicitly deallocate memory by calling the free procedure free frees the memory immediately, with no check to see if its still in use 11

Considering Explicit Delete Lets look at this example struct MBuf * receive () { struct MBuf* mbuf = (struct MBuf*) malloc (sizeof (struct MBuf));... return mbuf; void foo () { struct MBuf* mb = receive (); bar (mb); free (mb); is it safe to free mb where it is freed? what bad thing can happen? 12

Lets extend the example to see what might happen in bar() and why a subsequent call to bat() would expose a serious bug struct MBuf * receive () { struct MBuf* mbuf = (struct MBuf*) malloc (sizeof (struct MBuf));... return mbuf; void foo () { struct MBuf* mb = receive (); bar (mb); free (mb); void MBuf* amb; void bar (MBuf* mb) { amb = mb; void bat () { amb->x = 0; This statement writes to unallocated (or re-allocated) memory. 13

Dangling Pointers A dangling pointer is a pointer to an object that has been freed could point to unallocated memory or to another object Why they are a problem program thinks its writing to object of type X, but isn t it may be writing to an object of type Y, consider this sequence of events (1) Before free: amb: 0x2000 0x2000: a struct mbuf (2) After free: amb: 0x2000 0x2000: free memory dangling pointer (3) After another malloc: amb: 0x2000 0x2000: another thing dangling pointer that is really dangerous 14

Avoiding Dangling Pointers in C Understand the problem when allocation and free appear in different places in your code for example, when a procedure returns a pointer to something it allocates Avoid the problem cases, if possible restrict dynamic allocation/free to single procedure, if possible don t write procedures that return pointers, if possible use local variables instead, where possible - we ll see later that local variables are automatically allocated on call and freed on return Engineer for memory management, if necessary define rules for which procedure is responsible for deallocation, if possible implement explicit reference counting if multiple potential deallocators define rules for which pointers can be stored in data structures use coding conventions and documentation to ensure rules are followed 15

Avoiding dynamic allocation If procedure returns value of dynamically allocated object allocate that object in caller and pass pointer to it to callee good if caller can allocate on stack or can do both malloc / free itself struct MBuf * receive () { struct MBuf* mbuf = (struct MBuf*) malloc (sizeof (struct MBuf));... return mbuf; void foo () { struct MBuf* mb = receive (); bar (mb); free (mb); void receive (struct MBuf* mbuf) {... void foo () { struct MBuf mb; receive (&mb); bar (mb); 16

Reference Counting Use reference counting to track object use any procedure that stores a reference increments the count any procedure that discards a reference decrements the count the object is freed when count goes to zero struct MBuf* malloc_mbuf () { struct MBuf* mb = (struct MBuf* mb) malloc (sizeof (struct MBuf)); mb->ref_count = 1; return mb; void keep_reference (struct MBuf* mb) { mb->ref_count ++; void free_reference (struct MBuf* mb) { mb->ref_count --; if (mb->ref_count==0) free (mb); 17

The example code then uses reference counting like this struct MBuf * receive () { struct MBuf* mbuf = malloc_mbuf ();... return mbuf; void foo () { struct MBuf* mb = receive (); bar (mb); free_reference (mb); void MBuf* amb = 0; void bar (MBuf* mb) { if (amb!= 0) free_reference (amb); amb = mb; keep_reference (amb); 18

Garbage Collection In Java objects are deallocated implicitly the program never says free the runtime system tracks every object reference when an object is unreachable then it can be deallocated a garbage collector runs periodically to deallocate unreachable objects Advantage compared to explicit delete no dangling pointers MBuf receive () { MBuf mbuf = new MBuf ();... return mbuf; void foo () { MBuf mb = receive (); bar (mb); 19

Discussion What are the advantages of C s explicit delete What are the advantages of Java s garbage collection Is it okay to ignore deallocation in Java programs? 20

Memory Management in Java Memory leak occurs when the garbage collector fails to reclaim unneeded objects memory is a scarce resource and wasting it can be a serous bug its huge problem for long-running programs where the garbage accumulates How is it possible to create a memory leak in Java? Java can only reclaim an object if it is unreachable but, unreachability is only an approximation of whether an object is needed an unneeded object in a hash table, for example, is never reclaimed The solution requires engineering just as in C, you must plan for memory deallocation explicitly unlike C, however, if you make a mistake, you can not create a dangling pointer in Java you remove the references, Java reclaims the objects Further reading http://java.sun.com/docs/books/performance/1st_edition/html/jpappgc.fm.html 21

Ways to Avoid Unintended Retention imperative approach with explicit reference annulling explicitly set references to NULL when referent is longer needed add close() or free() methods to classes you create and call them explicitly use try-finally block to ensure that these clean-up steps are always taken these are imperative approaches; drawbacks? declarative approach with reference objects refer to objects without requiring their retention store object references that the garbage collector can reclaim WeakReference<Widget> weakref = new WeakReference<Widget>(widget); Widget widget = weakref.get() // may return NULL different levels of reference stickiness - soft discarded only when new allocations put pressure on available memory - weak discarded on next GC cycle when no stronger reference exists - phantom unretrievable (get always returns NULL), used to register with GC reference queue 22

Using Reference Objects Creating a reclaimable reference the Reference class is a template that be instantiated for any reference store instances of this class instead of the original reference void bar (MBuf mb) { amb = new WeakReference<Mbuf>(mb); allows the garbage collector to collect the MBuf even if amb points to it This does not reclaim the weak reference itself while the GC will reclaim the MBuf, it can t reclaim the WeakReference the problem is that amb stores a reference to WeakReference not a big issue here, there is only one but, what if we store a large collection of weak references? 23

Using Reference Queues The problem reference objects will be stored in data structures reclaiming them requires first removing them from these data structures The reference queue approach a reference object can have an associated reference queue the GC adds reference objects to the queue when it collects their referent your code scans the queue periodically to update referring data structures ReferenceQueue<MBuf> refq = new ReferenceQueue<MBuf> (); void bar (MBuf mb) { amb = new WeakReference<Mbuf> (mb,refq); void removegarbage () { while ((WeakReference<Mbuf> ref = refq.poll())!= null) // remove ref from data structure where it is stored if (amb==ref) amb = null; 24