mobiiliohjelmointi kevät 2009 1 2. memory management basics of memory usage in mobile devicesbasics...
Post on 21-Dec-2015
216 views
TRANSCRIPT
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
11
2. Memory Management2. Memory Management
• Basics of memory usage in mobile devicesBasics of memory usage in mobile devices– Static and dynamic allocationStatic and dynamic allocation– Managing memory organizationManaging memory organization
• Memory management in Symbian OSMemory management in Symbian OS• Memory management in Mobile JavaMemory management in Mobile Java• SummarySummary
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
22
RationaleRationale
• Memory usage is a tangling concern in mobile Memory usage is a tangling concern in mobile devicesdevices– All programs use memoryAll programs use memory– In order to reduce costs, only limited amount of memory In order to reduce costs, only limited amount of memory
available in mobile devicesavailable in mobile devices– Also power consumption factor existsAlso power consumption factor exists
• Memory usage is to a great extent determined by Memory usage is to a great extent determined by programmers, although used tools also have an programmers, although used tools also have an effecteffect– The fashion programs are composed is an issueThe fashion programs are composed is an issue– Allocation of variables to memory is an issueAllocation of variables to memory is an issue
• Not an option to blame infrastructure but focus placed Not an option to blame infrastructure but focus placed on application designon application design
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
33
Dynamic allocation Dynamic allocation
• Execution stackExecution stack– Manages control flow of a thread, allocation takes Manages control flow of a thread, allocation takes
places automatically as execution advancesplaces automatically as execution advances– Contains activation records (or stack frames); one Contains activation records (or stack frames); one
per method/function callper method/function call
• HeapHeap– Unstructured pool of memory Unstructured pool of memory – Must be managed by the programmer in C++; Must be managed by the programmer in C++;
Java VM manages this automaticallyJava VM manages this automatically– Risk of garbaging; risk of fragmentationRisk of garbaging; risk of fragmentation
• (Disk)(Disk)
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
44
int todo { return 1;};
int main() { todo();}
main()
todo()
stack
Memory
todo main
source code
returnaddress
Program and StackProgram and Stack
Program image
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
55
Stack and Activation RecordStack and Activation Record
Previous activation record
Next activation record
Return address
Return value
Parameters
Local variables
Other information
Activationrecord
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
66
ExampleExample
struct Sample {struct Sample {
int i;int i;
char c;char c;
};};
How to allocate to different memory locations? How to allocate to different memory locations?
Does it really matter?Does it really matter?
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
77
Sample in MemorySample in Memory
int SInStack() { Sample s; ....}
int SInHeap() { Sample * s; s = new Sample; ....}
s.i
s.c...
...
s
...
...
s.i
s.c...
...Stack Heap
Stack
How about limited stack size?
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
88
Content and goalsContent and goals
• Basics of memory usage in mobile devicesBasics of memory usage in mobile devices– Static and dynamic allocationStatic and dynamic allocation– Managing memory organizationManaging memory organization
• Memory management in Symbian OSMemory management in Symbian OS• Memory management in Mobile JavaMemory management in Mobile Java• SummarySummary
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
99
Static allocationStatic allocation
• Simplest case (in practice usually allocated in heap, but without Simplest case (in practice usually allocated in heap, but without programmer interventions)programmer interventions)
• Variable is statically allocated to a certain location in the Variable is statically allocated to a certain location in the memorymemory
int x;int x;
int * pointer_to_static()int * pointer_to_static() {{ static int y;static int y; return & y;return & y; }}
• Restrictions regarding e.g. information hiding, etc. Restrictions regarding e.g. information hiding, etc.
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
1010
StackStack
• Home for transient objectsHome for transient objects– Allocation and deallocation is automaticAllocation and deallocation is automatic– No need to make an explicit operating system call No need to make an explicit operating system call
for memoryfor memory• References and sharing of data problematicReferences and sharing of data problematic
int * pointer_to_int()int * pointer_to_int() {{ int y;int y; return & y;return & y; }}
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
1111
ExampleExample
xpointer_to_intactivation ...
Callingactivation
x
...
Callingactivation
Runningpointer_to_int
After executingpointer_to_int
stackpointer
stackpointer
Referenceto x
f
...
Callingactivation
Running otherprocedure
stackpointer
Someother
activation
...
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
1212
HeapHeap
• Home for long-living objectsHome for long-living objects– Sharing is less problematic than with stack-based variables, Sharing is less problematic than with stack-based variables,
but errors can still occur (aliasing -> copy-on-write?)but errors can still occur (aliasing -> copy-on-write?)– Large or global objects/data/variables that are needed in all Large or global objects/data/variables that are needed in all
phases of the programphases of the program• Reference passing commonly advocated in mobile Reference passing commonly advocated in mobile
settingsetting• Management of creation and deletion requiredManagement of creation and deletion required
int * pointer_to_int()int * pointer_to_int() {{ return new int(0);return new int(0); }}
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
1313
ExampleExample
x
...
x
...
Copyofx
...
Activation 1
&x
...
&x
...
&x
...
Full objectin stack
Referencein stack
Activation 2
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
1414
Inheritance and virtual machineInheritance and virtual machine
• Inheritance implies a data structure for maintaining Inheritance implies a data structure for maintaining information needed for dynamic bindinginformation needed for dynamic binding– Virtual function tableVirtual function table
• Virtual machine introduces yet another infrastructure Virtual machine introduces yet another infrastructure on top of a processoron top of a processor– Class loader loads programsClass loader loads programs– Execution engine acts as the scheduler, memory manager, Execution engine acts as the scheduler, memory manager,
and interpreterand interpreter– Run-time dataRun-time data
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
1515
Virtual Function TableVirtual Function Table
class C { int i; char c; virtual void v(); virtual double d();};
i
c
ID
Stack/Heap(Object instantiated
in memory)
d-id
Virtualfunction
table(programmemory,
RAM)
v-id
C.vcode
C.dcode
Methodcode
(programmemory,
RAM)
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
1616
Memory Allocation: What Is Memory Allocation: What Is Allocated Where?Allocated Where?
• ConstantsConstants– ROM if enabledROM if enabled
• Program binariesProgram binaries– ROM (in-place execution; how about e.g. code encryption ROM (in-place execution; how about e.g. code encryption
for protection or upgrades?)for protection or upgrades?)– RAM (additional RAM required for programs)RAM (additional RAM required for programs)
• DataData– RAMRAM– Saving to disk/flash memory? What would the user be Saving to disk/flash memory? What would the user be
expecting?expecting?
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
1717
Content and goalsContent and goals
• Basics of memory usage in mobile devicesBasics of memory usage in mobile devices– Static and dynamic allocationStatic and dynamic allocation– Managing memory organizationManaging memory organization
• Memory management in Symbian OSMemory management in Symbian OS• Memory management in Mobile JavaMemory management in Mobile Java• SummarySummary
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
1818
Managing memory organizationManaging memory organization
• Principle: Principle: – Use the simplest data structure that offers the Use the simplest data structure that offers the
necessary operationsnecessary operations
• Then:Then:– Consider linear data structures instead of Consider linear data structures instead of
distribution (less system calls, design-time distribution (less system calls, design-time management, monitoring option, cache, index management, monitoring option, cache, index size)size)
– Consider other means of benefitting from memory Consider other means of benefitting from memory layout (some basic principles to follow)layout (some basic principles to follow)
– Consider packing Consider packing
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
1919
Non-linear and linear data Non-linear and linear data structurestructure
Program’s address spaceProgram’s address space
List-based data structure Linear data structure
Cache window
Cache window
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
2020
Basic PrinciplesBasic Principles
• Allocate all memory at the beginning of a Allocate all memory at the beginning of a programprogram
• Allocate memory for several items even if you Allocate memory for several items even if you only need oneonly need one
• Use standard allocation sizesUse standard allocation sizes• Reuse objectsReuse objects• Release early, allocate lateRelease early, allocate late• Use permanent storage or ROM when Use permanent storage or ROM when
applicableapplicable• Avoid recursion (of uncontrolled depth?)Avoid recursion (of uncontrolled depth?)
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
2121
Consider packingConsider packing
• Use compression with careUse compression with care• Use efficient resource storage formatUse efficient resource storage format• Consider word alignmentConsider word alignment
struct S { struct S {struct S { struct S { char b; // boolean char b; // booleanchar b; // boolean char b; // boolean int i; char c;int i; char c; char c; int i;char c; int i;} }} }
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
2222
PackingPackingbo
ol
int
char
bool
int
char
int
bool
char
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
2323
Content and goalsContent and goals
• Basics of memory usage in mobile devicesBasics of memory usage in mobile devices– Static and dynamic allocationStatic and dynamic allocation– Managing memory organizationManaging memory organization
• Memory management in Symbian OSMemory management in Symbian OS• Memory management in Mobile JavaMemory management in Mobile Java• SummarySummary
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
2424
Naming ConventionsNaming Conventions
• Class names start with CClass names start with C• Kernel class names start with DKernel class names start with D• Type names start with TType names start with T• Mixin class names start with MMixin class names start with M• Enumerated class names start with EEnumerated class names start with E• Resource names start with RResource names start with R• Method names start with a capital letterMethod names start with a capital letter• Names of methods that can throw an exception end with L (or Names of methods that can throw an exception end with L (or
LC)LC)• Simple getters and setters reflect the name of the variableSimple getters and setters reflect the name of the variable• Instance variable names begin with iInstance variable names begin with i• Argument names begin with aArgument names begin with a• Constant names begin with KConstant names begin with K• Automatic variable names begin with lower-case lettersAutomatic variable names begin with lower-case letters
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
2525
DescriptorsDescriptors
• Symbian way of using stringsSymbian way of using strings
_L("Hello"); _L("Hello"); (depreciated except in demos and debugging)(depreciated except in demos and debugging) _LIT(KHelloRom, "Hello"); _LIT(KHelloRom, "Hello"); // String in program binary.// String in program binary.
TBufC<5> HelloStack(KHelloRom); // Data in thread stack.TBufC<5> HelloStack(KHelloRom); // Data in thread stack.
HBufC* helloHeap = KHelloRom.AllocLC(); // Data in heap.HBufC* helloHeap = KHelloRom.AllocLC(); // Data in heap.
• Guards against overflowsGuards against overflows
char userid[8]; // Vanilla C++char userid[8]; // Vanilla C++strcpy(userid, "[email protected]");strcpy(userid, "[email protected]");
TBuf<8> userid; // SymbianTBuf<8> userid; // Symbianuserid = _L("[email protected]");userid = _L("[email protected]");
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
2626
Some memory layoutsSome memory layouts
iLength12
iPtr Hello, World!
iLength12
iMaxLength12
iPtr
(unmodifiable)TPtrC
(modifiable)TPtr
ROM, heap or stack
(unmodifiable)TBufC<12>
iLength12
iBufHello, world!
(Modifiable)TBuf<15>
iLength12
iMaxLength15
iBufHello, world
TDesC TBufC
TDesC TDes TPtr
TDesC TDes TBuf
TDesC TPtrC
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
2727
Using DescriptorsUsing Descriptors
• Use descriptors rather than degenerate to Ttext* Use descriptors rather than degenerate to Ttext* formatformat
• Use TDesC& for argumentsUse TDesC& for arguments– Light-weightLight-weight– Safe (no accidential modifiction)Safe (no accidential modifiction)– Any descriptor can be passedAny descriptor can be passed
• Use Use newnew only with HBufC only with HBufC– Reserve others from stackReserve others from stack
• Type casting is possibleType casting is possible– HBufC::DesHBufC::Des– TPtr, TDesC::AllocTPtr, TDesC::Alloc– HBufC *HBufC *
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
2828
ExceptionsExceptions
• Trap harness rather than standard exceptionsTrap harness rather than standard exceptions
TRAPD(error, BehaveL()); // TRAPD(error, BehaveL()); // trytryif (error != KErrNone) // Exception handlerif (error != KErrNone) // Exception handler { // { // catchcatch if (error == KErrNotSupported) {...}if (error == KErrNotSupported) {...} if (error == KErrUnknown) {...}if (error == KErrUnknown) {...} }}
User::Leave(KOutOfMemory); // User::Leave(KOutOfMemory); // throwthrow
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
2929
Exceptions and AllocationExceptions and Allocation
• All memory allocations use an overridden All memory allocations use an overridden version of version of newnew operator operator
c = new (ELeave) CMyClass();c = new (ELeave) CMyClass();
• Corresponding methodCorresponding method
c = new CMyClass();c = new CMyClass();if (!c) User::Leave(KOutOfMemory);if (!c) User::Leave(KOutOfMemory);return c;return c;
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
3030
Problem: What happens to automatic Problem: What happens to automatic heap-based variables in an exception?heap-based variables in an exception?
Stack Heap
Before an exception
Stack Heap
Memory garbagingafter an exception
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
3131
Cleanup Stack – An Auxiliary Cleanup Stack – An Auxiliary Data StructureData Structure
Stack Heap Cleanup Stack
Cleanup stack enables deallocation during an exception
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
3232
Using Cleanup StackUsing Cleanup Stack
• A programmer responsiblityA programmer responsiblity• Only for automatic variables, never for othersOnly for automatic variables, never for others
CMyClass * c = new CMyClass();CMyClass * c = new CMyClass();CleanupStack::PushL(c)CleanupStack::PushL(c)... c is used... c is usedCleanupStack::Pop(); // cCleanupStack::Pop(); // cdelete c;delete c;c = 0;c = 0;
• Classes derived from CBase get their destructor called, for Classes derived from CBase get their destructor called, for others only memory is deallocatedothers only memory is deallocated
• Also other actions (e.g. CleanupStack::ClosePushL(file);)Also other actions (e.g. CleanupStack::ClosePushL(file);)
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
3333
Two-Phase ConstructionTwo-Phase Construction
• Cleanup stack cannot help in the creation of objectsCleanup stack cannot help in the creation of objects• Therefore:Therefore:
– actual constructor should never fail, and actual constructor should never fail, and – problematic aspects should be executed only after a problematic aspects should be executed only after a
reference to the object has been pushed to the cleanup reference to the object has been pushed to the cleanup stackstack
CData * id = new (ELeave) CData(256);CData * id = new (ELeave) CData(256);
CleanupStack::PushL(id);CleanupStack::PushL(id);id->ConstructL();id->ConstructL();
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
3434
ShorthandsShorthands
CItem::NewL() {CItem::NewL() { CItem * self = new (ELeave) CItem;CItem * self = new (ELeave) CItem; CleanupStack::PushL(self);CleanupStack::PushL(self); self->ConstructL();self->ConstructL(); CleanupStack::Pop(); // selfCleanupStack::Pop(); // self return self;return self;}}
CItem::NewLC() {CItem::NewLC() { CItem * self = new (ELeave) CItem;CItem * self = new (ELeave) CItem; CleanupStack::PushL(self);CleanupStack::PushL(self); self->ConstructL();self->ConstructL();return self;return self;}}
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
3535
Content and goalsContent and goals
• Basics of memory usage in mobile Basics of memory usage in mobile devicesdevices– Static and dynamic allocationStatic and dynamic allocation– Managing memory organizationManaging memory organization
• Memory management in Symbian OSMemory management in Symbian OS
• Memory management in Mobile JavaMemory management in Mobile Java
• SummarySummary
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
3636
MotivationMotivation
public void push(Object e) {public void push(Object e) { ensureCapasity(); // Check slots countensureCapasity(); // Check slots count elements[size++] = e;elements[size++] = e;}}
public Object pop() {public Object pop() { if (size == 0) throw new EmptyStackException();if (size == 0) throw new EmptyStackException(); return elements[--size];return elements[--size];}}
• Ok?Ok?
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
3737
Stack
size
Objects stored in Stack
Object stackObject stack
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
3838
Stack/Vector
size
Objects stored in Stack
Objects stored in Vectorbut not in Stack
Leaking AbstractionLeaking Abstraction
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
3939
UpgradeUpgrade
public Object pop() {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;
}}
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
4040
Rules of Thumb for Mobile JavaRules of Thumb for Mobile Java
• Avoid small classesAvoid small classes• Avoid dependenciesAvoid dependencies• Select size when relevant and manage Select size when relevant and manage
vector/string usagevector/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
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
4141
Example 1Example 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)); }}
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
4242
ResultsResults
• ArrayImp (minimal overhead)ArrayImp (minimal overhead)– Bytes: 8016Bytes: 8016– Objects: 1 Objects: 1
• VectorImp (integers wrapped to objects)VectorImp (integers wrapped to objects)– Bytes: 40000Bytes: 40000– Objects: 2002Objects: 2002
• VectorImpSimple (failures in guessing the size)VectorImpSimple (failures in guessing the size)– Bytes: 52000Bytes: 52000– Objects: 2010Objects: 2010[Hartikainen: Java application and library memory consumption, TUT, 2005][Hartikainen: Java application and library memory consumption, TUT, 2005]
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
4343
Example 2Example 2
static final int AMOUNT = 100;static final int AMOUNT = 100;
public void useString() {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() {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();}}
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
4444
ResultsResults
• UseString (simplest)UseString (simplest)– Bytes: 39000Bytes: 39000– Objects: 450Objects: 450
• UseStringBuffer (optimized)UseStringBuffer (optimized)– Bytes: 304Bytes: 304– Objects: 5Objects: 5
[Hartikainen: Java application and library memory consumption, TUT, 2005][Hartikainen: Java application and library memory consumption, TUT, 2005]
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
4545
Example 3Example 3
• A sample application consisting of 14 A sample application consisting of 14 classes was refactored into a form classes was refactored into a form where only 1 calss was used without where only 1 calss was used without altering the behavioraltering the behavior– 14 classes: 1401914 classes: 14019– 1 class: 74671 class: 7467
[Hartikainen: Java application and library memory consumption, TUT, 2005][Hartikainen: Java application and library memory consumption, TUT, 2005]
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
4646
Content and goalsContent and goals
• Basics of memory usage in mobile devicesBasics of memory usage in mobile devices– Static and dynamic allocationStatic and dynamic allocation– Managing memory organizationManaging memory organization
• Memory management in Symbian OSMemory management in Symbian OS• Memory management in Mobile JavaMemory management in Mobile Java• SummarySummary
MobiiliohjelmointiMobiiliohjelmointiKevät 2009Kevät 2009
4747
SummarySummary• Memory related considerations are a practical Memory related considerations are a practical
necessitynecessity– Even virtual machines require programmer to consider Even virtual machines require programmer to consider
allocation of variables and the use of data structuresallocation of variables and the use of data structures• Design idioms and patterns have been introduced Design idioms and patterns have been introduced
that give general guidelinesthat give general guidelines– Preallocation and static allocation simplify memory Preallocation and static allocation simplify memory
managementmanagement– Linear data structures offer several benefitsLinear data structures offer several benefits– Data packing as the last resortData packing as the last resort
• Mobile development platforms generally assume that Mobile development platforms generally assume that the developers are aware of the most common pitfallsthe developers are aware of the most common pitfalls– Often not adequately documented but must be experimented Often not adequately documented but must be experimented
in practice!in practice!