Upload
tim-poulsen
View
445
Download
3
Embed Size (px)
DESCRIPTION
Slides from my presentation at the 2014 ConnectJS / TiConnect conference in Atlanta. I cover tips and background info on managing memory and performance in your Titanium apps.
Citation preview
Memory Management in Titanium AppsTIM POULSENSOFTWARE ENGINEER, TOOLING TEAM
@skypanther
What are our goals?
No crashesFast, responsive appsDo moreLonger battery lifeHappy users!
<0.1 second = “instant”<1 second = “uninterrupted”~10 seconds = user loses attention -- Nielsen Norman Group
Topics I’ll cover
Memory limitsWhat uses memoryGarbage collectionGarbage is goodAlloy specificsProfiling
http://muppet.wikia.com/wiki/I_Love_Trash
Memory limits
Pre-ICS ~16-24 MBNexus 4 ~96 MB“Large heap” enabled ~128 MB+
iPhone 4 ~50 MBiPhone 5/5s ~100 MBiPad2 ~50 MBiPad Air ~100 MB
(Limit ~10% of device RAM)
What’s in a Titanium app?
Your app’s code
Titanium APIs
Kroll bridge
Native components & libraries
• Your app’s code• Alloy’s libraries• Graphics• Custom fonts• Data – queries,
network calls, etc.
Garbage Collection
Garbage Collection
Memory Management Masterclass, Addy Osmani https://www.youtube.com/watch?v=LaxbdIyBkL0
Garbage
Garbage Collection
Obj 1 Obj 2 Obj 4Obj
3Live
Reserve
Memory Management Masterclass, Addy Osmani https://www.youtube.com/watch?v=LaxbdIyBkL0
Obj 5
GC Pause
JavaScript Mark & Sweep
Obj 1 Obj 2 Obj 4Ob
j 3Live
Reserve
Memory Management Masterclass, Addy Osmani https://www.youtube.com/watch?v=LaxbdIyBkL0
Obj 5
JavaScript & native objects
Your app’s code
Titanium APIs
Kroll bridge
Native components & libraries
Object
Object
Native memory cleanup
Android / Java layer GC:Runs at its own intervalSlower & less frequent
iOSManual allocation/de-allocation by core SDKARC supported in modules
Garbage Collection Takeaways
Every allocation gets you closer to a GC pauseMore objects = more time for GCGC is automaticJavaScript and native processes to consider
http://muppet.wikia.com/wiki/I_Love_Trash
Efficient and garbage-friendly
Efficient and GC friendly
Watch those imagesAvoid global variablesDon’t create variables you don’t need toClean up after yourselfA couple of extras...
Images
Decompressed in memoryFile format doesn’t matter!Pixels and color depth doUse scaled resolutionsLazy load themRemove them when you canDon’t use them
200 by 200 px image= 40,000 pixels x 32 bits per pixel= 1,280,000 bits= 160,000 Bytes= 156.25 KB= ~1/6th MB
A 1536 x 2048 image ~ 12 MB
“Why not just use xhdpi images across all resolutions and let the device scale to the right size?” -- Q&A user
“I’ve got a table with 200 rows, each with an image and my app crashes. Titanium sucks” -- Q&A user
Custom fonts
Don’t use global variables
Scope - global & functionGlobals are bad:
Can’t be garbage collectedCollisions (unintended value changes)
Watch for accidental globalsUse scope to your advantage:
Wrap in functions / IIFEs
Locals & Globals
CommonJS Singleton
Don’t create variables you don’t need to
Don’t create variables you don’t need to
Closures
Unintended references
Clean up
Set to null or undefinedLet JavaScript clean up for youAvoid tight loops
IIFEs (self-calling functions)
Too tight
Process data in the right place
Data Data
SQLite
Android specifics
Use the R bitmapsStop servicesClean up in onPause() or onuserleavehint eventEspecially sensitive to rapid allocation/de-allocation
Alloy considerations
Alloy considerations
Use the frameworkModel/Collection referencesJust don’t – er, clean-up functionsRemember the basics
Use the framework
config.json Alloy.CFG.fooAlloy.Globals -> delete Alloy.Globals.fooCompiler / platform directives
(OS_IOS, OS_ANDROID, etc.)
But don’t abuse it
Don’t put images or large objects into Alloy.GlobalsDon’t declare variables in alloy.jsController listeners: $.view.on() and $.view.off()
Model/Collection listeners: coll.on() and coll.off()
Model/Collection cleanup
Speaking of, just don’t...
Well, at least do a good job...
Remember the basics: don’t create variables unnecessarily
Remember the basics: clean up after yourself
Remember the basics: even if you have to get tricky
Profiling and testing
github.com/skypanther/LeakyApp
Instruments
Instruments
Monitor
Monitor, almost useful...
Takeaways
Limit images, lazy load, use fonts ...I love trash (aka: code with GC in mind)
Let Alloy do its stuffBut clean up after yourself
Profile your apps
Thank YouTIM POULSEN@skypanther