SlideShare a Scribd company logo
1 of 18
Download to read offline
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

More Related Content

Viewers also liked

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 RubyMotionMichael Denomy
 
Memory management in Objective C
Memory management in Objective CMemory management in Objective C
Memory management in Objective CNeha Gupta
 
iOS Memory Management Basics
iOS Memory Management BasicsiOS Memory Management Basics
iOS Memory Management BasicsBilue
 
Memory Management
Memory ManagementMemory Management
Memory ManagementVisakh V
 

Viewers also liked (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
 

Similar to Memory Management in RubyMotion

Introduction of vertical crawler
Introduction of vertical crawlerIntroduction of vertical crawler
Introduction of vertical crawlerJinglun Li
 
Vulnerability, exploit to metasploit
Vulnerability, exploit to metasploitVulnerability, exploit to metasploit
Vulnerability, exploit to metasploitTiago Henriques
 
Ruby and Distributed Storage Systems
Ruby and Distributed Storage SystemsRuby and Distributed Storage Systems
Ruby and Distributed Storage SystemsSATOSHI TAGOMORI
 
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 & ProfitsHarsh Bothra
 
Ruby performance - The low hanging fruit
Ruby performance - The low hanging fruitRuby performance - The low hanging fruit
Ruby performance - The low hanging fruitBruce Werdschinski
 
Orthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable CodeOrthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable Codersebbe
 
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 TitaniumTechday7
 
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 WebsiteAcquia
 
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 againMobileFest2018
 
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 TipsAlex Blom
 
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_2012Ashish Bhasin
 
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 androidStanojko Markovik
 
Reviewing CPAN modules
Reviewing CPAN modulesReviewing CPAN modules
Reviewing CPAN modulesneilbowers
 
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 Courseeightbit
 
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 Courseeightbit
 
Killing Shark-Riding Dinosaurs with ORM
Killing Shark-Riding Dinosaurs with ORMKilling Shark-Riding Dinosaurs with ORM
Killing Shark-Riding Dinosaurs with ORMOrtus Solutions, Corp
 
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 GuideContinuent
 
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 DevelopmentBelighted
 

Similar to 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
 

More from Michael Denomy

Intro to automated testing
Intro to automated testingIntro to automated testing
Intro to automated testingMichael Denomy
 
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 2015Michael Denomy
 
Writing Maintainable Tests with PageObjects
Writing Maintainable Tests with PageObjectsWriting Maintainable Tests with PageObjects
Writing Maintainable Tests with PageObjectsMichael Denomy
 
Aikido, Controller Tests, and LinkedIn
Aikido, Controller Tests, and LinkedInAikido, Controller Tests, and LinkedIn
Aikido, Controller Tests, and LinkedInMichael Denomy
 
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 AcademyMichael Denomy
 
Introduction to Test Driven Development
Introduction to Test Driven DevelopmentIntroduction to Test Driven Development
Introduction to Test Driven DevelopmentMichael Denomy
 

More from 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
 

Recently uploaded

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
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...Miguel Araújo
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
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 Takeoffsammart93
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 

Recently uploaded (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
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
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 

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