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

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
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
 
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 productivityPrincipled Technologies
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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 Nanonetsnaman860154
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 

Recently uploaded (20)

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
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?
 
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
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
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
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 

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