Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Written by: Dr. JJ Shepherd
Chapter 10 Introduction to Arrays
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Road Map Introduction to object oriented programming. Classes
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.
Implications of Substitution Fall 2005 OOPD John Anthony.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Lecture 12: Using Classes Yoni Fridman 7/19/01 7/19/01.
Run time vs. Compile time
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
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.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Lec 19 Array Intro. Agenda Arrays—what are they Declaring Arrays Constructing Arrays Accessing Array cells.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Local Variables Garbage collection. © 2006 Pearson EducationParameters2 of 10 Using Accessors and Mutators Together What if you want to get a property.
Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
Object-Oriented Programming in C++
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
In Java.... Variables contain the values of primitive data types Value Types.
Constructors & Garbage Collection Ch. 9 – Head First Java.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Chapter 9 Life & Death of an Object Stacks & Heaps; Constructors Garbage Collector (gc)
Written by: Dr. JJ Shepherd
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)
Memory Management in Java Mr. Gerb Computer Science 4.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Sections 10.1 – 10.4 Introduction to Arrays
Pointers and Dynamic Variables
Advanced Programming Behnam Hatami Fall 2017.
Lecture 11 Memory Richard Gesick.
Arrays and Collections
Value Types and Reference Types
Declaring Variables – Mod 4
Java Programming Language
Classes and Objects Object Creation
SPL – PS2 C++ Memory Handling.
Coming up Variables Quick look at scope Primitives Objects
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java

References When you declare an array or class instance, you don’t actually create memory for them. Instead you create a reference. A reference tells Java where the class instance or array is stored Initially it is null, meaning it does not point anywhere. When you set it to a new array or class instance, the reference now points to the memory you created. All variables that contain composite types (arrays and instances of classes) have references. Primitive types (boolean, int, double, references) do not have references

For Example int[] myArray; –Creates a reference –No array has been created –No memory yet allocated myArray = new int[60]; –Creates an array –myArray now references the new array

Assigning Composite Types Assigning a reference to a composite type causes two references to point to the same object. –Different from primitive types –Assigning a primitive type copies the value of one into the other. int a=5,b=6; b=a; //b becomes 5. b=7; //b becomes 7. a stays 5. Does not work the same way for composite types...

Assigning Composite Types Example int[] a = {1,2,3}; int[] b = a; System.out.println(b[1]);//Prints 2. a[1]=6; System.out.println(b[1]);//Prints 6! B has been changed because it is set on the second line to point to the same array as a. –b’s and a’s references point to the same place. –Therefore, anything that happens to a, also happens to b.

Variable “a” references an array with three elements int[] a = {1,2,3}; int[] b = a; System.out.println(b[1]); a[1]=6; System.out.println(b[1]); a b

Variable “b” is set equal to “a”. They now reference the same array int[] a = {1,2,3}; int[] b = a; System.out.println(b[1]); a[1]=6; System.out.println(b[1]); a b

Output element 1 of the array that “b” references. int[] a = {1,2,3}; int[] b = a; System.out.println(b[1]); a[1]=6; System.out.println(b[1]); a b

Set element 1 of the array that “a” references to 6 int[] a = {1,2,3}; int[] b = a; System.out.println(b[1]); a[1]=6; System.out.println(b[1]); a b

Output element 1 of the array that “b” references again int[] a = {1,2,3}; int[] b = a; System.out.println(b[1]); a[1]=6; System.out.println(b[1]); a b

Another Example Consider the following code: Student st1; Student st2; st1 = new Student(); st2 = new Student(); st1 = st2;

Declare st1, a reference to a student. Starts out null. Student st1; Student st2; st1 = new Student(); st2 = new Student(); st1 = st2; ST1

Declare a second reference, st2, also null Student st1; Student st2; st1 = new Student(); st2 = new Student(); st1 = st2; ST2 ST1

Create a new Student instance. st1 points to it Student st1; Student st2; st1 = new Student(); st2 = new Student(); st1 = st2; ST2 ST1 Student

Create a second Student instance. st2 points to it Student st1; Student st2; st1 = new Student(); st2 = new Student(); st1 = st2; ST2 ST1 Student

st1’s reference is changed to refer to st2’s student Student st1; Student st2; st1 = new Student(); st2 = new Student(); st1 = st2; ST2 ST1 Student

Nothing points to the first student any more. –Java cannot use it –What happens to it? ST2 ST1 Student

Answer: When memory no longer has any references to it, it is “cleaned up” by Java. This behavior is called garbage collection ST2 ST1 Student

Answer: When memory no longer has any references to it, it is “cleaned up” by Java. This behavior is called garbage collection ST2 ST1 Student

Answer: When memory no longer has any references to it, it is “cleaned up” by Java. This behavior is called garbage collection ST2 ST1 Student

For Historical Context C++ does not have garbage collection –If you lose all the references to something it’s still there. –But you can’t use it. –This is called a memory leak The main reason why Java is a big improvement on C++

The Stack and the Heap Two place Java stores data, the stack and the heap –The stack stores local variables. These are all primitive types (boolean, int, double, references) –The heap stores composite types (arrays and instances of classes) and the primitive types they contain. Rule of thumb: –Memory on the stack is created each time a variable declaration in a method is executed. –Memory on the heap is created only when new is used.

Memory on the Stack Stores variables local to a method Very quick - takes no time to allocate Lasts until the method exits Stored in blocks of memory called frames –Each call to a method creates a stack frame –Frame disappears when method exits

Memory on the Heap Stores class instances and arrays created with the new operator Takes time to allocate memory –Can occasionally slow a program down Lasts until no longer needed.

Summary Primitive types stored on the stack. –Copied when assigned –Disappear when method exits Composite types stored on the heap –References point to them –References initially set to null –The composite type is created when the new operator is used. –Composite types last until no longer needed –When assigned, two references point to the same place