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.

Slides:



Advertisements
Similar presentations
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Advertisements

Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
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.
Dynamic Allocation Eric Roberts CS 106B February 4, 2013.
Chapter 11 Arrays. Introduction Array: An ordered collection of values with two distinguishing characters: – Ordered and fixed length – Homogeneous. Every.
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
Java Software Solutions
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
1 A Sorting Example (a start for Lab 4 problem number 3)
Run-Time Storage Organization
Using final We use the notion of constant data to represent data that cannot be changed. public class Test { static final int someInt = 10; static final.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
Run-time Environment and Program Organization
Static Class Members Wrapper Classes Autoboxing Unboxing.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Java Data Types  Everything is an Object  Except Primitive Data Types  For efficiency  Platform independent  Portable  “slow”  Objects are often.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Memory and C++ Eric Roberts CS 106B February 1, 2013.
Pointer Data Type and Pointer Variables
Chapter 7—Objects and Memory The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Memory C H A P T E R 7 Yea, from.
Chapter 7 Evaluating the Instruction Set Architecture of H1: Part 1.
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
Chapter 7—Objects and Memory The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Memory C H A P T E R 7 Yea, from.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
1 Comp 104: Operating Systems Concepts Java Development and Run-Time Store Organisation.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Chapter 7—Objects and Memory The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Memory C H A P T E R 7 Yea, from.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD.
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.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
SEEM Java – Basic Introduction, Classes and Objects.
Object Oriented Software Development 4. C# data types, objects and references.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Chapter 9 Life & Death of an Object Stacks & Heaps; Constructors Garbage Collector (gc)
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Wrapper Classes Use wrapper objects in Collections when you can’t use primitive types Primitive TypeWrapper Class byteByte shortShort intInteger longLong.
Data Representation Eric Roberts CS 106A February 10, 2016.
“Unboxing” means taking an Integer object and assigning its value to a primitive int. This is done using the.intValue( ) method. Example; Integer z = new.
RealTimeSystems Lab Jong-Koo, Lim
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Object Lifetime and Pointers
Java Memory Management
Java Memory Management
Java Primer 1: Types, Classes and Operators
Please visit
Objects and Memory Eric Roberts CS 106A February 5, 2010.
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Chapter 7—Objects and Memory
Chapter 7—Objects and Memory
Chapter 7—Objects and Memory
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Five-Minute Review What are local/instance/class variables? What about constants? What is an array? How do we locally declare an array of 5 integers? How.
Presentation transcript:

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 unit on which the hardware operates is a sequence of eight consecutive bits called byte. a binary (executable) file Numbers and instructions are stored in still larger units. The most common integer size on a particular hardware is called a word. Because machines have different architectures, the number of bytes and the order of bytes in a word may vary from machine to machine.

Numbers, bases, and conversion = = Octal (0,1,2,3,4,5,6,7) = = = = Hexadecimal (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F) = = = = 0.A8 16 Useful numbers = (about 1K)

Memory allocation of variables objects (heap) local variables (stack) code and static data LOW HIGH

Memory allocation to variables One region of memory is reserved for static data, variables that are never created or destroyed as the program runs, such as named constants and other class variables. When a new object is created, Java allocates space from a pool f memory called the heap. Each time a method is called, Java allocates a new block of memory called a stack frame to hold its local variables. When the method returns, its stack frame is erased. Stack frames come from a region of memory called the stack. In classical architectures, the stack and heap grow toward each other for flexibility.

Heap-stack diagrams It is easier to understand how Java works if you have a good mental model of its use of memory. Whenever your program creates a new object, you need to add a block of memory to the heap. That block must be large enough to store the instance variables for the object, along with some extra space called overhead, that is required for any object. Whenever your program calls a method, you need to create a new stack frame by adding a block of memory to the stack region. The stack frame must be large enough to store local variables for the method, along with some overhead. When a method returns, Java reclaims the memory in its frame.

Object references Internally, Java identifies an object by its address in memory. That address is called a reference. As an example, when Java evaluates the declaration Rational a = new Rational(1, 2) it allocates heap space for the new Rational object. For this example, imagine that the object is allocated at address The local variable a is allocated in the current stack frame and is assigned the value (address), which identifies the object.

public void run() { Rational a = new Rational(1, 2); Rational b = new Rational(1, 3); Rational c = new Rational(1, 6); Rational sum = (a.add(b)).add(c); } 1000 a.num a.den 1020 b.num b.den 1040 c.num c.den a b c sum FFB4 FFB8 FFBC FFC0 heap stack Address model

public void run() { Rational a = new Rational(1, 2); Rational b = new Rational(1, 3); Rational c = new Rational(1, 6); Rational sum = (a.add(b)).add(c); } a.num a.den b.num b.den c.num c.den a b c sum heap stack Pointer model

public void run() { Rational a = new Rational(1, 2); Rational b = new Rational(1, 3); Rational c = new Rational(1, 6); Rational sum = (a.add(b)).add(c); } 1000 a.num a.den 1020 b.num b.den 1040 c.num c.den a b c sum FFB4 FFB8 FFBC FFC0 heap stack public Rational add(Rational r) { return new Rational(this.num*r.den + r.num*this.den, this.den*r.den); } this r FFA8 FFAC

public void run() { Rational a = new Rational(1, 2); Rational b = new Rational(1, 3); Rational c = new Rational(1, 6); Rational sum = (a.add(b)).add(c); } 1000 a.num a.den 1020 b.num b.den 1040 c.num c.den a b c sum FFB4 FFB8 FFBC FFC0 heap stack public Rational add(Rational r) { return new Rational(this.num*r.den + r.num*this.den, this.den*r.den); } (a.add(b)).num (a.add(b)).den 1060

public void run() { Rational a = new Rational(1, 2); Rational b = new Rational(1, 3); Rational c = new Rational(1, 6); Rational sum = (a.add(b)).add(c); } 1000 a.num a.den 1020 b.num b.den 1040 c.num c.den a b c sum FFB4 FFB8 FFBC FFC0 heap stack public Rational add(Rational r) { return new Rational(this.num*r.den + r.num*this.den, this.den*r.den); } this r FFA8 FFAC 5 6 (a.add(b)).num (a.add(b)).den (a.add(b)).add(c).num (a.add(b)).add(c).den

public void run() { Rational a = new Rational(1, 2); Rational b = new Rational(1, 3); Rational c = new Rational(1, 6); Rational sum = (a.add(b)).add(c); } 1000 a.num a.den 1020 b.num b.den 1040 c.num c.den a b c sum FFB4 FFB8 FFBC FFC0 heap stack public Rational add(Rational r) { return new Rational(this.num*r.den + r.num*this.den, this.den*r.den); } (a.add(b)).num (a.add(b)).den (a.add(b)).add(c).num (a.add(b)).add(c).den 1080

public void run() { Rational a = new Rational(1, 2); Rational b = new Rational(1, 3); Rational c = new Rational(1, 6); Rational sum = (a.add(b)).add(c); } 1000 a.num a.den 1020 b.num b.den 1040 c.num c.den a b c sum FFB4 FFB8 FFBC FFC0 heap stack public Rational add(Rational r) { return new Rational(this.num*r.den + r.num*this.den, this.den*r.den); } (a.add(b)).num (a.add(b)).den (a.add(b)).add(c).num (a.add(b)).add(c).den 1080

Garbage collection In the previous example, the object a.add(b) was created in the intermediate step but not referenced by the final stack. It is now garbage. When memory is running short, Java does garbage collection – Mark the objects referenced by variables on stack or in static storage. – Sweep all objects in the heap, reclaim unmarked objects (garbage). This process is called garbage collection (mark-and- sweep collector).

Exercise: Stack-heap diagram public class Point {public class Line { public Point(int x, int y) { public Line(Point p1, Point p2) { cx = x; start = p1; cy = y; finish = p2; } } private int cx; private Point start; private int cy; private Point finish;} public void run() { Point p1 = new Point(0, 0); Point p2 = new Point(200, 200); Line line = new Line(p1, p2); } Draw a heap-stack diagram (pointer model) showing the state of memory just before the run() method returns.

Primitive type versus objects Primitive type public void run() { int x = 17; increment(x); println(“x = “ + x); } private void increment(int n) { n++; println(“n = “ + n); } Output n = 18 n = 17 When you pass an argument of a primitive type to a method, Java copies the value of the argument into the parameter variable. As a result, changes to the parameter variable have no effect on the argument.

Passing x of primitive type int, a value increment(x); 17 n x FFC0 FFC8 x (a value) is copied into n

EmbeddedInteger class public class EnbeddedInteger { public EmbeddedInteger(int n) { value = n; } public void setValue(int n) { value = n; } public int getValue() { return value; } public String toString() { return “” + value; } private int value; }

Object public void run() { EmbeddedInteger x = new EmbeddedInteger(17); increment(x); println(“x = “ + x); } private void increment(EmbeddedInteger n) { n.setValue(n.getValue() + 1); println(“n = “ + n); } Output n = 18

Primitive types vs objects When you pass an object as an argument, there seems to be some form of sharing going on. However, any changes that you make to the instance variables inside an object have a permanent effect on the object. Stack-heap diagrams make the reason for this seeming asymmetry clear. When you pass an object to a method, Java copies the reference, not the object itself.

Passing object x, a reference (address) increment(x) 1000 x.value17 n x 1000 FFC0 FFC8 heapstack x (a reference to an object) is copied into n x and n share the same object

Wrapper classes byteByte shortShort intInteger longLong floatFloat doubleDouble booleanBoolean charCharacter

Using wrapper classes You can create an instance of a wrapper class by calling its constructor with the primitive value. For example, Integer five = new Integer(5); creates a new Integer object containing the value 5. For each of the wrapper classes, Java defines a method to retrieve the primitive value, as illustrated below: int underlyingValue = five.intValue();

Boxing and unboxing As of Java Standard Edition 5.0, Java automatically converts values back and forth between a primitive type and the corresponding wrapper class. For example, if you write Integer five = 5; Java will automatically call the Integer constructor. Similarly, if you then write int six = five + 1; Java will automatically call intValue before the addition. These operations are called boxing and unboxing. Although boxing and unboxing can be quite convenient, this feature can generate confusion and should be used with care.