SPL/2010 StackVsHeap. SPL/2010 Objectives ● Memory management ● central shared resource in multiprocessing RTE ● memory models that are used in Java and.

Slides:



Advertisements
Similar presentations
Part IV: Memory Management
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Names and Bindings.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
SPL/2010 Pointers and Parameter Passing in C++ 1.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
User-Level Memory Management in Linux Programming
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Run time vs. Compile time
The Concept of Variables
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Linked lists and memory allocation Prof. Noah Snavely CS1114
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
COP4020 Programming Languages
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
OPERATING SYSTEM OVERVIEW. Contents Basic hardware elements.
A genda for Today What is memory management Source code to execution Address binding Logical and physical address spaces Dynamic loading, dynamic linking,
1 Comp 104: Operating Systems Concepts Java Development and Run-Time Store Organisation.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Stack and Heap Memory Stack resident variables include:
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Chapter 8 – Main Memory (Pgs ). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Lecture 8 February 29, Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields.
Data structures Abstract data types Java classes for Data structures and ADTs.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
COMP3190: Principle of Programming Languages
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Operating Systems Lecture 14 Segments Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software Engineering.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Object-Oriented Programming Chapter Chapter
Basic Memory Management 1. Readings r Silbershatz et al: chapters
Object Oriented Software Development 4. C# data types, objects and references.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Peyman Dodangeh Sharif University of Technology Spring 2014.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
LECTURE 13 Names, Scopes, and Bindings: Memory Management Schemes.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
ISBN Chapter 12 Support for Object-Oriented Programming.
RealTimeSystems Lab Jong-Koo, Lim
Main Memory CSSE 332 Operating Systems Rose-Hulman Institute of Technology.
Design issues for Object-Oriented Languages
Processes and threads.
Dynamic Memory Allocation
Lecture 2 Memory management.
Inheritance, Polymorphism and the Object Memory Model
Main Memory Management
Advanced Programming Behnam Hatami Fall 2017.
Chapter 8: Memory management
Lecture 2 Memory management.
Topic 2b ISA Support for High-Level Languages
SPL – PS3 C++ Classes.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

SPL/2010 StackVsHeap

SPL/2010 Objectives ● Memory management ● central shared resource in multiprocessing RTE ● memory models that are used in Java and C++ ● services for Java/C++ programmer from RTE (JVM / OS).

SPL/2010 Perspectives of Memory Services ● Hardware level: ● READ / WRITE ● OS level: ● sharing physical memory among processes ● allocating memory to processors, etc. ● Programming Language: ● how programming language relate to memory operations at runtime and at compile time.

SPL/2010 Overview ● background on memory technologies, ● hardware level aspects ● programming language aspect

SPL/2010 Memory Technologies ● On-chip storage - registers and cache storage ● very fast storage ● generally reside on the same chip as main processor

SPL/2010

Memory Technologies ● Random Access Memory - family of technologies used to implement the computer main memory

SPL/2010 Memory Technologies ● Persistent online storage - Hard disk/solid state disks using magnetic/electromagnetic technology

SPL/2010

Flash memory

SPL/2010 Memory Technologies ● Persistent offline storage - Tapes/DVD/CD are magnetic or optical offline storage technologies 1.44 MB

SPL/2010 Memory hierarchy ● faster memory available in smaller quantities ● slower memory available in larger quantities ● Memory costs rise the faster the memory

SPL/2010 ● Memory system characterization: ● Memory space: how much main memory the processor can address (maximum potential size) ● Word size: writes/reads from main memory are at least word-sized – words are the native units of data the CPU operates on - internal registers can store exactly one word

SPL/2010 ● Width of address: ● memory is viewed as an array of words. ● Each word has an index (0, 1, …, up to the size of the memory space). ● index of a word in the main memory is called its address. 15

SPL/2010 ● Width of the data: ● memory is read and written to/from registers in the processor ● Each such operation is performed on a single word at a time. T ● size of the word is the number of physical connections between processor and memory (BUS) 16

SPL/

SPL/2010 Hardware Perspective ● Operation cycle: ● Load the data from memory into processor registers. ● Apply operation on registers and put result in another register ● Write result from register into memory location. 18

SPL/2010 Hardware Perspective ● primitive operations processor uses with relation to memory are: ● Read (Address), (Register) : copy content of word stored at (Address) into (Register) – Registers are referenced by their number (0, 1, … number of registers on the processor). ● Write (Register), (Address): copy content of (Register) to memory cell at (Address). 19

SPL/ compiler generates abstract set of processor instructions [var] - address in memory in which a variable is stored.

SPL/2010 Programming language perspective ● explicit/implicit memory-related services we already used in Java? 21

SPL/2010 Programming language perspective ● Explicit ● Object allocation - Object allocation using new ● Initialization - Constructor invocation or variable initialization ● Copying - Copying data from one variable to another 22

SPL/2010 Programming language perspective ● Implicit: ● Object deletion - done by the Garbage Collector ● Variable allocation - Local (primitive) variables allocation ● Variable deallocation - Local variable deallocation when leaving scope 23

SPL/2010 Memory Access ● Usually one way to access memory: variable ● variable in Java is characterized by: ● A name ● A type ● A scope: the program region in which the name is recognized and refers to the same variable 24

SPL/2010 Memory Access ● Variables are bound to values at runtime. ● value is characterized by: ● A type ● A position in memory which is the value address ● A size - how much memory does the value requires in memory 25

SPL/2010 Memory Access ● Variable bound to value - type of value must be compatible with type of variable. ● Types are compatible when: ● They are identical ● type of the value is more specific in inheritance hierarchy than type of variable. ● type of value implements type of variable (type of variable is an interface). 26

SPL/2010 Procedures ● program is composed of several procedures. ● procedure is a small piece of code, performing a simple and defined operation. ● procedures may call other procedures or themselves. ● procedure may receive parameters, define local variables and return a value. 27

SPL/2010 OO vs. procedures ● object oriented programs are procedural ● call to a member method of some object = call method with the object as one of the arguments. ● (in some languages, you are required to pass the object as the first argument!) 28

SPL/2010 Stack model vs. Heap model ● two different memory models 29

SPL/2010 Stack ● Procedural calls are viewed in a stack-like manner: ● each procedure's activation requires a frame on a stack. ● each time a procedure is called, a new activation frame is generated on the stack. 30

SPL/2010 ● Each activation frame holds the following information: ● The parameters passed to the procedure ● Local primitive variables declared in the procedure ● The return value - Where to return to when the procedure ends 31

SPL/2010 Each time a function is invoked, an activation frame will open on the stack 32

SPL/2010 run-time/execution/call stack ● Activation Frame Generation: compiler generates the appropriate code, and inserts it as the preamble of each procedure. ● Variable Bindings: compiler hard-codes the location of each variable in the procedure's code, usually using a simple offset from the start of the activation frame. 33

SPL/

SPL/2010 ● When an activation frame is created, the binding between local variables and their location on the stack is made ● When a procedure terminates, its associated activation frame is popped from the stack, and (figuratively) discarded. ● In Java, only variables located on top-most activation frame may be accessed = local variables/parameters. (not the case in C++) 35

SPL/

SPL/2010 Stack Implementation ● modern computer systems support stack data structure; ● For each execution element (process, thread) - memory region called stack. ● OS is responsible for allocating enough space for the stack of each process (or thread), ● process (or thread) use code generated by the compiler, manages the stack on its own. 37

SPL/2010 Stack Implementation ● stack is defined by a single CPU register which points to top of the stack ● Register directly modified by the process (or thread) ● Indirectly modified, by pushing and popping words to/from the stack using machine code instructions. 38

SPL/2010 Heap ● programs need to store information which is relevant across function calls ● too big to fit on the stack. Specify block memory size to store some information. ● Unknown at compilation time 39

SPL/2010 ● Heap - blocks of memory are heaped in a pile, and we can get to the block we need if we remember where we left it. ● In Java: ● local variables are stored on the stack ● objects are stored on the heap 40

SPL/

SPL/2010 ● i stored on the stack ● o(=reference) stored on the stack ● Object allocated by new stored on heap. ● section of the stack represented by variable i contains binary int 5 ( ) ● So, what is stored in o's section? ● address to heap 42

SPL/2010 ● [Object] - might be very big ● [variable o]–holds location of Object on heap ● section associated with o on stack holds reference to starting location of the block of memory new allocated to hold the instance of the new Object() 43

SPL/2010 Objects vs. primitives ● In JAVA: ● primitives always passed to functions by value ● objects are always passed to functions by reference 44

SPL/2010 Heap Implementation ● OS allocates a large block of memory to each process, to serve as its heap. ● block is shared by all threads in same process ● may be enlarged during the life time of the process by using special system calls (e.g.,brk(2)). ● Smaller blocks are (logically) allocated to hold specific data items (e.g., Objects). 45

SPL/2010 Man ● Man - Manual (as in a guide, reference). ● standard documentation system in unix (help files) ● arranged in sections, 1=utilities, 2=system calls, 3=library calls etc. ● brk(2) tells us that brk is a system call. [man 2 brk] 46

SPL/2010 Heap Implementation ● Heap responsibilities of the RTE: – Allocating smaller blocks from the heap – keeping track of which parts of the heap are used at any given time – requesting the OS to enlarge the heap 47

SPL/2010 ● managing the heap is done by the RTE ● managing the stack is done with code instructions inserted by the compiler 48

SPL/2010 Memory deallocation in Java ● blocks of memory are allocated in Java only when creating new Objects ● call to new operator= block of memory is allocated to hold the Object 49

SPL/2010 Memory deallocation in Java ● What happens to an Object once we have no further use for it? ● e.g., we allocated an array inside some function, and then leave the function without leaking the reference to the array outside of the scope of the function. 50

SPL/2010 Memory deallocation in Java ● Should array be kept in memory indefinitely? ● No. This would exhaust our memory rapidly. ● We want to reuse the memory / tell the RTE that the memory can be recycled. ● In Java, this is done automatically for us, by an active entity : garbage collector 51

SPL/2010 Memory deallocation in Java ● Once in a while / user calls System.gc(): ● garbage collector kicks into action, and frees garbage Objects ● Object is considered garbage if it cannot be accessed from a variable on the stack 52

SPL/2010 algorithm of the garbage collector 1. Compose a set of all Objects referenced from the stack and from registers. 2. For each Object in the set, add all Objects it references. 3. Repeat step (2) until the set remains unchanged (compute transitive closure of the "a references b" relation). 4. Free all objects which are not in the set. 53

SPL/2010 Stack and Heap in C++ ● general abstractions of stack and heap are similar in C++ and Java ● fundamental differences in how they are managed. ● C++ allows the programmer more control over memory than Java. Price: ● lines of code to write ● requires the programmer completely understands the environment, avoid (the very large) pitfalls 54

SPL/2010 Stack ● stack in C++ is managed in the same way the stack in Java is managed ● one important difference: objects in C++ may be allocated on the stack ● objects can be passed by value to functions! 55

SPL/2010 Stack ● when an activation frame is deleted, objects are not just discarded as primitive types are, ● C++ RTE ensures that a special function of the object will be called - the destructor ● Destructor: function in charge of cleaning after the object: ● Releasing object state and any resources the object might have acquired. 56

SPL/2010 ● We later discuss three important methods of objects in C++: ● destructor ● copy constructor ● assignment operator 57

SPL/2010 Objects on the stack and pass by value 58

SPL/2010 ● At runtime: ● A place to hold class Foo is reserved on the stack and is binded with the variable foo1. ● The default constructor is called ● An activation frame for doSomthing is created, with enough space to hold the return address and an object of class Foo. ●. 59

SPL/2010 ● A new object of class Foo is instantiated on the stack, by using the copy constructor of class Foo and passing foo1 as the argument. ● The method changeMe() of the object foo2 is called. ● The destructor of foo2 is called. ● doSomething frame is poped from the stack and the program continues from the return address 60

SPL/2010 ● changes made on foo2 are not reflected in foo1. 61

SPL/2010 ● A place to hold class Foo is reserved on the stack and is binded with the variable foo1. ● A place to hold class Foo is reserved on the stack and is binded with the variable foo3. ● The default constructor is called for foo1 ● An activation frame for doSomthing is created, with enough space to hold the return address and an object of class Foo. 62

SPL/2010 ● A new object of class Foo is instantiated on the stack, by using the copy constructor of class Foo and passing foo1 as the argument. ● The method changeMe() of the object foo2 is called. ● foo2 is copied by the copy constructor of class Foo to the location of foo3. ●. 63

SPL/2010 ● The destructor of foo2 is called. ● The doSomething frame is poped from the stack and the program continues from the return address ● changes made on foo2 are not reflected in foo1 ● doSomething implicitly employs three objects of type Foo, instantiating, copying and destructing them! 64

SPL/2010 Heap ● As everything in C++ can be allocated on the stack, so can everything be allocated on the heap. Even primitive types 65

SPL/

SPL/2010 New Operator ● Allocating on the heap is achieved by calling the new operator. ● new is an operator which allocates space on the heap and initializes the space as we requested by calling a constructor ● returned value of new is always of type a_type * = a pointer to a_type ● memory is initialized using constructor (must exists, otherwise code will not compile) 67

SPL/2010 Delete operator ● memory allocated on the heap in C++ is never garbage collected ● responsibility of programmer to free memory. ● Each time we delete an object, its destructor is called. 68

SPL/2010 Allocating and Deleting Arrays in C++ ● Arrays in C++ are allocated by using a special operator, new [], which takes the size of the array as a parameter. 69

SPL/2010 Allocating and Deleting Arrays in C++ ● type returned by the new [100] operator is, type Foo *. ● allocated a block of memory on the heap, large enough to hold 100 Foo objects. ● Each object is initialized using the default constructor of class Foo 70

SPL/2010 ● how can we release the memory? by calling the delete[] operator! ● calls the destructor of each object in the array, and then frees the memory 71

SPL/2010 ● new and new[] are not the same ● delete and delete[] are not the same ● call delete[] on memory allocated by new[] and delete for memory allocated by new. 72