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.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Memory Management & Method Calls in Java Program Execution © Allan C. Milne v
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
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. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Runtime Environments Source language issues Storage organization
Run-Time Storage Organization
Run time vs. Compile time
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
1 Memory Model of A Program, Methods Overview l Memory storage areas for an executing program l Introduction to methods and methods definitions l General.
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.
Processes CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Introduction to Java Programming, 4E Y. Daniel Liang.
Methods & Activation Record. Recap: what’s a method?
OOP Languages: Java vs C++
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
ISBN Chapter 10 Implementing Subprograms.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
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.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Runtime Environments Compiler Construction Chapter 7.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Compiler Construction
CPSC 388 – Compiler Design and Construction Runtime Environments.
Basic Semantics Associating meaning with language entities.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
4-Methods Dr. John P. Abraham Professor UTPA. Common ways of packaging code Properties Methods Classes Namespaces (related classes are grouped into a.
Activation Records (in Tiger) CS 471 October 24, 2007.
Peyman Dodangeh Sharif University of Technology Fall 2013.
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.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Session 22 Chapter 11: Implications of Inheritance.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Programming Languages and Paradigms Activation Records in Java.
Session 6 Comments on Lab 3 & Implications of Inheritance.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Chapter 9 Life & Death of an Object Stacks & Heaps; Constructors Garbage Collector (gc)
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Int main( ) { x = a(); } int a() { y = b(); } int b() { z = c(); } int c() { } 1.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
ISBN Chapter 10 Implementing Subprograms.
RealTimeSystems Lab Jong-Koo, Lim
Design issues for Object-Oriented Languages
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Java Memory Management
Java Memory Management
AKA the birth, life, and death of variables.
Corresponds with Chapter 7
CS2011 Introduction to Programming I Arrays (II)
References Revisted (Ch 5)
Pointers, Dynamic Data, and Reference Types
Basic Guarantees Right off the bat… what I'm going to describe is what almost certainly happens on contemporary machines, but there is absolutely nothing.
Classes and Objects Object Creation
Corresponds with Chapter 5
Run-time environments
CMSC 202 Constructors Version 9/10.
Implementing Functions: Overview
Presentation transcript:

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 interface object running method

1-3 Call stack / runtime stack Used for method information while the method is being executed Local variables Formal parameters Return value Where method should return to Heap Used for Static information (interfaces and classes) Instance information (objects) Memory Allocation in Java

1-4 call stack static space in the heap objects in the heap Memory allocated to your program

1-5 Example: What happens when an object is created by new, as in Person friend = new Person(…); The reference variable has memory allocated to it on the call stack The object is created using memory in the heap Memory Allocation in Java

1-6 Runtime Stack Call stack (runtime stack) is the memory space used for method information while a method is being run When a method is invoked, a call frame (or activation record ) for that method is created and “pushed” onto the call stack All the information needed during the execution of the method is grouped together in the call frame

1-7 Call Frame (Activation Record) for a Method Return value Local variables Formal Parameters Return address

1-8 Call Frame (Activation Record) A call frame contains: Address to return to after method ends Method’s formal parameter variables Method’s local variables Return value (if any) Note that the values in a call frame are accessible only while the corresponding method is being executed!

1-9 public class CallStackDemo { public static void m2( ) { System.out.println("Starting m2"); System.out.println("m2 calling m3"); m3(); System.out.println("m2 calling m4"); m4(); System.out.println("Leaving m2"); return; } public static void m3( ) { System.out.println("Starting m3"); System.out.println("Leaving m3"); return; } Example: a Typical Calling Sequence

1-10 public static void m4( ) { System.out.println("Starting m4"); System.out.println("Leaving m4"); return; } public static void main(String args[ ]) { System.out.println("Starting main"); System.out.println("main calling m2"); m2( ); System.out.println("Leaving main"); }

1-11 Frame for main Frame for m2 Frame for main Frame for m2 Frame for m3 main calls m2m2 calls m3 Frame for main Frame for m2 Return from m3 Frame for main Frame for m2 Frame for m4 m2 calls m4 Frame for main Frame for m2 Return from m4 etc. Call Stack for a Typical Calling Sequence

1-12 When the main method is invoked: A call frame for main is created and pushed onto the runtime stack When main calls the method m2: A call frame for m2 is created and pushed onto the runtime stack When m2 calls m3: A call frame for m3 is created and pushed onto the runtime stack When m3 terminates, its call frame is popped off and control returns to m2 Call Stack for a Typical Calling Sequence

1-13 When m2 now calls m4: What happens next? What happens when m4 terminates? What happens when m2 terminates? What happens when main terminates? Its call frame is popped off and control returns to the operating system Call Stack for a Typical Calling Sequence

1-14 Call Frames We will now look at some examples of what is in a call frame for a method First for simple variables Then for reference variables

1-15 Example: Call Frames - Simple Variables public class CallFrameDemo1 { public static double square(double n){ double temp; temp = n * n; return temp; } public static void main(String args[ ]) { double x = 4.5; double y; y = square(x); System.out.println("Square of " + x + " is " + y); }

1-16 Draw a picture of the call frames on the call stack: What will be in the call frame for the main method? Address to return to in operating system Variable args Variable x Variable y What will be in the call frame for the method square? Address to return to in main Variable n Variable temp Return value Call Frames – Example 1

1-17 There will be a call frame on the call stack for each method called. So what other call frame(s) will be pushed onto the call stack for our example? Which call frames will be on the call stack at the same time? Discussion

1-18 Heap Space Static space: contains one copy of each class and interface named in the program Contains their static variables, and methods Object space: Information is stored about each object: Value of its instance variables Type of object (i.e. name of class)

1-19 Object Creation Now let's look at reference variables … Memory is allocated in the heap area when an object is created using new The reference variable is put in the call frame on the runtime stack The object is created using memory in the heap

1-20 public class CallFrameDemo2 { private static void printAll(String s1, String s2, String s3){ System.out.println(s1.toString( )); System.out.println(s2.toString( )); System.out.println(s3.toString( )); } public static void main(String args[ ]) { String str1, str2, str3; str1 = new String(“ string 1 ”); str2 = new String(“ string 2 ”); str3 = new String(“ string 3 ”); printAll(str1, str2, str3); } Example: Call Frames- Reference Variables

1-21 Draw a picture of the call stack and of the heap as the program executes What will be the sequence of call frames on the call stack? for main for String constructor for str1 – then popped off for String constructor for str2 – then popped off for String constructor for str3 – then popped off for printAll for toString for str1 – then popped off for System.out.println – then popped off etc. Call Frames – Example 2

1-22 What will be in the call frame for main? (and in the heap?) Address to return to in operating system Variable args Variable str1 Initially? After return from String constructor? Variable str2 Variable str3 What will be in the call frame for printAll? Call Frames – Example 2

1-23 Memory Deallocation What happens when a method returns? On the runtime stack: The call frame is automatically popped off when the method returns So, that memory is deallocated

1-24 Memory Deallocation What happens to objects on the heap? An object stays on the heap even if there is no longer a variable referencing it! So, Java has automatic garbage collection It regularly identifies objects which no longer have a variable referencing them, and deallocates that memory