Mobiiliohjelmointi Kevät 2009 1 4. Dynamic Linking OverviewOverview Implementation techniquesImplementation techniques PluginsPlugins Managing memory consumptionManaging.

Slides:



Advertisements
Similar presentations
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Advertisements

CS 211 Inheritance AAA.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Chapter 10.
Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Run time vs. Compile time
Mobiiliohjelmointi Kevät b. That’s all folks! IntroductionIntroduction Memory managementMemory management Application developmentApplication development.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Mobiiliohjelmointi Kevät Memory Management Basics of memory usage in mobile devicesBasics of memory usage in mobile devices –Static and dynamic.
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.
Chapter 10 Classes Continued
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Abstract Data Types and Encapsulation Concepts
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
Java Security. Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security Manager.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Pointer Data Type and Pointer Variables
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Java and C++, The Difference An introduction Unit - 00.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
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.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Programming Languages and Paradigms Object-Oriented Programming.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Replay Compilation: Improving Debuggability of a Just-in Time Complier Presenter: Jun Tao.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Examples (D. Schmidt et al)
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Abstract Data Types and Encapsulation Concepts
SPL – PS3 C++ Classes.
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Introduction to Classes and Objects
Presentation transcript:

Mobiiliohjelmointi Kevät Dynamic Linking OverviewOverview Implementation techniquesImplementation techniques PluginsPlugins Managing memory consumptionManaging memory consumption Using DLLsUsing DLLs Mobile Java implementationMobile Java implementation Symbian OS implementationSymbian OS implementation –DLL structure –ECOM SummarySummary

Mobiiliohjelmointi Kevät Motivation Several applications use the same code; no need to include it in all applications separatelySeveral applications use the same code; no need to include it in all applications separately Application-specific tailoring in cases where similar functions are needed (plugins)Application-specific tailoring in cases where similar functions are needed (plugins) Smaller compilations and deliveries; smaller upgradesSmaller compilations and deliveries; smaller upgrades Flexible compositionFlexible composition Enables focused testingEnables focused testing Scoping of system management (smallest unit addressed in e.g. build or release management)Scoping of system management (smallest unit addressed in e.g. build or release management) Work allocationWork allocation

Mobiiliohjelmointi Kevät Dynamically linked libraries Single application fileMultiple files that are loaded dynamically Before loadingAfter loading

Mobiiliohjelmointi Kevät Release Definition with DLLs First action to take in the beginning of the development of a new (or enhanced) systemFirst action to take in the beginning of the development of a new (or enhanced) system Defines all the necessary elements for a complete systemDefines all the necessary elements for a complete system Enables early toolchain development as well as decomposition of the system into different subsystemsEnables early toolchain development as well as decomposition of the system into different subsystems –Test of the build system Some parts can be left open for future extensions (plugins)Some parts can be left open for future extensions (plugins)

Mobiiliohjelmointi Kevät Required Implementation Facilities Dynamic capabilities for …Dynamic capabilities for … –Loading –Unloading –Selection if several possibilities exist Copy in processes’ own memory vs. in-place executionCopy in processes’ own memory vs. in-place execution –Data must be instantiated in any case RestrictionsRestrictions –All code should not be active all the time; memory related problems (e.g. fragmentation) if numerous loading of libraries of different sizes

Mobiiliohjelmointi Kevät Static and Dynamic DLLs Static DLLStatic DLL –(Commonly) Instantiated at application startup –Resides in the memory until the application terminates Dynamic DLL (often referred to as a plugin)Dynamic DLL (often referred to as a plugin) –Loaded and unloaded whenever needed –E.g. different plugin for different messaging types ( /SMS/MMS)

Mobiiliohjelmointi Kevät Challenges Fragmentation of the system into an unmanageable collection of DLLsFragmentation of the system into an unmanageable collection of DLLs –Versioning –Number of allowed configurations –Number of accidentally created configurations –Fractal-like use of plugins Benefits of application-specific DLLs?Benefits of application-specific DLLs? Interface compatibility when market requirements force to change hardware characteristicsInterface compatibility when market requirements force to change hardware characteristics –Platform continuity by freezing the interface for some period of time

Mobiiliohjelmointi Kevät Content and goals OverviewOverview Implementation techniquesImplementation techniques PluginsPlugins Managing memory consumptionManaging memory consumption Using DLLsUsing DLLs Mobile Java implementationMobile Java implementation Symbian OS implementationSymbian OS implementation –DLL structure –ECOM SummarySummary

Mobiiliohjelmointi Kevät Implementing DLLs DLL External interface Code

Mobiiliohjelmointi Kevät Option 1: Offset based Calls DLL (support for plugins)APP LIBOrdinal Code

Mobiiliohjelmointi Kevät Option 2: Using Method Signatures DLL External interface Code methodX() methodY(int i, j) methodZ(string myName)

Mobiiliohjelmointi Kevät Comparison Offset based linking isOffset based linking is –more efficient (no need to look for the method) –more error-prone (what happens when somebody adds a new method?) Signature based linking isSignature based linking is –more natural (one can call methods from different libraries with ease) –more memory hungry (signatures must be saved somewhere)

Mobiiliohjelmointi Kevät Content and goals OverviewOverview Implementation techniquesImplementation techniques PluginsPlugins Managing DLL memory consumptionManaging DLL memory consumption Using DLLsUsing DLLs Mobile Java implementationMobile Java implementation Symbian OS implementationSymbian OS implementation –DLL structure –ECOM SummarySummary

Mobiiliohjelmointi Kevät Plugin Principle Plugin implementations P1 Application P2 P3 P1 Application P3 P2 Application P2 P3 P1 No plugins loaded Plugin P2 loaded Plugin P2 unloaded and P1 loaded Plugin interface Plugin implementations Plugin implementations

Mobiiliohjelmointi Kevät Abstract Factory as Implementation mechanism > AbsDLLA Client > AbsDLLB > AbsFactory ConcFactory1 ConcFactory2 CreateProductA CreateProductB CreateDLLA CreateDLLB CreateDLLA CreateDLLB ConcDLLA1ConcDLLA2ConcDLLB1ConcDLLB2 operAx operAy operBx operBy

Mobiiliohjelmointi Kevät Infrastructure and implementation level concerns Loading and unloading frameworkLoading and unloading framework ResolutionResolution –Searching and selecting the right plugin Policy for registering and removing of plugins to enable dynamic creation of new (or improved) featuresPolicy for registering and removing of plugins to enable dynamic creation of new (or improved) features –Who should be able to do this? –Compatibility issues Plugin use can also introduce side-effectsPlugin use can also introduce side-effects –E.g. Response to a message

Mobiiliohjelmointi Kevät Content and goals OverviewOverview Implementation techniquesImplementation techniques PluginsPlugins Managing DLL memory consumptionManaging DLL memory consumption Using DLLsUsing DLLs Mobile Java implementationMobile Java implementation Symbian OS implementationSymbian OS implementation –DLL structure –ECOM SummarySummary

Mobiiliohjelmointi Kevät Principles 1.Always use the simplest structure you really need! 2.Make all DLLs (and DLL developers) responsible for the memory they allocate Needed for realistically defining a release and its resource consumption in any case!Needed for realistically defining a release and its resource consumption in any case! 3.When in doubt, consider what will eventually happen at the level of implementation Desk exercise to get the first estimateDesk exercise to get the first estimate Running real code as soon as possible to get more realistic figures on actual performanceRunning real code as soon as possible to get more realistic figures on actual performance

Mobiiliohjelmointi Kevät Merging software and data elements as the last resort Merging packages and DLLsMerging packages and DLLs –Reduces overhead of referring to the methods of the packages/DLLs Flattening hierarchiesFlattening hierarchies –Reduces the number of identifiers, reduces the number of virtual function tables Embedding objectsEmbedding objects –Alters the layout of the data structure

Mobiiliohjelmointi Kevät Example: Embedded Pointers next object ref next object ref next object ref object next object next object next

Mobiiliohjelmointi Kevät Content and goals OverviewOverview Implementation techniquesImplementation techniques PluginsPlugins Managing DLL memory consumptionManaging DLL memory consumption Using DLLsUsing DLLs Mobile Java implementationMobile Java implementation Symbian OS implementationSymbian OS implementation –DLL structure –ECOM SummarySummary

Mobiiliohjelmointi Kevät Rules of Thumb for DLL Creation Shareable componentShareable component Variation or management pointVariation or management point –Several different implementations (or alternatives) Test process requirementsTest process requirements –Testing needs a concrete subject –Automated testing with binary deliveries End product of an organizational unitEnd product of an organizational unit –Release management can be eased if compilations are not to be managed as a part of it –Black-box subcontracting

Mobiiliohjelmointi Kevät Content and goals OverviewOverview Implementation techniquesImplementation techniques PluginsPlugins Managing DLL memory consumptionManaging DLL memory consumption Using DLLsUsing DLLs Mobile Java implementationMobile Java implementation Symbian OS implementationSymbian OS implementation –DLL structure –ECOM SummarySummary

Mobiiliohjelmointi Kevät Mobile Java and DLLs All class files can be considered as more or less similar to DLLsAll class files can be considered as more or less similar to DLLs Linking is a built-in feature at class levelLinking is a built-in feature at class level –Performed automatically for a complete class -> Smaller classes have certain advantages –Class memory overhead -> Larger classes have certain advantages No support for plugins due to CLDC restrictionsNo support for plugins due to CLDC restrictions –More capable mobile systems can include also support for plugins; not a technical impossibility in mobile setting

Mobiiliohjelmointi Kevät Average Class File Content (CLDC library classes) Measurements by Hartikainen (Java application and library memory consumption, MSc thesis, Tampere U of Tech, 2005):Measurements by Hartikainen (Java application and library memory consumption, MSc thesis, Tampere U of Tech, 2005): –Metadata 45.4% (about half is debug info) –Strings 34.8% –Bytecode 19.1% –Class field data 0.4% –Instance field data 0.4%

Mobiiliohjelmointi Kevät Effect of compression and different application structures Format14 classes 1 class No compression No compression, obfuscated JAR Jar Obfuscated Pack Pack.gz

Mobiiliohjelmointi Kevät Effect of compression and library structures FormatSize (bytes) Classes JAR Pack Pack.gz tar.gz JXE104919

Mobiiliohjelmointi Kevät Library Considerations Prelinking of most commonly used standard librariesPrelinking of most commonly used standard libraries –Only interface must be visible –Internals can be implemented as e.g. a part of the virtual machine –Uses more memory, but linking/loading becomes easier Structure considerationsStructure considerations –JARring class by class cannot benefit from recurring constructs (e.g. constructors in every class)

Mobiiliohjelmointi Kevät Also programmer actions matter! public void push(Object e) { ensureCapasity(); // Check slots count ensureCapasity(); // Check slots count elements[size++] = e; elements[size++] = e;} public Object pop() { if (size == 0) throw new EmptyStackException(); if (size == 0) throw new EmptyStackException(); return elements[--size]; return elements[--size];} Ok?Ok?

Mobiiliohjelmointi Kevät Stack size Objects stored in Stack Object stack

Mobiiliohjelmointi Kevät Stack/Vector size Objects stored in Stack Objects stored in Vector but not in Stack Leaking Abstraction

Mobiiliohjelmointi Kevät Upgrade public Object pop() { if (size == 0) if (size == 0) throw new EmptyStackException(); throw new EmptyStackException(); Object result = elements[--size]; Object result = elements[--size]; elements[size] = null; elements[size] = null; return result; return result;}

Mobiiliohjelmointi Kevät Rules of Thumb for Mobile Java Avoid small classesAvoid small classes Avoid dependenciesAvoid dependencies Select size when relevant and manage vector/string usageSelect size when relevant and manage vector/string usage Consider using array vs. using vectorConsider using array vs. using vector Use stringBuffer when possibleUse stringBuffer when possible Manage class and object structureManage class and object structure Generate less garbageGenerate less garbage Consider obfuscationConsider obfuscation Handle array initializationHandle array initialization

Mobiiliohjelmointi Kevät Example 1 static final int SIZE = 2000; private void arrayImp() { numbers = new int[SIZE]; for (int i = 0; i < SIZE; i++) { numbers[i] = i; } } private void vectorImp() { numberV = new Vector(SIZE); for (int i = 0; i < SIZE; i++) { numberV.addElement(new Integer(i)); } } private void vectorImpSimple() { numberV2 = new Vector(); // Default size for (int i = 0; i < SIZE; i++) { numberV2.addElement(new Integer(i)); } }

Mobiiliohjelmointi Kevät Results ArrayImp (minimal overhead)ArrayImp (minimal overhead) –Bytes: 8016 –Objects: 1 VectorImp (integers wrapped to objects)VectorImp (integers wrapped to objects) –Bytes: –Objects: 2002 VectorImpSimple (failures in guessing the size)VectorImpSimple (failures in guessing the size) –Bytes: –Objects: 2010 [Hartikainen: Java application and library memory consumption, TUT, 2005]

Mobiiliohjelmointi Kevät Example 2 static final int AMOUNT = 100; public void useString() { String s = “”; String s = “”; for(int i = 0; i < AMOUNT; i++) { for(int i = 0; i < AMOUNT; i++) { s = s + “a”; s = s + “a”; }} public void useStringBuffer() { String s = “”; String s = “”; StringBuffer sb = new StringBuffer(AMOUNT); StringBuffer sb = new StringBuffer(AMOUNT); for(int i = 0; i < AMOUNT; i++) { for(int i = 0; i < AMOUNT; i++) { sb = sb.append(“a”); sb = sb.append(“a”); } s = sb.toString(); s = sb.toString();}

Mobiiliohjelmointi Kevät Results UseString (simplest)UseString (simplest) –Bytes: –Objects: 450 UseStringBuffer (optimized)UseStringBuffer (optimized) –Bytes: 304 –Objects: 5 [Hartikainen: Java application and library memory consumption, TUT, 2005]

Mobiiliohjelmointi Kevät Example 3 A sample application consisting of 14 classes was refactored into a form where only 1 calss was used without altering the behaviorA sample application consisting of 14 classes was refactored into a form where only 1 calss was used without altering the behavior –14 classes: –1 class: 7467 [Hartikainen: Java application and library memory consumption, TUT, 2005]

Mobiiliohjelmointi Kevät Content and goals OverviewOverview Implementation techniquesImplementation techniques PluginsPlugins Managing DLL memory consumptionManaging DLL memory consumption Using DLLsUsing DLLs Mobile Java implementationMobile Java implementation Symbian OS implementationSymbian OS implementation –DLL structure –ECOM SummarySummary

Mobiiliohjelmointi Kevät Offset-based linking DLL (support for plugins)APP LIBOrdinal Code

Mobiiliohjelmointi Kevät Expressed in Code with Macros (IMPORT_C, EXPORT_C) class CQAEng : public CBase {public: IMPORT_C static CQAEng* NewL(); IMPORT_C static CQAEng* NewL(); protected: CQAEng(); CQAEng(); }; EXPORT_C CQAEng * CQAEng::NewL() { CQAEng* self = CQAEng* self = new (ELeave) CQAEng; new (ELeave) CQAEng; CleanupStack::PushL(self); CleanupStack::PushL(self); self->ConstructL(); self->ConstructL(); CleanupStack::Pop(); CleanupStack::Pop(); return self; return self; }CQAEng::CQAEng() { iQuestion = 0; iQuestion = 0; iAnswer = 0; iAnswer = 0; iUsed = EFalse; iUsed = EFalse; }

Mobiiliohjelmointi Kevät Managing Binary Compatibility AbsoluteAbsolute –Do not change the size of a class object –Do not remove anything accessible –Do not rearrange accessible class member data –Do not rearrange the ordinal of exported functions –Do no re-order virtual functions –Do not modify documented semantics of API –Do not remove const –Do not change from pass by value to pass by reference, or vice versa Future-proofFuture-proof –Do not inline functions –Do not expose public or protected member data –Allow object initialization to leave –Override virtual functions that are expected to be overridden in the future –Provide ”spare” member data

Mobiiliohjelmointi Kevät Programmer Options API extensionsAPI extensions Private internals of a class can be modifiedPrivate internals of a class can be modified Access specification can be relaxedAccess specification can be relaxed Pointers can be substituted with references and vice versaPointers can be substituted with references and vice versa Names of exported non-virtual functions can be modifiedNames of exported non-virtual functions can be modified Input can be widened and output can be narrowedInput can be widened and output can be narrowed Specifier const can be appliedSpecifier const can be applied

Mobiiliohjelmointi Kevät Content and goals OverviewOverview Implementation techniquesImplementation techniques PluginsPlugins Managing DLL memory consumptionManaging DLL memory consumption Using DLLsUsing DLLs Mobile Java implementationMobile Java implementation Symbian OS implementationSymbian OS implementation –DLL structure –ECOM SummarySummary

Mobiiliohjelmointi Kevät ECOM Framework Interface client Interface (CCryptoIF) Implementation (CCryptoSW) NewL ECOM Framework REComSession::CreateImplementationL Implements Resolves and instantiates

Mobiiliohjelmointi Kevät ECOM Interface Requirements Abstract class with a set of one or more pure virtual functionsAbstract class with a set of one or more pure virtual functions Provides one or more factory functions to allow the client to instantiate an interface implementation objectProvides one or more factory functions to allow the client to instantiate an interface implementation object Provides means for clients to release it (e.g. destructor, Release, Close,...)Provides means for clients to release it (e.g. destructor, Release, Close,...) TUid data member, which is used internally to identify an implementation instance for cleanup purposesTUid data member, which is used internally to identify an implementation instance for cleanup purposes

Mobiiliohjelmointi Kevät Content and goals OverviewOverview Implementation techniquesImplementation techniques PluginsPlugins Managing DLL memory consumptionManaging DLL memory consumption Using DLLsUsing DLLs Mobile Java implementationMobile Java implementation Symbian OS implementationSymbian OS implementation –DLL structure –ECOM SummarySummary

Mobiiliohjelmointi Kevät Summary Dynamically linked libraries offer a way to save memory, as code can be shared by several unitsDynamically linked libraries offer a way to save memory, as code can be shared by several units It is also possible to use DLLs to management purposes (e.g. release definition and management)It is also possible to use DLLs to management purposes (e.g. release definition and management) Linking can be based on e.g. method signatures (Java) or offset (Symbian OS)Linking can be based on e.g. method signatures (Java) or offset (Symbian OS) Plugins are special DLLs that can be dynamically loaded and unloadedPlugins are special DLLs that can be dynamically loaded and unloaded –Common interface –Implementation for the interface –Framework for loading the interface