Cmput 115 - Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.

Slides:



Advertisements
Similar presentations
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Advertisements

The University of Adelaide, School of Computer Science
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
Names and Bindings.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Sorting - Selection Sort Cmput Lecture 10 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Self-Reference - Recursion Cmput Lecture 6 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
Run-Time Storage Organization
Run time vs. Compile time
Semantics of Calls and Returns
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Elementary Data Types Scalar Data Types Numerical Data Types Other
Run-time Environment and Program Organization
Code Clarity - Comments, Preconditions and Postconditions Cmput Lecture 2 Department of Computing Science University of Alberta ©Duane Szafron 1999.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
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.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
Processes CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Chapter 8 :: Subroutines and Control Abstraction
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Chapter 1 Algorithm Analysis
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Chapter 7 Objects and Memory. Structure of memory The fundamental unit of memory is called a bit, either 0 or 1. In most modern architectures, the smallest.
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
Lecture No.01 Data Structures Dr. Sohail Aslam
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
CPSC 388 – Compiler Design and Construction Runtime Environments.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
LECTURE 13 Names, Scopes, and Bindings: Memory Management Schemes.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Windows Programming Lecture 03. Pointers and Arrays.
Java Memory Management
Java Memory Management
COMBINED PAGING AND SEGMENTATION
Chapter 10: Void Functions
Run-time organization
More Object Oriented Programming
Memory Allocation CS 217.
Course Overview PART I: overview material PART II: inside a compiler
Run Time Environments 薛智文
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model

©Duane Szafron About This Lecture In this lecture we will learn how memory is structured and used in Java programs.

©Duane Szafron Outline Memory References The stack and the heap Parameter passing Objects on the stack Assignment

©Duane Szafron Why Study a Memory Model? In this course we are studying data structures and algorithms. To compare the space and time requirements of these data structures and algorithms we need to know how the data is actually stored in memory and how it is modified. The way that data is stored and accessed is called a memory model.

©Duane Szafron Memory When a program is running, the data it needs is stored in memory in a binary format. In a simple memory model, there are two kinds of memory –a small set of memory that can be manipulated directly by the CPU is called register memory. –a larger set of memory that cannot be manipulated directly by the CPU is called main memory.

©Duane Szafron Accessing Memory Every byte (8 binary digits) of main memory or word (1, 2, 4 or 8 bytes) is assigned a numerical address. A fetch instruction is used to copy the contents of one word of main memory at a particular address to register memory. A store instruction is used to copy the contents of a register to an address in main memory Registers CPU fetch 0001 store

©Duane Szafron References Application programmers use symbolic program references to refer to objects and values that are stored in memory, instead of numerical addresses. The compiler translates each symbolic program reference into an expression that evaluates to an address in main memory or a register. To understand how memory is used, we must look at how it is organized.

©Duane Szafron Direct References in Methods When a method is executing it can access some objects and some values. The receiver object can be referenced directly using the pseudo-variable this. Other objects and values can be referenced directly using method parameters and local variables. Still other objects and values can only be accessed indirectly by sending messages that return references to them.

©Duane Szafron Method Activations A method can only access objects while it is executing or active. The collection of all direct references in a method is called the frame or stack frame of a method. The frame is created when the method is invoked, and destroyed when the method finishes. The stack frames are organized into a region of memory called the run-time stack.

©Duane Szafron Example Method Consider a method in the class Person, where each person has two instance variables, name which is bound to a String, and age which is bound to an int: public void alpha(Person child, int index) { int anInt; Person newPerson; anInt = 30; newPerson = new Person(“Fred”, anInt); }

©Duane Szafron Local Variables map to Addresses The stack frames are stored in main memory where the receiver reference (this), the parameters and the local variables are mapped to consecutive memory locations that are part of the run-time stack. this child index anInt newPerson stack frame for alpha { {

©Duane Szafron Assignment of Values When a value is assigned to a local variable, the value is put into the appropriate memory location in the stack frame. For example consider the statement: anInt = 30; this child index anInt newPerson stack frame for alpha 30

©Duane Szafron Assignment of Objects When an object is assigned to a local variable, a reference to that object is put into the appropriate memory location in the stack frame. For example, consider the statement: newPerson = new Person(“Fred”, anInt); this child index anInt newPerson stack frame for alpha object ref

©Duane Szafron Object Memory To understand the nature of the object reference we must consider the memory for the object itself. When an object is created, memory for that object is allocated in a region of memory called the heap where space is allocated for each instance variable of the object. For example: new Person(“Fred”, anInt); name age a Person object }

©Duane Szafron Object Memory Organization Each instance variable in an object is bound to a value or another object. If an instance variable is bound to a value, the value is stored directly. If an instance variable is bound to an object, a reference to that object is stored. A constructor initializes this memory. new Person(“Fred”, anInt); name age a Person object object ref 30

©Duane Szafron Object References map to Addresses An object reference maps to an address in the heap called a pointer. newPerson = new Person(“Fred”, anInt); this child index anInt newPerson name age value offset count stack frame for alpha a Person object a String object length data 4 ‘F’ an Array ‘r’ ‘e’‘d’

©Duane Szafron Object References as Arrows When we draw pictures of memory, we often omit the addresses and represent object references as arrows. 30 this child index anInt newPerson 30 name age 0 4 stack frame for alpha a Person object a String object value offset count length data 4 ‘F’ an Array ‘r’ ‘e’‘d’

©Duane Szafron Parameter Passing in Java When a method call is made, data from a calling method must be transferred to the method that is called. How is the argument data passed to the method? Java uses call by value for each value parameter and call by reference for each object parameter. In call by value, a copy of the data value is put into the stack frame for the corresponding parameter. In call by reference, a reference to an object is put into the stack frame.

©Duane Szafron Example Parameter Passing Consider some code that calls the method alpha: aPerson = new Person(“Wilma”, 28); daughter = new Person(“Pebbles”, 5); childIndex = 1; aPerson.alpha(daughter, childIndex); ??? this aPerson daughter childIndex stack frame for caller name age a Person object name age a Person object this child index anInt newPerson stack frame for alpha 1

©Duane Szafron Example Parameter Passing - Arrows Consider some code that calls the method alpha: aPerson = new Person(“Wilma”, 28); daughter = new Person(“Pebbles”, 5); childIndex = 1; aPerson.alpha(daughter, childIndex); 1 this aPerson daughter childIndex stack frame for caller 28 name age a Person object 5 name age a Person object 1 this child index anInt newPerson stack frame for alpha

©Duane Szafron Objects in Objects Some languages (C++) allow objects to contain other objects rather than just references to other objects. Some languages (C, Pascal) use structures or records for composite data instead of objects. In this case the layout of an object is “flattened”: name age a Person object 4 ‘F’‘r’ ‘e’‘d’ 28

©Duane Szafron Objects on the Stack Some languages (C++, C, Pascal) allow objects and structures to be allocated directly in a stack frame instead of the heap. Person aPerson; int anInt; aPerson anInt ‘F’‘r’ ‘e’‘d’ stack frame

©Duane Szafron The Assignment Operation Revisited Assigning a value to a value variable, copies the value. Assigning an object to an object variable, copies only the object reference, not the entire object.

©Duane Szafron Second Java Assignment Example Person person1; Person person2; int int1; int int2; person1 = new Person(“Fred”, 30); int1 = 7; person2 = person1; int2 = int1; 7 7 person1 person2 int1 int2 stack frame 30 name age 0 4 a String object value offset count length data 4 ‘F’ an Array ‘r’ ‘e’‘d’

©Duane Szafron Stack Object or Structure Assignment If an object or a structure is on the stack (not possible in Java), then assigning it to a variable, copies the entire object or structure.

©Duane Szafron Stack Assignment Example Person person1; Person person2; int int1; int int2; person1 = new Person(“Fred”, 30); int1 = 7; person2 = person1; int2 = int1; person1 person2 int1 int2 stack frame 4 ‘F’‘r’ ‘e’‘d’ 4 ‘F’‘r’ ‘e’‘d’ 30

©Duane Szafron The Memory for Collections In Java, an array of Objects or a Vector of objects, actually contains references to objects. The memory for the collection itself is one four byte object reference per element, regardless of the size of the element object itself. In other languages all of the memory for the collection may be in the collection itself and this may even be in the stack.

©Duane Szafron Java Collection Memory Example Person people[ ]; people = new Person[4]; for (index = 0; index < 4, index++) people[index] = new Person(“name” + index, index); people stack frame 4 length data an Array name age a String object length data 5 ‘n’ an Array ‘a’ ‘m’‘e’ ‘0’ 1 name age value offset count 2 name age 3 name age

©Duane Szafron Collection Stack Memory Example stack frame people 5 ‘n’‘a’ ‘m’‘e’ ‘0’ 0 5 ‘n’‘a’ ‘m’‘e’ ‘1’ 1 5 ‘n’‘a’ ‘m’‘e’ ‘2’ 2 5 ‘n’‘a’ ‘m’‘e’ ‘3’ 3