Memory Management & Method Calls in Java Program Execution © Allan C. Milne v15.3.1.

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

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Informática II Prof. Dr. Gustavo Patiño MJ
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 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)
Runtime Environments Source language issues Storage organization
Run-Time Storage Organization
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
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.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
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.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Understanding class definitions Looking inside classes.
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Values, variables and types © Allan C. Milne v
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Stack and Heap Memory Stack resident variables include:
CPSC 388 – Compiler Design and Construction Runtime Environments.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
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.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
CSC 8505 Compiler Construction Runtime Environments.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Programming Languages and Paradigms Activation Records in Java.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
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)
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
RealTimeSystems Lab Jong-Koo, Lim
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Design issues for Object-Oriented Languages
Memory Management.
Storage Allocation Mechanisms
Java Memory Management
Chapter 7: User-Defined Functions II
Java Memory Management
AKA the birth, life, and death of variables.
Cinda Heeren / Geoffrey Tien
Advanced Programming Behnam Hatami Fall 2017.
Dynamically Allocated Memory
understanding memory usage by a c++ program
Memory Allocation CS 217.
Chapter 4 Writing Classes.
UNIT V Run Time Environments.
Runtime Environments What is in the memory?.
Pointers, Dynamic Data, and Reference Types
Corresponds with Chapter 5
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Memory Management & Method Calls in Java Program Execution © Allan C. Milne v15.3.1

Agenda. The role of the JVM. The stack & the heap. Stack management. Calling methods. Creating objects.

The JVM … … automatically manages memory; … allocates memory for variables, parameters and objects; … frees memory locations when they are no longer reachable.

JVM will allocate memory… … on the stack for local variables and formal parameters. … on the heap for objects; this will include space for the fields.

Stack & heap memory. int i = 123; String s = "dog"; Stack Heap i 123 s "dog"

Stack management. A stack is used because storage is –allocated on the top of the stack (pushed) –freed from the top of the stack (popped), The stack is used for local variables, temporary values in expression evaluation and for handling the calling of methods.

Local variables. … … … { double d; String s; Stack On block entry storage locations for local variables are allocated on the stack. … … … d s } On block exit the storage locations for the local variables is freed from the stack.

Local variables … … … { double d; String s; Stack … … … }

Formal parameters. class MyClass { … … … public void myMethod (int i, String s) { … … … } These are known as formal parameters. Within the method body these are treated as for local variables.

Actual parameters. MyClass obj = new MyClass(); String s = "wow"; obj.myMethod (123, s); … … … These are known as actual parameters. Actual parameters can be constants, variables or expressions. These are evaluated when the method is called.

Method calling mechanism. 1.Allocate a stack frame including storage locations for the formal parameters. 2.Evaluate the actual parameters and assign the values to the formal parameters. 3.Execute the method body;  this includes memory alocation for any local variables. 4.On return from the method all stack allocations are freed.

The method call in action. … … … Stack Heap MyClass obj = new MyClass (); obj String s = "wow" s "wow" obj.myMethod (123, s); 1.Allocate a stack frame including storage locations for the formal parameters. i s 2.Evaluate the actual parameters and assign the values to the formal parameters Execute method body – first allocate storage locations for local variables. … 4.On return from the method all stack allocations are freed.

The method call in action. … … … Stack Heap obj s "wow" 4.On return from the method all stack allocations are freed.

Stack summary. So we've seen how the stack is pushed & popped as storage locations are allocated and freed in a structured way. This process reflects –the entering & exiting of program blocks –calling & returning from method calls. It reflects the scope and the lifetime of variables.

Using the heap. Space for objects is always allocated on the heap. This allocation is on a demand basis. The space allocated for an object includes the space for all fields. Space for an object is automatically freed when it is no longer accessible; that is, when it is not referenced by any variables or fields.

The new operator. This is the main mechanism used to create objects. –It requests the correct amount of heap memory from the JVM, –calls the appropriate constructor method, –returns the reference to the new object.

So draw the picture of … class Message { private int mNumber; private String mText; public Message (int i, String s) { mNumber = i; mText = s; } … … … } Message msg = new Message (5, “Hello”);  With the type defined as …