O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Objective-C Memory Management Essentials

Book Description

Learn and put into practice various memory management techniques in Objective-C to create robust iOS applications

In Detail

Objective-C Memory Management Essentials will familiarize you with the basic principles of Objective-C memory management, to create robust and effective iOS applications. You will begin with a basic understanding of memory management, and why memory leaks occur in an application, moving on to autorelease pools and object creation/storage to get an idea of how memory is allocated. You will also see what ARC (Automatic Reference Counting) is and how it helps in memory management. Finally, you will cover examples on how to use the various tools provided by Xcode to help in memory management. You will also get a basic understanding of Swift, the recently introduced programming language to write interactive and lightning-fast applications.

By the end of this book, you will have all the necessary knowledge on how to effectively memory-manage your application with best practices.

What You Will Learn

  • Understand the theoretical basics of memory management
  • Discover how and why memory leaks occur
  • Use Objective-C to stem memory leaks in your application
  • Familiarize yourself with the various memory debugging tools available in Xcode
  • Implement data persistence using Core Data
  • Understand how ARC helps in memory management
  • Introduce yourself to Swift, the brand new programming language to write effective, lightning-fast applications

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Objective-C Memory Management Essentials
    1. Table of Contents
    2. Objective-C Memory Management Essentials
    3. Credits
    4. About the Authors
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Introduction to Objective-C Memory Management
      1. Why do we need memory management in Objective-C?
      2. An object's ownership and life cycle
      3. Ownership of object and reference counting
      4. What's a memory leak and why pay attention to it?
      5. What is an object within Objective-C?
      6. Summary
    9. 2. Automatic Reference Counting
      1. What is ARC and how does it work?
      2. How ARC looks
      3. Project settings for ARC
      4. A memory model in Objective-C
      5. What you need to know about ARC and weak references
      6. Summary
    10. 3. Using Autorelease Pools
      1. Understanding the autorelease pool mechanism
      2. Autorelease pool mechanism
        1. Reducing peak memory footprint with autorelease pool blocks
      3. An overview of Apple autoreleased classes
        1. NSRunLoop
        2. NSException
      4. ARC and autorelease
      5. Autorelease pool blocks and threads
      6. Summary
    11. 4. Object Creation and Storage
      1. Creation and initialization of objects
      2. What is a class?
        1. Classes
      3. Object immutability
      4. Object mutability
      5. Inheritance
      6. Convenience initializers
      7. An Objective-C programmer's responsibility
      8. The singleton pattern
      9. Creating @property
      10. Creating custom methods
      11. String formatting
      12. Summary
    12. 5. Managing Your Application Data
      1. Device memory
        1. Image optimization
        2. Lazy loading
        3. Control creation
      2. Caching
        1. SDWebImage
        2. Object serialization
      3. SQLite
        1. SQLite versus Core Data
      4. Summary
    13. 6. Using Core Data for Persistence
      1. Why use Core Data?
      2. Understanding Core Data concepts
      3. Putting it into practice
      4. Getting into the code
      5. Saving data into the persistent store
      6. Deleting data from the persistent store
      7. Updating data
      8. Summary
    14. 7. Key-value Programming Approaches
      1. What is key-value coding or KVC?
      2. The NSKeyValueCoding protocol
        1. Compliance of attributes and one-to-one relationships
        2. Compliance of indexed one-to-many relationships
        3. Compliance of unordered many-to-many relationships
        4. Advantages of key-value coding
        5. Disadvantages of key-value coding
      3. Manual subsets of NSKeyValueCoding behavior
        1. Advantages of creating your own lookup path
        2. Disadvantages of creating your own lookup path
      4. Associated objects
        1. Advantages of using associated objects
        2. Disadvantages of using associated objects
      5. Selectors as keys
        1. Advantages of using selectors as keys
        2. Disadvantages of using selectors as keys
      6. Maximum flexibility and handling unusual keys/values
        1. Advantages of doing your own implementation
        2. Disadvantages of doing your own implementation
      7. Key-value observing
        1. Implementing key-value observing
        2. Performance considerations
      8. Summary
    15. 8. Introduction to Swift
      1. Welcome to Swift
      2. Basics of Swift
        1. Variable declaration
        2. Iterating statements
        3. Conditional statements
        4. Functions
        5. Classes and structures in Swift
        6. Closures
      3. Memory management in Swift
      4. Summary
    16. 9. Memory Management and Debugging
      1. Memory leaks
        1. Strong/weak references
        2. Retain cycles
        3. Memory overuse
      2. Using the debugger and breakpoints
      3. Collecting data on your app
      4. Plumbing memory leaks
      5. Using the LLVM / Clang Static Analyzer
        1. Using NSZombie
      6. Summary
    17. 10. Tips and Tricks for Memory Management
      1. Objective-C, C, and memory management
      2. Getters and setters
      3. The property attribute in Objective-C
      4. Performance guidelines
      5. Don't overthink about memory management
      6. When to avoid KVC and KVO
      7. Summary
    18. 11. Features of Xcode 6
      1. Introducing Xcode 6
      2. What's new in storyboard
        1. Allowing storyboard or the NIB file to replace launch images
          1. Launching images from your app
        2. Universal storyboards
      3. Debugging in Xcode 6
        1. Debug gauges
      4. What's new in Interface Builder
      5. Playground for Swift
      6. Summary
    19. Index