Avoid memory leaks
using unit tests
Swift Delhi Meetup - Chapter 15
Who am I?





Raunak Talwar
What I do?





Software Engineer(iOS) 

Expedia, Gurugram

++

Swift Meetups
Memory Management in
iOS?
• Doesn’t use garbage collection (like Java and
C#)
• Reference counting based - (Retain count)

Retain count
• Every object has a retain count- counts how
many other objects want to keep the object
• When retain count reaches zero - object is
deallocated.
Reference counting types
1. Manual Reference counting 🙁

2. ARC - iOS5-2011 😋
Manual Reference Counting
•
Example
autorelease?
Autoreleasepool?
• Autorelease/Autorelasepool?
• The autorelease pool is a convenience that
defers sending an explicit release message to
an object until “later”, with the responsibility of
freeing the memory allocated to objects added
to an autorelease pool devolved onto cocoa
framework.
• This saves a lot of manual release retain.
Autorelasepool?🤔
• Autorelease pool :
• List of objects to send release message later
in the time.
• [myObject autorelease] -> adds objects to
autorelease pool.
• Autorelease pool is always drained at the end
of current runloop.
ARC?😍
• Came with release of iOS5
• No need to remember when to use retain,
release and autorelease.
• Compiler automatically inserts the appropriate method
calls at compile time.
• Important to note that ARC is a precompiltation step.
• Conclusion is reference counting is not disappeared,
but is automated.
With ARC enabled.
• This code







Will be transformed during pre-compilation step
into:
Retain Cycle?
• Retain Cycle is the condition When 2 objects
keep a reference to each other and
are retained, it creates a retain cycle since both
objects try to retain each other, making it
impossible to release causing memory leaks.
Retain Cycle%
Memory leak?🐛
• Memory leak - memory is allocated but never
deallocated.
• Drawbacks of memory leaks
• It’s a finite resource
• When a system is out of memory ->
performance degrades.
EXAMPLE?

SampleProject
How can we avoid it using unit
tests?🤔
• Writing unit test of object creation.
• It asserts that when an object is created with
dependency injection. It doesn’t create retain
cycle within it.
What is unit test?
• Takes input -> Runs method -> Output -> Assertion
• Here we will assert object deallocation after its scope ends.
Deallocaction Test
• An XCTestCase Extension
How to write a dealloc test
Demo
Benefits?😎
• Helps you avoid writing code that contain
reference cycles.
• Dev-time feedback and boost developer
confidence.
• Easy to write
• Fast - (as it’s unit test)
• Avoid you run your instruments and debug deeper.
Sample Project links
• https://github.com/raunakrocks/
ManualMemoryManagement-1
• https://github.com/raunakrocks/SampleProject
Thanks!!!

(

https://twitter.com/raunaktalwar00

Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15

  • 1.
    Avoid memory leaks usingunit tests Swift Delhi Meetup - Chapter 15
  • 2.
    Who am I?
 
 
 RaunakTalwar What I do?
 
 
 Software Engineer(iOS) 
 Expedia, Gurugram
 ++
 Swift Meetups
  • 3.
    Memory Management in iOS? •Doesn’t use garbage collection (like Java and C#) • Reference counting based - (Retain count)

  • 4.
    Retain count • Everyobject has a retain count- counts how many other objects want to keep the object • When retain count reaches zero - object is deallocated.
  • 5.
    Reference counting types 1.Manual Reference counting 🙁
 2. ARC - iOS5-2011 😋
  • 6.
  • 7.
  • 8.
    Autoreleasepool? • Autorelease/Autorelasepool? • Theautorelease pool is a convenience that defers sending an explicit release message to an object until “later”, with the responsibility of freeing the memory allocated to objects added to an autorelease pool devolved onto cocoa framework. • This saves a lot of manual release retain.
  • 9.
    Autorelasepool?🤔 • Autorelease pool: • List of objects to send release message later in the time. • [myObject autorelease] -> adds objects to autorelease pool. • Autorelease pool is always drained at the end of current runloop.
  • 10.
    ARC?😍 • Came withrelease of iOS5 • No need to remember when to use retain, release and autorelease. • Compiler automatically inserts the appropriate method calls at compile time. • Important to note that ARC is a precompiltation step. • Conclusion is reference counting is not disappeared, but is automated.
  • 11.
    With ARC enabled. •This code
 
 
 
 Will be transformed during pre-compilation step into:
  • 12.
    Retain Cycle? • RetainCycle is the condition When 2 objects keep a reference to each other and are retained, it creates a retain cycle since both objects try to retain each other, making it impossible to release causing memory leaks.
  • 13.
  • 14.
    Memory leak?🐛 • Memoryleak - memory is allocated but never deallocated. • Drawbacks of memory leaks • It’s a finite resource • When a system is out of memory -> performance degrades.
  • 15.
  • 16.
    How can weavoid it using unit tests?🤔 • Writing unit test of object creation. • It asserts that when an object is created with dependency injection. It doesn’t create retain cycle within it.
  • 17.
    What is unittest? • Takes input -> Runs method -> Output -> Assertion • Here we will assert object deallocation after its scope ends.
  • 18.
    Deallocaction Test • AnXCTestCase Extension
  • 19.
    How to writea dealloc test
  • 20.
  • 21.
    Benefits?😎 • Helps youavoid writing code that contain reference cycles. • Dev-time feedback and boost developer confidence. • Easy to write • Fast - (as it’s unit test) • Avoid you run your instruments and debug deeper.
  • 22.
    Sample Project links •https://github.com/raunakrocks/ ManualMemoryManagement-1 • https://github.com/raunakrocks/SampleProject
  • 23.