18
Memory Management in RubyMotion Some things you should know when coming from Rails Michael Denomy BostonMotion Meetup March 25, 2014

Memory Management in RubyMotion

  • Upload
    mdenomy

  • View
    1.907

  • Download
    3

Embed Size (px)

DESCRIPTION

If you are a Rails developer moving to RubyMotion, you need to understand the differences in how memory is managed between the two frameworks. In this talk, we'll discuss how memory management works in iOS, including ARC (Automatic Reference Counting), and how that is different from the garbage collection mechanisms in Ruby MRI. We'll also look at a few "gotchas" that can lead to memory leaks and what you can do to track them down.

Citation preview

Page 1: Memory Management in RubyMotion

Memory Management in RubyMotionSome things you should know when

coming from Rails

Michael Denomy BostonMotion Meetup March 25, 2014

Page 2: Memory Management in RubyMotion

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

Page 3: Memory Management in RubyMotion

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.

Page 4: Memory Management in RubyMotion

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

Page 5: Memory Management in RubyMotion

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

Page 6: Memory Management in RubyMotion

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)

Page 7: Memory Management in RubyMotion

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”

Page 8: Memory Management in RubyMotion

Retain Cycles

Page 9: Memory Management in RubyMotion

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

Page 10: Memory Management in RubyMotion

Using WeakRef To Avoid Retain Cycle

Page 11: Memory Management in RubyMotion

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

Page 12: Memory Management in RubyMotion

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

Page 13: Memory Management in RubyMotion

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

Page 14: Memory Management in RubyMotion

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!!!

Page 15: Memory Management in RubyMotion

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

Page 16: Memory Management in RubyMotion

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

Page 17: Memory Management in RubyMotion

Questions?

Page 18: Memory Management in RubyMotion

Memory Management in RubyMotionSome things you should know when

coming from Rails

Michael Denomy BostonMotion Meetup March 25, 2014