SlideShare une entreprise Scribd logo
1  sur  45
Télécharger pour lire hors ligne
Memory Management in Titanium Apps 
TIM POULSEN 
SOFTWARE ENGINEER, TOOLING TEAM 
@skypanther
What are our goals? 
No crashes 
Fast, responsive apps 
Do more 
Longer battery life 
Happy users! 
<0.1 second = “instant” 
<1 second = “uninterrupted” 
~10 seconds = user loses attention 
-- Nielsen Norman Group
Topics I’ll cover 
Memory limits 
What uses memory 
Garbage collection 
Garbage is good 
Alloy specifics 
Profiling 
http://muppet.wikia.com/wiki/I_Love_Trash
Memory limits 
Pre-ICS ~16-24 MB 
Nexus 4 ~96 MB 
“Large heap” enabled ~128 MB+ 
iPhone 4 ~50 MB 
iPhone 5/5s ~100 MB 
iPad2 ~50 MB 
iPad 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 
Garbage 
Memory Management Masterclass, AddyOsmani https://www.youtube.com/watch?v=LaxbdIyBkL0
Garbage Collection 
Obj 
3 
Obj 1 Obj 2 Obj 4 
Live 
Reserve 
Obj 5 
GC Pause 
Memory Management Masterclass, AddyOsmani https://www.youtube.com/watch?v=LaxbdIyBkL0
JavaScript Mark & Sweep 
Obj 
3 
Obj 1 Obj 2 Obj 4 
Live 
Reserve 
Obj 5 
Memory Management Masterclass, AddyOsmani https://www.youtube.com/watch?v=LaxbdIyBkL0
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 interval 
Slower & less frequent 
iOS 
Manual allocation/de-allocation by core SDK 
ARC supported in modules
Garbage Collection Takeaways 
Every allocation gets you closer to a GC pause 
More objects = more time for GC 
GC is automatic 
JavaScript and native processes 
to consider 
http://muppet.wikia.com/wiki/I_Love_Trash
Efficient and garbage-friendly
Efficient and GC friendly 
Watch those images 
Avoid global variables 
Don’t create variables you don’t need to 
Clean up after yourself 
A couple of extras...
Images 
Decompressed in memory 
File format doesn’t matter! 
Pixels and color depth do 
Use scaled resolutions 
Lazy load them 
Remove them when you can 
Don’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 
“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 
A 1536 x 2048 image ~ 12 MB
Custom fonts
Don’t use global variables 
Scope - global & function 
Globals are bad: 
Can’t be garbage collected 
Collisions (unintended value changes) 
Watch for accidental globals 
Use 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 undefined 
Let JavaScript clean up for you 
Avoid tight loops
IIFEs (self-calling functions)
Too tight
Process data in the right place 
Data Data 
SQLite
Android specifics 
Use the R bitmaps 
Stop services 
Clean up in onPause() or onuserleavehint event 
Especially sensitive to rapid allocation/de-allocation
Alloy considerations
Alloy considerations 
Use the framework 
Model/Collection references 
Just don’t – er, clean-up functions 
Remember the basics
Use the framework 
config.json  Alloy.CFG.foo 
Alloy.Globals -> delete Alloy.Globals.foo 
Compiler / platform directives 
(OS_IOS, OS_ANDROID, etc.)
But don’t abuse it 
Don’t put images or large objects into Alloy.Globals 
Don’t declare variables in alloy.js 
Controller 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 stuff 
But clean up after yourself 
Profile your apps
Thank You 
TIM POULSEN 
@skypanther

Contenu connexe

Tendances

Building testable chrome extensions
Building testable chrome extensionsBuilding testable chrome extensions
Building testable chrome extensionsSeth McLaughlin
 
SeConf_Nov2016_London
SeConf_Nov2016_LondonSeConf_Nov2016_London
SeConf_Nov2016_LondonPooja Shah
 
How React Native, Appium and me made each other shine @ContinuousDeliveryAmst...
How React Native, Appium and me made each other shine @ContinuousDeliveryAmst...How React Native, Appium and me made each other shine @ContinuousDeliveryAmst...
How React Native, Appium and me made each other shine @ContinuousDeliveryAmst...Wim Selles
 
Building a Desktop Streaming console with Electron and ReactJS
Building a Desktop Streaming console with Electron and ReactJSBuilding a Desktop Streaming console with Electron and ReactJS
Building a Desktop Streaming console with Electron and ReactJSEmanuele Rampichini
 
OGDC2012 Cross-Platform Development On Mobile Devices_Mr.Takaaki Mizuno_DeNA
OGDC2012 Cross-Platform Development On Mobile Devices_Mr.Takaaki Mizuno_DeNAOGDC2012 Cross-Platform Development On Mobile Devices_Mr.Takaaki Mizuno_DeNA
OGDC2012 Cross-Platform Development On Mobile Devices_Mr.Takaaki Mizuno_DeNABuff Nguyen
 
Desktop Apps in a Javascript World - Electron
Desktop Apps in a Javascript World - ElectronDesktop Apps in a Javascript World - Electron
Desktop Apps in a Javascript World - ElectronMarc MacLeod
 
Get Hip with JHipster: Spring Boot + AngularJS + Bootstrap - Angular Summit 2015
Get Hip with JHipster: Spring Boot + AngularJS + Bootstrap - Angular Summit 2015Get Hip with JHipster: Spring Boot + AngularJS + Bootstrap - Angular Summit 2015
Get Hip with JHipster: Spring Boot + AngularJS + Bootstrap - Angular Summit 2015Matt Raible
 
SydPHP May 2012 - Deployment
SydPHP May 2012 - DeploymentSydPHP May 2012 - Deployment
SydPHP May 2012 - DeploymentGraham Weldon
 
Building a Desktop Streaming console with Node.js and WebKit
Building a Desktop Streaming console with Node.js and WebKitBuilding a Desktop Streaming console with Node.js and WebKit
Building a Desktop Streaming console with Node.js and WebKitEmanuele Rampichini
 
Electron. Build cross platform desktop apps with web technologies!
Electron. Build cross platform desktop apps with web technologies!Electron. Build cross platform desktop apps with web technologies!
Electron. Build cross platform desktop apps with web technologies!*instinctools
 
No Va Taig April 7 2010
No Va Taig April 7 2010No Va Taig April 7 2010
No Va Taig April 7 2010rudy regner
 
Building Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in RailsBuilding Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in RailsJim Jeffers
 
Avoiding Common Pitfalls in Ember.js
Avoiding Common Pitfalls in Ember.jsAvoiding Common Pitfalls in Ember.js
Avoiding Common Pitfalls in Ember.jsAlex Speller
 
Building desktop applications with web technologies - ELECTRON the easy way
Building desktop applications with web technologies - ELECTRON the easy wayBuilding desktop applications with web technologies - ELECTRON the easy way
Building desktop applications with web technologies - ELECTRON the easy waystefanjudis
 
Flu3nt highlights
Flu3nt highlightsFlu3nt highlights
Flu3nt highlightsdswork
 
React For Vikings
React For VikingsReact For Vikings
React For VikingsFITC
 
Front-end Automated Testing
Front-end Automated TestingFront-end Automated Testing
Front-end Automated TestingRuben Teijeiro
 
Calabash Andoird + Calabash iOS
Calabash Andoird + Calabash iOSCalabash Andoird + Calabash iOS
Calabash Andoird + Calabash iOSAnadea
 
Cross-Platform Desktop Apps with Electron
Cross-Platform Desktop Apps with ElectronCross-Platform Desktop Apps with Electron
Cross-Platform Desktop Apps with ElectronDavid Neal
 
Front-End Engineering 101
Front-End Engineering 101Front-End Engineering 101
Front-End Engineering 101Milan Korsos
 

Tendances (20)

Building testable chrome extensions
Building testable chrome extensionsBuilding testable chrome extensions
Building testable chrome extensions
 
SeConf_Nov2016_London
SeConf_Nov2016_LondonSeConf_Nov2016_London
SeConf_Nov2016_London
 
How React Native, Appium and me made each other shine @ContinuousDeliveryAmst...
How React Native, Appium and me made each other shine @ContinuousDeliveryAmst...How React Native, Appium and me made each other shine @ContinuousDeliveryAmst...
How React Native, Appium and me made each other shine @ContinuousDeliveryAmst...
 
Building a Desktop Streaming console with Electron and ReactJS
Building a Desktop Streaming console with Electron and ReactJSBuilding a Desktop Streaming console with Electron and ReactJS
Building a Desktop Streaming console with Electron and ReactJS
 
OGDC2012 Cross-Platform Development On Mobile Devices_Mr.Takaaki Mizuno_DeNA
OGDC2012 Cross-Platform Development On Mobile Devices_Mr.Takaaki Mizuno_DeNAOGDC2012 Cross-Platform Development On Mobile Devices_Mr.Takaaki Mizuno_DeNA
OGDC2012 Cross-Platform Development On Mobile Devices_Mr.Takaaki Mizuno_DeNA
 
Desktop Apps in a Javascript World - Electron
Desktop Apps in a Javascript World - ElectronDesktop Apps in a Javascript World - Electron
Desktop Apps in a Javascript World - Electron
 
Get Hip with JHipster: Spring Boot + AngularJS + Bootstrap - Angular Summit 2015
Get Hip with JHipster: Spring Boot + AngularJS + Bootstrap - Angular Summit 2015Get Hip with JHipster: Spring Boot + AngularJS + Bootstrap - Angular Summit 2015
Get Hip with JHipster: Spring Boot + AngularJS + Bootstrap - Angular Summit 2015
 
SydPHP May 2012 - Deployment
SydPHP May 2012 - DeploymentSydPHP May 2012 - Deployment
SydPHP May 2012 - Deployment
 
Building a Desktop Streaming console with Node.js and WebKit
Building a Desktop Streaming console with Node.js and WebKitBuilding a Desktop Streaming console with Node.js and WebKit
Building a Desktop Streaming console with Node.js and WebKit
 
Electron. Build cross platform desktop apps with web technologies!
Electron. Build cross platform desktop apps with web technologies!Electron. Build cross platform desktop apps with web technologies!
Electron. Build cross platform desktop apps with web technologies!
 
No Va Taig April 7 2010
No Va Taig April 7 2010No Va Taig April 7 2010
No Va Taig April 7 2010
 
Building Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in RailsBuilding Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in Rails
 
Avoiding Common Pitfalls in Ember.js
Avoiding Common Pitfalls in Ember.jsAvoiding Common Pitfalls in Ember.js
Avoiding Common Pitfalls in Ember.js
 
Building desktop applications with web technologies - ELECTRON the easy way
Building desktop applications with web technologies - ELECTRON the easy wayBuilding desktop applications with web technologies - ELECTRON the easy way
Building desktop applications with web technologies - ELECTRON the easy way
 
Flu3nt highlights
Flu3nt highlightsFlu3nt highlights
Flu3nt highlights
 
React For Vikings
React For VikingsReact For Vikings
React For Vikings
 
Front-end Automated Testing
Front-end Automated TestingFront-end Automated Testing
Front-end Automated Testing
 
Calabash Andoird + Calabash iOS
Calabash Andoird + Calabash iOSCalabash Andoird + Calabash iOS
Calabash Andoird + Calabash iOS
 
Cross-Platform Desktop Apps with Electron
Cross-Platform Desktop Apps with ElectronCross-Platform Desktop Apps with Electron
Cross-Platform Desktop Apps with Electron
 
Front-End Engineering 101
Front-End Engineering 101Front-End Engineering 101
Front-End Engineering 101
 

Similaire à TiConnect: Memory Management in Titanium apps

Production debugging web applications
Production debugging web applicationsProduction debugging web applications
Production debugging web applicationsIdo Flatow
 
Production Debugging War Stories
Production Debugging War StoriesProduction Debugging War Stories
Production Debugging War StoriesIdo Flatow
 
Real World Single Page App - A Knockout Case Study
Real World Single Page App - A Knockout Case StudyReal World Single Page App - A Knockout Case Study
Real World Single Page App - A Knockout Case Studyhousecor
 
Performant Django - Ara Anjargolian
Performant Django - Ara AnjargolianPerformant Django - Ara Anjargolian
Performant Django - Ara AnjargolianHakka Labs
 
Low latency in java 8 v5
Low latency in java 8 v5Low latency in java 8 v5
Low latency in java 8 v5Peter Lawrey
 
Making it fast: Zotonic & Performance
Making it fast: Zotonic & PerformanceMaking it fast: Zotonic & Performance
Making it fast: Zotonic & PerformanceArjan
 
Capacity Management from Flickr
Capacity Management from FlickrCapacity Management from Flickr
Capacity Management from Flickrxlight
 
Speeding up mobile web apps
Speeding up mobile web appsSpeeding up mobile web apps
Speeding up mobile web appsIvano Malavolta
 
Why Wordnik went non-relational
Why Wordnik went non-relationalWhy Wordnik went non-relational
Why Wordnik went non-relationalTony Tam
 
Google App Engine for Java v0.0.2
Google App Engine for Java v0.0.2Google App Engine for Java v0.0.2
Google App Engine for Java v0.0.2Matthew McCullough
 
Azure Functions - Get rid of your servers, use functions!
Azure Functions - Get rid of your servers, use functions!Azure Functions - Get rid of your servers, use functions!
Azure Functions - Get rid of your servers, use functions!QAware GmbH
 
Orlando DNN Usergroup Pres 12/06/11
Orlando DNN Usergroup Pres 12/06/11Orlando DNN Usergroup Pres 12/06/11
Orlando DNN Usergroup Pres 12/06/11Jess Coburn
 
GWT is Smarter Than You
GWT is Smarter Than YouGWT is Smarter Than You
GWT is Smarter Than YouRobert Cooper
 
Cvcc performance tuning
Cvcc performance tuningCvcc performance tuning
Cvcc performance tuningJohn McCaffrey
 
Node.js meetup 17.05.2017 ember.js - escape the javascript fatigue
Node.js meetup 17.05.2017   ember.js - escape the javascript fatigueNode.js meetup 17.05.2017   ember.js - escape the javascript fatigue
Node.js meetup 17.05.2017 ember.js - escape the javascript fatigueTobias Braner
 
Optimizing for a faster user experience Pt 2: How-to.
Optimizing for a faster user experience Pt 2: How-to.Optimizing for a faster user experience Pt 2: How-to.
Optimizing for a faster user experience Pt 2: How-to.James Christie
 
The Need for Speed, Optimizing the User Experience pt2 - UXPA Boston 2014 - J...
The Need for Speed, Optimizing the User Experience pt2 - UXPA Boston 2014 - J...The Need for Speed, Optimizing the User Experience pt2 - UXPA Boston 2014 - J...
The Need for Speed, Optimizing the User Experience pt2 - UXPA Boston 2014 - J...Mad*Pow
 
BigDataCloud meetup - July 8th - Cost effective big-data processing using Ama...
BigDataCloud meetup - July 8th - Cost effective big-data processing using Ama...BigDataCloud meetup - July 8th - Cost effective big-data processing using Ama...
BigDataCloud meetup - July 8th - Cost effective big-data processing using Ama...BigDataCloud
 
Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey J On The Beach
 
An introduction to Titanium
An introduction to TitaniumAn introduction to Titanium
An introduction to TitaniumGraham Weldon
 

Similaire à TiConnect: Memory Management in Titanium apps (20)

Production debugging web applications
Production debugging web applicationsProduction debugging web applications
Production debugging web applications
 
Production Debugging War Stories
Production Debugging War StoriesProduction Debugging War Stories
Production Debugging War Stories
 
Real World Single Page App - A Knockout Case Study
Real World Single Page App - A Knockout Case StudyReal World Single Page App - A Knockout Case Study
Real World Single Page App - A Knockout Case Study
 
Performant Django - Ara Anjargolian
Performant Django - Ara AnjargolianPerformant Django - Ara Anjargolian
Performant Django - Ara Anjargolian
 
Low latency in java 8 v5
Low latency in java 8 v5Low latency in java 8 v5
Low latency in java 8 v5
 
Making it fast: Zotonic & Performance
Making it fast: Zotonic & PerformanceMaking it fast: Zotonic & Performance
Making it fast: Zotonic & Performance
 
Capacity Management from Flickr
Capacity Management from FlickrCapacity Management from Flickr
Capacity Management from Flickr
 
Speeding up mobile web apps
Speeding up mobile web appsSpeeding up mobile web apps
Speeding up mobile web apps
 
Why Wordnik went non-relational
Why Wordnik went non-relationalWhy Wordnik went non-relational
Why Wordnik went non-relational
 
Google App Engine for Java v0.0.2
Google App Engine for Java v0.0.2Google App Engine for Java v0.0.2
Google App Engine for Java v0.0.2
 
Azure Functions - Get rid of your servers, use functions!
Azure Functions - Get rid of your servers, use functions!Azure Functions - Get rid of your servers, use functions!
Azure Functions - Get rid of your servers, use functions!
 
Orlando DNN Usergroup Pres 12/06/11
Orlando DNN Usergroup Pres 12/06/11Orlando DNN Usergroup Pres 12/06/11
Orlando DNN Usergroup Pres 12/06/11
 
GWT is Smarter Than You
GWT is Smarter Than YouGWT is Smarter Than You
GWT is Smarter Than You
 
Cvcc performance tuning
Cvcc performance tuningCvcc performance tuning
Cvcc performance tuning
 
Node.js meetup 17.05.2017 ember.js - escape the javascript fatigue
Node.js meetup 17.05.2017   ember.js - escape the javascript fatigueNode.js meetup 17.05.2017   ember.js - escape the javascript fatigue
Node.js meetup 17.05.2017 ember.js - escape the javascript fatigue
 
Optimizing for a faster user experience Pt 2: How-to.
Optimizing for a faster user experience Pt 2: How-to.Optimizing for a faster user experience Pt 2: How-to.
Optimizing for a faster user experience Pt 2: How-to.
 
The Need for Speed, Optimizing the User Experience pt2 - UXPA Boston 2014 - J...
The Need for Speed, Optimizing the User Experience pt2 - UXPA Boston 2014 - J...The Need for Speed, Optimizing the User Experience pt2 - UXPA Boston 2014 - J...
The Need for Speed, Optimizing the User Experience pt2 - UXPA Boston 2014 - J...
 
BigDataCloud meetup - July 8th - Cost effective big-data processing using Ama...
BigDataCloud meetup - July 8th - Cost effective big-data processing using Ama...BigDataCloud meetup - July 8th - Cost effective big-data processing using Ama...
BigDataCloud meetup - July 8th - Cost effective big-data processing using Ama...
 
Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey
 
An introduction to Titanium
An introduction to TitaniumAn introduction to Titanium
An introduction to Titanium
 

Dernier

A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFMichael Gough
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 

Dernier (20)

A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDF
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 

TiConnect: Memory Management in Titanium apps

  • 1. Memory Management in Titanium Apps TIM POULSEN SOFTWARE ENGINEER, TOOLING TEAM @skypanther
  • 2. What are our goals? No crashes Fast, responsive apps Do more Longer battery life Happy users! <0.1 second = “instant” <1 second = “uninterrupted” ~10 seconds = user loses attention -- Nielsen Norman Group
  • 3. Topics I’ll cover Memory limits What uses memory Garbage collection Garbage is good Alloy specifics Profiling http://muppet.wikia.com/wiki/I_Love_Trash
  • 4. Memory limits Pre-ICS ~16-24 MB Nexus 4 ~96 MB “Large heap” enabled ~128 MB+ iPhone 4 ~50 MB iPhone 5/5s ~100 MB iPad2 ~50 MB iPad Air ~100 MB (Limit ~10% of device RAM)
  • 5. 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.
  • 7. Garbage Collection Garbage Memory Management Masterclass, AddyOsmani https://www.youtube.com/watch?v=LaxbdIyBkL0
  • 8. Garbage Collection Obj 3 Obj 1 Obj 2 Obj 4 Live Reserve Obj 5 GC Pause Memory Management Masterclass, AddyOsmani https://www.youtube.com/watch?v=LaxbdIyBkL0
  • 9. JavaScript Mark & Sweep Obj 3 Obj 1 Obj 2 Obj 4 Live Reserve Obj 5 Memory Management Masterclass, AddyOsmani https://www.youtube.com/watch?v=LaxbdIyBkL0
  • 10. JavaScript & native objects Your app’s code Titanium APIs Kroll bridge Native components & libraries Object Object
  • 11. Native memory cleanup Android / Java layer GC: Runs at its own interval Slower & less frequent iOS Manual allocation/de-allocation by core SDK ARC supported in modules
  • 12. Garbage Collection Takeaways Every allocation gets you closer to a GC pause More objects = more time for GC GC is automatic JavaScript and native processes to consider http://muppet.wikia.com/wiki/I_Love_Trash
  • 14. Efficient and GC friendly Watch those images Avoid global variables Don’t create variables you don’t need to Clean up after yourself A couple of extras...
  • 15. Images Decompressed in memory File format doesn’t matter! Pixels and color depth do Use scaled resolutions Lazy load them Remove them when you can Don’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 “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 A 1536 x 2048 image ~ 12 MB
  • 17. Don’t use global variables Scope - global & function Globals are bad: Can’t be garbage collected Collisions (unintended value changes) Watch for accidental globals Use scope to your advantage: Wrap in functions / IIFEs
  • 20. Don’t create variables you don’t need to
  • 21. Don’t create variables you don’t need to
  • 24. Clean up Set to null or undefined Let JavaScript clean up for you Avoid tight loops
  • 27. Process data in the right place Data Data SQLite
  • 28. Android specifics Use the R bitmaps Stop services Clean up in onPause() or onuserleavehint event Especially sensitive to rapid allocation/de-allocation
  • 30. Alloy considerations Use the framework Model/Collection references Just don’t – er, clean-up functions Remember the basics
  • 31. Use the framework config.json  Alloy.CFG.foo Alloy.Globals -> delete Alloy.Globals.foo Compiler / platform directives (OS_IOS, OS_ANDROID, etc.)
  • 32. But don’t abuse it Don’t put images or large objects into Alloy.Globals Don’t declare variables in alloy.js Controller listeners: $.view.on() and $.view.off() Model/Collection listeners: coll.on() and coll.off()
  • 34. Speaking of, just don’t...
  • 35. Well, at least do a good job...
  • 36. Remember the basics: don’t create variables unnecessarily
  • 37. Remember the basics: clean up after yourself
  • 38. Remember the basics: even if you have to get tricky
  • 39. Profiling and testing github.com/skypanther/LeakyApp
  • 44. Takeaways Limit images, lazy load, use fonts ... I love trash (aka: code with GC in mind) Let Alloy do its stuff But clean up after yourself Profile your apps
  • 45. Thank You TIM POULSEN @skypanther

Notes de l'éditeur

  1. Very squishy figures ... They’re probably not right but they’re in the ballpark Requesting “large heap” on Android is generally not recommended, certainly not as a simple “out of memory fix” as it is problematic on many devices, plus not guaranteed.
  2. See docs.appcelerator.com
  3. Let’s get a little esoteric here for a few...
  4. Circles represent objects, lines are references There can be multiple references to an object Nulling a reference breaks the line But an object can’t be removed until all references to it are removed
  5. Addy referred to these as the To and From spaces Allocating obj 5 would cross GC threshold Your app is paused and GC process begins
  6. Live & Reserve spaces swapped (internal references are changed) JS interpreter sweeps memory & marks live objects It copies living objects to the live space This is the slow part of GC Orphaned objects are torn down Your app is resumed and Obj 5 can be allocated now
  7. See docs.appcelerator.com
  8. We’ll talk more later about how to mark objects as collectible Android Java layer also uses a garbage collection method of its own. It runs less frequently than JavaScript’s. This can cause issues where the JS layer cleans up objects while the associated proxies remain valid. Our current iOS SDK does not implement ARC. Proxies are manually allocated and de-allocated. This causes fewer issues like described for Android/Java.
  9. Every object you create eats up your budget and gets you close to a garbage collection pause Once that pause is hit, the more objects in memory the longer the GC pause GC is automatic, with no way for you to start or prevent Don’t forget that the native objects must be cleaned up too, and each platform handles that in its own way.
  10. Let’s talk about some ways to play nice with garbage collection as well as write memory-efficient apps
  11. Images are decompressed in memory to a bitmap A 200 x 200 px 5kb JPG will form just as big a bitmap as a 200x200 px 50kb GIF – it’s not the file size that matters but the number of pixels and the number of bits/pixel (color depth) that do Load images only when you need them Remove them from the screen and destroy their associated objects to free memory as soon as you can When possible, don’t use images – views and icon fonts are two ways you can avoid using images.
  12. Icon fonts are a great way to: Provide resolution-independent assets (no scaling, no mult. assets, easy to recolor) Reduce memory Reduce package size
  13. Let’s get into some coding principles
  14. var keyword required to make variable local to a function There are other ways to make globals unintentionally
  15. CommonJS modules are a great way to enforce scope They are essentially functions that return an object with a standard interface A singleton object might be a better way to pass values throughout your app Will consume some memory, but eliminates other problems from globals
  16. If you don’t create a variable, you don’t have to remember to clean it up. Create a variable reference only when you need to manipulate the object in some way: add child views, hide/show it later, call its methods, etc.
  17. Where this becomes a problem is in loops like this, which create a lot of objects that need to be cleaned up Remember Android does this slowly / differently than the JS level For the last option: If possible, pass generic objects and let the native side do all the work. My sample wouldn’t be creating custom TableViewRows and would rely on the native title, etc. properties. Thus, it’s not applicable in every case. Doing so would also require you to manipulate the data to create generic dictionaries that can be passed to TableViews, ListViews, etc.
  18. Screwy way to do this, but it’s here to make a point about closures The defaults object persists – not taking up a lot of memory, but still persist
  19. Using scope to our advantage, we make sure that the names & values & q arrays are GC’d when the function finishes This from the Alloy SQL sync adapter
  20. Especially on Android, something like this will probably run faster in JavaScript than the native side GC can keep up with Could cause a crash with a rather cryptic error message
  21. Whenever possible, do your data processing at the right layer: Filter and manipulate API-generated data in the cloud Sort, select, filter data at the DBMS level This reduces the amount of processing (thus memory) required by your app.
  22. The built-in R resources include resolution-specific optimized bitmaps – so you don’t have to worry about creating resolution-specific icons & buttons. Android will try to keep an app alive even after backgrounded if it started a service. So your app won’t necessarily take more memory to run, but it will use more overall memory and be less likely to be killed. This will slow the user’s device, maybe making it unstable. Users could determine it was your app that was at fault and uninstall or review your app poorly. When possible, destroy objects when the app is put into the background. Onuserleavehint is fired right before the app is paused. OnPause() runs as the app is paused.
  23. The config file is created for you. Use it rather than creating your own global object or singleton module Useful for any values that can be JSON-stringified Alloy.Globals is an object property – you’re not really setting a global per se. Compiler / platform directives can cut large chunks out of your runtime code. Use them!
  24. Avoid wasting memory by limiting what you store in Alloy.Globals Variables & functions defined in alloy.js will be retained throughout your app and could act as globals If you add custom listeners to controller or models/collections, make sure to remove them
  25. You need to destroy collections associated with a controller when you close that controller. References are stored in the global Alloy.Collections array $.destroy() accepts no arguments and destroys all collections associated with that controller
  26. People seem to love clean up functions But most that I’ve seen are crap This one, for example, comes from an “answer” in the Q&A. It removes all the child objects, but ultimately probably does nothing more than slow down the app The original view objects might still exist elsewhere. And simply nulling out the $.items object would delete its children
  27. Fokke’s function is about the best I’ve seen It encapsulates the basics you must do: - destroy collections - remove controller listeners - remove global event listeners - null out and/or clean up child controllers
  28. Just some quick notes on profiling, this isn’t a how-to tutorial
  29. Leaks is gone in Xcode 6. Allocations seems to be sufficient.
  30. Filter on TiUI Goal should be nothing in the Transient column There’s a lot more you can do with Instruments, but not today ...
  31. DDMS is deprecated, stop using it Traditionally, Monitor provided little help beyond a simple object count
  32. The Allocation Tracker is almost useful In this case, I can filter to my target object – row proxies While this isn’t live, I was able to see the count increase as I opened & closed a leaky window Have to do something in your app, click Get Allocations, then the view will be updated