SlideShare une entreprise Scribd logo
1  sur  18
Télécharger pour lire hors ligne
Memory Management
in RubyMotion
Some things you should know when
coming from Rails
Michael Denomy
BostonMotion Meetup
March 25, 2014
About Me
• Software developer @TerribleLabs
• Lover of TDD and little ‘a’ agile
• Experienced Rails developer, budding RubyMotion
developer
• mdenomy on twitter, gmail, github
• Blog at http://mdenomy.wordpress.com
Memory Management in
Ruby (MRI)
• You probably haven’t had to think about memory
management a lot in Ruby (or other languages).
• Ruby automatically allocates memory for you and
frees it up using garbage collection mechanism.
• Ruby uses a “mark and sweep” algorithm to find
objects that are actively being used and ones that
are available to be freed up.
Mark and Sweep
(the simplified version)
• GC walks your code to find
“active” objects
• Objects no longer used are
swept away
• MRI 2.1, JRuby, and Rubinius use more sophisticated
“generational” mechanisms
• Check out Pat Shaughnessy’s “Ruby Under
a Microscope” for more GC goodness
Memory Management in iOS
• Objective C uses “reference counting” to determine
when objects can be freed up.
• The number of references to each object is tracked
• set to 1 when the object is created
• incremented when another object references it
• decremented when an object releases it
• available to be freed up when count reaches zero
The Dark Ages
• Developers manually managed reference count in
Objective-C using “Manual Retain-Release MRR”
• “retain” incremented reference count
• “release” decremented reference count
• Under the covers, retain/release still used, but no
longer need to manually do it since Automatic
Reference Counting (ARC)
Automatic Reference Counting
(ARC)
• In ARC, calls to retain and release are inserted into the
code automatically at compile time
• References can be strong or weak
• strong - counted as an owner in the “retain-release”
cycle
• weak - not an owner in “retain-release” cycle. Gets
set to nil if object pointed to is released.
• Weak references allow us to avoid “retain cycles”
Retain Cycles
What Does This Have To Do
With RubyMotion
• Still bound by memory management constructs of
Objective-C
• Need to avoid retain cycles
• WeakRef class allows us to create Objective-C
weak references to avoid retain cycles
Using WeakRef To Avoid Retain Cycle
How Do We Detect
Memory Leaks
• Xcode Instruments can be used to examine
allocations, memory usage, leaks, etc.
• Launch from command line
rake profile # Same as profile:simulator
rake profile:device # Run a build on the device through Instruments
rake profile:device:templates # List all built-in device Instruments templates
rake profile:simulator # Run a build on the simulator through Instruments
rake profile:simulator:templates # List all built-in Simulator Instruments templates
• Can also attach to running instance
Can We Please
See Some Code
• Very simple example of creating a retain cycle
• No extra gems to keep memory “noise” to a
minimum
• https://github.com/mdenomy/motion-weakref-demo
• See README and labels for different conditions
• Use Xcode Instruments to examine memory usage
Memory Usage ‘notifier’ tag
• No retain cycle because Notifier does not have reference
to ToDoItem
• Memory increases on create item, but returns to baseline
on delete item
Memory Usage ‘retain-cycle’ tag
• Notifier has a strong reference to parent ToDoItem,
creating retain cycle
• Memory increases each call to create item, but is not freed
on delete item. Memory leak!!!
Memory Usage ‘weak-ref’ tag
• Notifier has a weak reference to parent ToDoItem, so no
retain cycle.
• Memory usage follows expected pattern of increasing on
create item and returning to baseline on delete item
Summary
• Reference counting creates the possibility to create
memory leaks via retain cycles.
• Avoid retain cycles by using WeakRef in
RubyMotion when child needs reference to parent
• Xcode Instruments can help identify memory leaks
or other allocation errors
Questions?
Memory Management
in RubyMotion
Some things you should know when
coming from Rails
Michael Denomy
BostonMotion Meetup
March 25, 2014

Contenu connexe

En vedette

En vedette (6)

From Ruby on Rails to RubyMotion - Writing your First iOS App with RubyMotion
From Ruby on Rails to RubyMotion - Writing your First iOS App with RubyMotionFrom Ruby on Rails to RubyMotion - Writing your First iOS App with RubyMotion
From Ruby on Rails to RubyMotion - Writing your First iOS App with RubyMotion
 
Ios - Introduction to memory management
Ios - Introduction to memory managementIos - Introduction to memory management
Ios - Introduction to memory management
 
Memory management in Objective C
Memory management in Objective CMemory management in Objective C
Memory management in Objective C
 
iOS Memory Management
iOS Memory ManagementiOS Memory Management
iOS Memory Management
 
iOS Memory Management Basics
iOS Memory Management BasicsiOS Memory Management Basics
iOS Memory Management Basics
 
Memory Management
Memory ManagementMemory Management
Memory Management
 

Similaire à Memory Management in RubyMotion

Orthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable CodeOrthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable Code
rsebbe
 
5 Common Mistakes You are Making on your Website
 5 Common Mistakes You are Making on your Website 5 Common Mistakes You are Making on your Website
5 Common Mistakes You are Making on your Website
Acquia
 
Reviewing CPAN modules
Reviewing CPAN modulesReviewing CPAN modules
Reviewing CPAN modules
neilbowers
 

Similaire à Memory Management in RubyMotion (20)

Introduction of vertical crawler
Introduction of vertical crawlerIntroduction of vertical crawler
Introduction of vertical crawler
 
Vulnerability, exploit to metasploit
Vulnerability, exploit to metasploitVulnerability, exploit to metasploit
Vulnerability, exploit to metasploit
 
Ruby and Distributed Storage Systems
Ruby and Distributed Storage SystemsRuby and Distributed Storage Systems
Ruby and Distributed Storage Systems
 
Weaponizing Recon - Smashing Applications for Security Vulnerabilities & Profits
Weaponizing Recon - Smashing Applications for Security Vulnerabilities & ProfitsWeaponizing Recon - Smashing Applications for Security Vulnerabilities & Profits
Weaponizing Recon - Smashing Applications for Security Vulnerabilities & Profits
 
Ruby performance - The low hanging fruit
Ruby performance - The low hanging fruitRuby performance - The low hanging fruit
Ruby performance - The low hanging fruit
 
Orthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable CodeOrthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable Code
 
Javascript Best Practices and Intro to Titanium
Javascript Best Practices and Intro to TitaniumJavascript Best Practices and Intro to Titanium
Javascript Best Practices and Intro to Titanium
 
5 Common Mistakes You are Making on your Website
 5 Common Mistakes You are Making on your Website 5 Common Mistakes You are Making on your Website
5 Common Mistakes You are Making on your Website
 
Mobile Fest 2018. Алексей Лизенко. Make your project great again
Mobile Fest 2018. Алексей Лизенко. Make your project great againMobile Fest 2018. Алексей Лизенко. Make your project great again
Mobile Fest 2018. Алексей Лизенко. Make your project great again
 
Phonegap Day 2016: Ember/JS & Hybrid Apps Tips
Phonegap Day 2016: Ember/JS & Hybrid Apps TipsPhonegap Day 2016: Ember/JS & Hybrid Apps Tips
Phonegap Day 2016: Ember/JS & Hybrid Apps Tips
 
TechGIG_Memory leaks in_java_webnair_26th_july_2012
TechGIG_Memory leaks in_java_webnair_26th_july_2012TechGIG_Memory leaks in_java_webnair_26th_july_2012
TechGIG_Memory leaks in_java_webnair_26th_july_2012
 
The Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidThe Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with android
 
Reviewing CPAN modules
Reviewing CPAN modulesReviewing CPAN modules
Reviewing CPAN modules
 
CrikeyCon 2015 - iOS Runtime Hacking Crash Course
CrikeyCon 2015 - iOS Runtime Hacking Crash CourseCrikeyCon 2015 - iOS Runtime Hacking Crash Course
CrikeyCon 2015 - iOS Runtime Hacking Crash Course
 
Wahckon[2] - iOS Runtime Hacking Crash Course
Wahckon[2] - iOS Runtime Hacking Crash CourseWahckon[2] - iOS Runtime Hacking Crash Course
Wahckon[2] - iOS Runtime Hacking Crash Course
 
Garbage Collection .Net
Garbage Collection .NetGarbage Collection .Net
Garbage Collection .Net
 
Killing Shark-Riding Dinosaurs with ORM
Killing Shark-Riding Dinosaurs with ORMKilling Shark-Riding Dinosaurs with ORM
Killing Shark-Riding Dinosaurs with ORM
 
Ember - introduction
Ember - introductionEmber - introduction
Ember - introduction
 
Training Slides: Tungsten Replicator AMI - The Getting Started Guide
Training Slides: Tungsten Replicator AMI - The Getting Started GuideTraining Slides: Tungsten Replicator AMI - The Getting Started Guide
Training Slides: Tungsten Replicator AMI - The Getting Started Guide
 
25 Real Life Tips In Ruby on Rails Development
25 Real Life Tips In Ruby on Rails Development25 Real Life Tips In Ruby on Rails Development
25 Real Life Tips In Ruby on Rails Development
 

Plus de Michael Denomy

Plus de Michael Denomy (8)

Intro to automated testing
Intro to automated testingIntro to automated testing
Intro to automated testing
 
Breathe In and Out With Me - Burlington Ruby Conference 2015
Breathe In and Out With Me - Burlington Ruby Conference 2015Breathe In and Out With Me - Burlington Ruby Conference 2015
Breathe In and Out With Me - Burlington Ruby Conference 2015
 
Writing Maintainable Tests with PageObjects
Writing Maintainable Tests with PageObjectsWriting Maintainable Tests with PageObjects
Writing Maintainable Tests with PageObjects
 
Aikido, Controller Tests, and LinkedIn
Aikido, Controller Tests, and LinkedInAikido, Controller Tests, and LinkedIn
Aikido, Controller Tests, and LinkedIn
 
Reinventing Yourself
Reinventing YourselfReinventing Yourself
Reinventing Yourself
 
Design Principles - Michael Denomy at Launch Academy
Design Principles - Michael Denomy at Launch AcademyDesign Principles - Michael Denomy at Launch Academy
Design Principles - Michael Denomy at Launch Academy
 
Debugging rails
Debugging railsDebugging rails
Debugging rails
 
Introduction to Test Driven Development
Introduction to Test Driven DevelopmentIntroduction to Test Driven Development
Introduction to Test Driven Development
 

Dernier

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Dernier (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

Memory Management in RubyMotion

  • 1. Memory Management in RubyMotion Some things you should know when coming from Rails Michael Denomy BostonMotion Meetup March 25, 2014
  • 2. About Me • Software developer @TerribleLabs • Lover of TDD and little ‘a’ agile • Experienced Rails developer, budding RubyMotion developer • mdenomy on twitter, gmail, github • Blog at http://mdenomy.wordpress.com
  • 3. Memory Management in Ruby (MRI) • You probably haven’t had to think about memory management a lot in Ruby (or other languages). • Ruby automatically allocates memory for you and frees it up using garbage collection mechanism. • Ruby uses a “mark and sweep” algorithm to find objects that are actively being used and ones that are available to be freed up.
  • 4. Mark and Sweep (the simplified version) • GC walks your code to find “active” objects • Objects no longer used are swept away • MRI 2.1, JRuby, and Rubinius use more sophisticated “generational” mechanisms • Check out Pat Shaughnessy’s “Ruby Under a Microscope” for more GC goodness
  • 5. Memory Management in iOS • Objective C uses “reference counting” to determine when objects can be freed up. • The number of references to each object is tracked • set to 1 when the object is created • incremented when another object references it • decremented when an object releases it • available to be freed up when count reaches zero
  • 6. The Dark Ages • Developers manually managed reference count in Objective-C using “Manual Retain-Release MRR” • “retain” incremented reference count • “release” decremented reference count • Under the covers, retain/release still used, but no longer need to manually do it since Automatic Reference Counting (ARC)
  • 7. Automatic Reference Counting (ARC) • In ARC, calls to retain and release are inserted into the code automatically at compile time • References can be strong or weak • strong - counted as an owner in the “retain-release” cycle • weak - not an owner in “retain-release” cycle. Gets set to nil if object pointed to is released. • Weak references allow us to avoid “retain cycles”
  • 9. What Does This Have To Do With RubyMotion • Still bound by memory management constructs of Objective-C • Need to avoid retain cycles • WeakRef class allows us to create Objective-C weak references to avoid retain cycles
  • 10. Using WeakRef To Avoid Retain Cycle
  • 11. How Do We Detect Memory Leaks • Xcode Instruments can be used to examine allocations, memory usage, leaks, etc. • Launch from command line rake profile # Same as profile:simulator rake profile:device # Run a build on the device through Instruments rake profile:device:templates # List all built-in device Instruments templates rake profile:simulator # Run a build on the simulator through Instruments rake profile:simulator:templates # List all built-in Simulator Instruments templates • Can also attach to running instance
  • 12. Can We Please See Some Code • Very simple example of creating a retain cycle • No extra gems to keep memory “noise” to a minimum • https://github.com/mdenomy/motion-weakref-demo • See README and labels for different conditions • Use Xcode Instruments to examine memory usage
  • 13. Memory Usage ‘notifier’ tag • No retain cycle because Notifier does not have reference to ToDoItem • Memory increases on create item, but returns to baseline on delete item
  • 14. Memory Usage ‘retain-cycle’ tag • Notifier has a strong reference to parent ToDoItem, creating retain cycle • Memory increases each call to create item, but is not freed on delete item. Memory leak!!!
  • 15. Memory Usage ‘weak-ref’ tag • Notifier has a weak reference to parent ToDoItem, so no retain cycle. • Memory usage follows expected pattern of increasing on create item and returning to baseline on delete item
  • 16. Summary • Reference counting creates the possibility to create memory leaks via retain cycles. • Avoid retain cycles by using WeakRef in RubyMotion when child needs reference to parent • Xcode Instruments can help identify memory leaks or other allocation errors
  • 18. Memory Management in RubyMotion Some things you should know when coming from Rails Michael Denomy BostonMotion Meetup March 25, 2014