© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 6 Object Oriented Programming in Java Language Basics Objects.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
RTS - Implementation ITV Real-Time Systems Anders P. Ravn Aalborg University February 2006.
Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
© Andy Wellings, 2003 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Reference Types. 2 Objectives Introduce reference types –class –array Discuss details of use –declaration –allocation –assignment –null –parameter –aggregation.
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.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles C/C++ Emery Berger and Mark Corner University of Massachusetts.
Lecture 9 Concepts of Programming Languages
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
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.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Initialization & Cleanup Guaranteed initialization with the constructor The name of the constructor is the same as the name of the class.
Enhancements to Java for Real Time Systems Theresa Dsena CSE Fall 2006 Prof. Ganesan.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.
Object-Oriented Programming in C++
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
EEL The Real-Time Specification for Java (1)
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.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
111 G53SRP: RTSJ Memory Areas Chris Greenhalgh School of Computer Science.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Object-Oriented Programming Chapter Chapter
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
ISBN Object-Oriented Programming Chapter Chapter
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
How to Compile Aspects with Real-Time Java Pengcheng Wu Northeastern University Mar. 14, 2005 FOAL Workshop with AOSD’05.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
© Andy Wellings, 2004 Thread Priorities I  Although priorities can be given to Java threads, they are only used as a guide to the underlying scheduler.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Multi Threading.
Lecture 9 Concepts of Programming Languages
Java Programming Language
Packages and Interfaces
Classes and Objects Object Creation
Lecture 9 Concepts of Programming Languages
CMSC 202 Constructors Version 9/10.
Presentation transcript:

© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the RTSJ  Memory Management  Clocks and Time  Scheduling and Schedulable Objects  Asynchronous Events and Handlers  Real-Time Threads  Asynchronous Transfer of Control  Resource Control  Schedulability Analysis  Conclusions

© Andy Wellings, 2004 Memory Management Lecture aims  To provide an introduction to memory management in the RTSJ  To give an overview of MemoryAreas  To show an example of Scoped Memory Usage  To consider how to estimating the size of objects

© Andy Wellings, 2004 Introduction I  An advantage of using a high-level language is that it relieves the programmer of the burden of dealing with many of the low-level resource allocation issues  Issues such as assigning variables to registers or memory locations, allocating and freeing memory for dynamic data structures, etc., all detract the programmer from the task at hand  Languages like Java remove many of these troublesome worries and provide high-level abstract models that the programmer can use

© Andy Wellings, 2004 Introduction II  For real-time/embedded systems programming there is a conflict  Use of high-level abstractions aid in the software engineering of the application  Yet embedded and real-time systems often only have limited resources (time and space) and these must be carefully managed  Nowhere is this conflict more apparent than in the area of memory management  Embedded systems usually have a limited amount of memory available; this is because of cost, size, power, weight or other constraints imposed by the overall system requirements  It may be necessary to control how this memory is allocated so that it can be used effectively

© Andy Wellings, 2004 Stack versus Heap Memory  The run-time system of most languages provide two data structures to help manage dynamic memory: the stack and the heap  In Java, the stack is typically used for storing variables of basic data types (such as int, boolean and reference variables) which are local to a method  All objects, which are created from class definitions, are stored on the heap and the language requires garbage collection (GC) for an effective JVM implementation  Much work has been done on real-time GC, yet there is still a reluctance to rely on it in time-critical systems

© Andy Wellings, 2004 Real-time and Garbage Collection  GC may be performed either when the heap is full or incrementally (asynchronous garbage collection)  Running the garbage collector may have a significant impact on the response time of a time-critical thread  Consider a time-critical periodic thread which has had all its objects pre-allocated  Even though it may have a higher priority than a non time-critical thread and will not require any new memory, it may still be delayed if it preempts the non time-critical thread when garbage collection has been initiated by an action of that thread  In this instance, it is not safe for the time-critical thread to execute until garbage collection has finished (particularly if memory compaction is taking place)

© Andy Wellings, 2004 The RTSJ and Memory Management  The RTSJ recognizes that it is necessary to allow memory management which is not affected by the problems of garbage collection  It does this via the introduction of immortal and scoped memory areas  These are areas of memory which are logically outside of the heap and, therefore, are not subject to garbage collection  If a schedulable object is active in a memory area, all calls to new create the object in that memory area

© Andy Wellings, 2004 The Basic Model  RTSJ provides two alternatives to using the heap: immortal memory and scoped memory  The memory associated with objects allocated in immortal memory is never subject to garbage collection and is never released during the lifetime of the application  Objects allocated in scoped memory have a well-defined life time  Schedulable objects may enter and leave a scoped memory area  Whilst they are executing within that area, all memory allocations are performed from the scoped memory  When there are no schedulable objects active inside a scoped memory area, the allocated memory is reclaimed

© Andy Wellings, 2004 Memory Areas I public abstract class MemoryArea { protected MemoryArea(long sizeInBytes); protected MemoryArea(long sizeInBytes, Runnable logic);... // methods public void enter(); // Associate this memory area to the current // schedulable object for the duration of the // logic.run method passed as a parameter to // the constructor public void enter(Runnable logic); // Associate this memory area to the current // schedulable object for the duration of the // logic.run method passed as a parameter

© Andy Wellings, 2004 Memory Areas II public static MemoryArea getMemoryArea(Object object); // Get the memory area associated with the object public long memoryConsumed(); // Returns the number of bytes consumed public long memoryRemaining(); // Returns the number of bytes remaining public long size(); // Returns the current size of the memory area... }

© Andy Wellings, 2004 Immortal Memory public final class ImmortalMemory extends MemoryArea { public static ImmortalMemory instance(); }  There is only one ImmortalMemory area, hence the class is defined as final and has only one additional method ( instance ) which will return a reference to the immortal memory area  Immortal memory is shared among all threads in an application  Note, there is no public constructor for this class. Hence, the size of the immortal memory is fixed by the real-time JVM

© Andy Wellings, 2004 Allocating Objects in Immortal Memory  The simplest method for allocating objects in immortal memory is to use the enter method in the MemoryArea class and pass an object implementing the Runnable interface ImmortalMemory.instance().enter(new Runnable() { public void run() { // any memory allocation performed here // using the allocator will occur in // Immortal memory } } ); see Introduction notes about anonymous inner classes

© Andy Wellings, 2004 Warning It should be stressed that although all memory allocated by the above run method will occur from immortal memory, any memory needed by the object implementing the run method will be allocated from the current memory area at the time of the call to enter In this case, some small amount of memory will still be allocated by the virtual machine in order to implement the object

© Andy Wellings, 2004 Scope Memory Areas public abstract class ScopedMemory extends MemoryArea { // constructors public ScopedMemory(long size); public ScopedMemory(long size, Runnable logic);... // methods public void enter(); public void enter(Runnable logic); public long getMaximumSize();... }

© Andy Wellings, 2004 Linear Time Memory package javax.realtime; public class LTMemory extends ScopedMemory { //constructors public LTMemory(long initialSizeInBytes, long maxSizeInBytes); public LTMemory(long initialSizeInBytes, long maxSizeInBytes, Runnable logic);... // methods public int getMaximumSize(); } Similarly for VTMemory: variable time memory

© Andy Wellings, 2004 Note Linear time refers to the time it takes to create an object in the memory, NOT, the time it takes to run the constructor

© Andy Wellings, 2004 Reference Counts  Each scoped memory object has a reference count which indicates the number of times the scope has been entered  When that reference count goes from 1 to 0, the memory allocated in the scoped memory area can be reclaimed (after running any finalization code associated with the allocated objects)

© Andy Wellings, 2004 Example: Scoped Memory  Consider a class that encapsulates a large table which contains (two by two) matrices  The match method takes a two by two matrix and performs the dot product of this with every entry in the table  It then counts the number of results for which the dot product is the unity matrix

© Andy Wellings, 2004 Matrix public class MatrixExample { public MatrixExample(int Size) { /* initialize table */ } public int match(final int with[][]) { /* check if “with” is in the table */ } private int[][] dotProduct(int[][] a, int [][] b) { /* calculate dot product return result */ } private boolean equals(int[] [] a, int[] [] b) { /* returns true if the matrices are equal */ } private int [][][] table; // 2 by 2 matrices private int [][] unity = {{1,1},{1,1}}; }

© Andy Wellings, 2004 Match Method: with GC  Each time around the loop, a call to dotProduct is made which creates and returns a new matrix object  This object is then compared to the unity matrix  After this comparison, the matrix object is available for garbage collection public int match(final int with[][]) { int found = 0; for(int i=0; i < table.length; i++) { int[][] product = dotProduct(table[i], with); if(equals(product, unity)) found++; } return found; }

© Andy Wellings, 2004 Match Method: LTMemory public int match(final int with[][]) { // first attempt LTMemory myMem = new LTMemory(1000, 5000); int found = 0; for(int i=0; i < table.length; i++) { myMem.enter(new Runnable() { public void run() { int[][] product = dotProduct(table[i], with); if(equals(product, unity)) found++; } }); } return found; }

© Andy Wellings, 2004 Problems  The accessibility of the for loop parameter i ; the only local parameters that can be accessed from within a class nested within a method are those that are final. i and found are not final  To solve this problem it is necessary to create a new final variable, j, inside the loop which contains the current value of i  The variable found must be moved to be local to the class and initialized to 0 on each call of the match

© Andy Wellings, 2004 Problems Continued  The anonymous class is a subclass of Object not the outer class MatrixExample  Consequently, the default equals method is the one in Object and a compilation error occurs  This can be circumvented by naming the method explicitly

© Andy Wellings, 2004 Matrix Version 2 public class MatrixExample { public MatrixExample(int Size) { /* initialize table */ } public int match(final int with[][]) { /* check if “with” is in the table */ } private int[][] dotProduct(int[][] a, int [][] b) { /* calculate dot product return result */ } private boolean equals(int[] [] a, int[] [] b) { /* returns true if the matrices are equal */ } private int [][][] table; // 2 by 2 matrices private int [][] unity = {{1,1},{1,1}}; public int found = 0; private LTMemory myMem; }

© Andy Wellings, 2004 Match Version 2 public int match(final int with[][]) { found = 0; for(int i=0; i < table.length; i++) { final int j = i; // myMem now declared at the class level myMem.enter(new Runnable() { public void run() { int[][] product = dotProduct(table[j], with); if(MatrixExample.this.equals(product, unity)) found++; } }); } return found; }

© Andy Wellings, 2004 Problem  This approach now reclaims the temporary memory every time the enter method terminates  However the creating of the anonymous class will still use memory in the surrounding memory area  In this case, there will be memory for a private copy of the j variable and a private reference to the with array (this is how the compiler implements the access to the method ’ s data)  As well as these, there will be some other memory allocated for implementing the object  Although this memory cannot be entirely eliminated, it can be significantly reduced by creating an object from a named inner class and reusing that object

© Andy Wellings, 2004 Matrix Final Version public class MatrixExample {... // constructor contains produce = new Product(); private class Product implements Runnable { int j; int withMatrix[][]; int found = 0; public void run() { int[][] product = dotProduct(table[j], withMatrix); if(matrixEquals(product, unity)) found++; }... }

© Andy Wellings, 2004 Match Final Version public int match(final int with[][]) { produce.found = 0; for(int i=0; i < table.length; i++) { produce.j = i; produce.withMatrix = with; myMem.enter(produce); } return produce.found; }  With this solution, there is just the initial cost of creating the produce object (performed in the constructor of MatrixExample )  Note that the only way to pass parameters to the run method is via setting attributes of the object (in this case directly)

© Andy Wellings, 2004 Size Estimator  Allows the size of a Java object to be estimated  Again, size is the object itself, it does not include an objects created during the constructor package javax.realtime; public final class SizeEstimator { // constructor public SizeEstimator(); //methods public long getEstimate(); public void reserve(java.lang.Class c, int n); public void reserve(SizeEstimator s); public void reserve(SizeEstimator s, int n); }

© Andy Wellings, 2004 Example { SizeEstimator s = new SizeEstimator(); s.reserve(javax.realtime.PriorityParameters.class, 1); System.out.println(“size of PriorityParameters is “ +s.getEstimate()); }

© Andy Wellings, 2004 Summary  The lack of confidence in real-time garbage collection is one of the main inhibitors to the widespread use of Java in real-time and embedded systems  The RTSJ has introduced an alternative (additional) memory management facility based on the concept of memory areas  There are two types on non-heap memory areas  a singleton immortal memory which is never subject to garbage collection  scoped memory in to which schedulable objects can enter and leave. When there are no schedulable objects active in a scoped memory area, all the objects are destroyed and their memory reclaimed.

© Andy Wellings, 2004 Further Reading and Exercises  Scoped memory areas are one of the most controversial aspects of the RTSJ - look on the web to find out why.