Find memory issues & leaks in your iOS app without instruments

Overview

HeapInspector

Find memory issues & leaks in your iOS app

Build Status CocoaPods Version CocoaPods Platform Carthage Compatible Twitter

HeapInspector is a debug tool that monitors the memory heap with backtrace recording in your iOS app. You can discover memory leaks, no longer used objects, abandoned memory and more issues directly on your device without ever starting Instruments.

Memory heap snapshots with backtrace record

Basically you can inspect the entire heap and see all living objects of your iOS app.
To be more precise you can record the heap for a specific part of the app. For instance when navigating through the menu. Like in Apple's Instruments the snapshot compares the heap before you started recording. For instance you can start the snapshot before you push a new UIViewController onto your UINavigationController stack and stop after popping the UIViewController. With HeapInspector and heap snapshots you can identify:

  • Leaking objects
  • Retain cycles
  • Living objects that are no longer needed
  • static objects like singletons or cached UIImage
  • Dirty memory and your objects on the heap

HeapInspector gives you detailed information for the living objects:

  • Reference history (backtrace support) See who called retain, strong, release
  • Responder chain for recorded objects
  • Screenshots of the inspected UIView, UIViewController, UIImage
  • Detailed information about the object (Description, frame, properties, iVars, methods)

In Action

HeapInspector

Why

Since ARC has been introduced we don't need to manage the retain & release anymore. ARC is very powerful and makes Objective-C more stable. ARC decreased the number of crashes and improves the memory footprint.
ARC is technically doing a powerful job. It knows when to retain, autorelease and release.
But ARC doesn't think about the overall architecture how to design for low memory usage. You should be aware that you can still do a lot of things wrong with your memory (even with ARC). You can still get memory pressures or peaks with ARC.

  • You can still create Retain Cycles
  • The strong property lifetime qualifier can be misused (i.e. holding an object twice and longer than needed.)
  • Memory peaks through loops (if you're not using a proper @autoreleasepool)
  • Wrong caching with static

And that's why we introduced HeapInspector to find those issues.

Installation

CocoaPods

HeapInspector runs with Objective-C and Swift via CocoaPods Just add the HeapInspector to your Podfile.

pod "HeapInspector"

and run pod install afterwards.

Carthage

You can use Carthage. Specify in Cartfile:

github "tapwork/HeapInspector-for-iOS"

Manual

Download the repository into your project via git or just as zip. Drag it the HeapInspector folder into your Xcode project. See following image.

Disable ARC for NSObject+HeapInspector.m by adding -fno-objc-arc to Xcode's Build Phases -> Compile Source. See example images here: Drag and disable ARC

How to use it

Make sure to import the header file
Objective-C

@import HeapInspector;

Swift

import HeapInspector

Start

Just run the following to start HeapInspector in a separated debug window. The window can be moved on your screen in order to reach all your UI elements. The left circle button starts / stops the memory heap snapshot. See demo above.
Objective-C

[HINSPDebug start];

Swift

HINSPDebug.start()

We recommend to use a specific class prefixes, Swift modules or even a real classes like UIImageView. Or just run start to record all NSObject subclasses.
Objective-C

[HINSPDebug addClassPrefixesToRecord:@[@"RM", @"UITableView"];

Swift You can register modules for the heap snapshot and recordings.

HINSPDebug.addSwiftModulesToRecord(["MyModule", "AnotherFrameworkModule"])

Stop

Stopping and removing the inspector's window goes with
Objective-C

[HINSPDebug stop];

Swift

HINSPDebug.stop()

Just call the start/stop methods at app launch or via your custom button.

Backtrace record

HeapInspector can also record the backtrace for each object that received an alloc, retain, release or dealloc. Use this only with very specific recorded classes or in smaller apps. Start the backtrace with
Objective-C

[HINSPDebug recordBacktraces:YES]; 

Swift

HINSPDebug.recordBacktraces(true)

Example project

HeapInspector comes with an example project. There you will see a lot of mistakes made with the memory design.

  • strong delegate properties
  • NSTimer that is not being invalidated properly
  • Holding objects longer than needed. strong property for the UIViewController that is pushed onto the UINavigationController stack

References, Inspirations & Thanks

Author

License

MIT

Comments
  • Support module instead of class prefix

    Support module instead of class prefix

    Since swift isn't using prefix anymore, it's really hard our own objects. I can think two solutions to make it working with this kind of projects:

    • Filtering using the project module instead of using a simple prefix
    • Add a search field which can make easier to track our own objects
    opened by tbaranes 7
  • Crash Bug

    Crash Bug

    crash stack: image

    crash code: image

    the log output: 2016-03-16 22:55:04.864 Weiyun[3862:2294084] zone address:0x1042ac000 index:0 2016-03-16 22:55:05.123 Weiyun[3862:2294084] zone address:0x104518000 index:1 2016-03-16 22:55:05.123 Weiyun[3862:2294084] zone address:0x13784a000 index:2 2016-03-16 22:55:05.124 Weiyun[3862:2294084] zone address:0x1047f8000 index:3 2016-03-16 22:55:05.124 Weiyun[3862:2294084] zone address:0x19f939e70 index:4 2016-03-16 22:55:05.124 Weiyun[3862:2294084] zone address:0x137179800 index:5 2016-03-16 22:55:05.130 Weiyun[3862:2294084] zone address:0x1042ac000 index:0 2016-03-16 22:55:05.636 Weiyun[3862:2294084] zone address:0x104518000 index:1 2016-03-16 22:55:05.636 Weiyun[3862:2294084] zone address:0x13784a000 index:2 2016-03-16 22:55:05.637 Weiyun[3862:2294084] zone address:0x1047f8000 index:3 2016-03-16 22:55:05.637 Weiyun[3862:2294084] zone address:0x19f939e70 index:4 Weiyun(3862,0x1a05f9000) malloc: Attempted to register zone more than once: 0x16fd99c18 2016-03-16 22:55:05.637 Weiyun[3862:2294084] zone address:0x137179800 index:5 2016-03-16 22:55:05.637 Weiyun[3862:2294084] zone address:0x16fd99c18 index:6

    I don't know what happed, It's crash everytime I want stop record or tap the Heap Bar.

    Thanks

    opened by matthewyan 5
  • It's too slow

    It's too slow

    In my app, I record the memory usage, but it too slow. One operation may cost ten minutes.

    If run for a while, the app may killed because the memory:Message from debugger: Terminated due to Memory Error I Test this in iPhone 6 & iphone 5c. The project is large and many objects are alloced when running.

    somebody has the same problem? how should I resolve this. Thanks!

    opened by matthewyan 4
  • Non-GUI example

    Non-GUI example

    Can you show how this could be used without a GUI? e.g.. in app extensions that can't display a full UI? A simple way using NSLog() would be much appreciated.

    opened by rlaferla 3
  • Confuse on Retain Count

    Confuse on Retain Count

    I am confused by the algorithm of Retain Count. in HeapInspector,Retain Count = alloc+retain+strong -release, but is not always equals to CFGetRetainCount((__bridge CFTypeRef)object), why?

    opened by zzaiguo 2
  • Xcode6.1 start HeapInspectorExample/HeapInspectorExample.xcodeproj  build error

    Xcode6.1 start HeapInspectorExample/HeapInspectorExample.xcodeproj build error

    ld: library not found for -lPods-HeapInspector clang: error: linker command failed with exit code 1 (use -v to see invocation)

    @tapwork if use pods, will start .xcworkspace file, but project no this file

    opened by leiyong316 2
  • fix a crash when using BlocksKit (caused by NSProxy)

    fix a crash when using BlocksKit (caused by NSProxy)

    If obj is a instance of subclass of NSProxy, when calling class_getName([obj class]) will cause EXC_BAD_ACCESS crash. The case I met is when A2DynamicDelegate instance from BlocksKit is passed to static inline bool canRecordObject(Class cls); method, it crashes on the line const char *name = class_getName(cls);

    Try to fix it by simply not testing its class, and just ignore it.

    opened by allenhsu 2
  • HeapInspectorExample crash

    HeapInspectorExample crash

    1. Check out code with tag 0.0.6, and run the HeapInspectorExample.
    2. Tap Record button and Gallery button. It crash in NSObject+HeapInspector.m, Line 152, EXC_BAD_ACCESS.

    BTW, it seams this was introduced in commit: 45fb730. The param backtrace is released in getBacktrace().

    opened by 52doho 1
  • Correct the spelling of CocoaPods in README

    Correct the spelling of CocoaPods in README

    This pull requests corrects the spelling of CocoaPods 🤓 https://github.com/CocoaPods/shared_resources/tree/master/media

    Created with cocoapods-readme.

    opened by ReadmeCritic 0
  • bugfix/canRecordObject fix

    bugfix/canRecordObject fix

    HeapInspector-for-iOS is not compatible with ReactiveCocoa , because [obj class] in canRecordObject method will cause stack overflow when rac_signalForSelector is called in ReactiveCocoa.

    opened by weike9786 0
  • fix return value bug causing crash with blocks in retainBlock

    fix return value bug causing crash with blocks in retainBlock

    I found the crash I mentioned in https://github.com/tapwork/HeapInspector-for-iOS/issues/2 is related to this bug.

    It should return the address of copied object, via: http://clang.llvm.org/docs/AutomaticReferenceCounting.html

    And according to the doc, if the object is no longer on stack (actually I didn't quite understand the meaning of on stack, retainCount > 0?), it should be retained instead of copied.

    But with this fix, at least blocks won't crash with HeapInspector enabled.

    opened by allenhsu 0
  • memory leak

    memory leak

    when I install HeapInspector in my program,I find that your framework can product memory leak! Can you answer me the reason? I want to say my program has not memory leak. I just want to test your framwork,no except to see what it causes now.

    opened by DreamOfXM 2
  • Everything freezes

    Everything freezes

    When I run my app with HeapInspector, I start it in AppDelegate in didFinishLaunching, a couple of seconds in running it, the simulator freezes. Both the record and heap buttons, and everything in my app stops responding to inputs. Issue reproduces both on simulator and on device.

    Debug navigator shows like 10-20% CPU activity and about 10mbps of ram, which is regular app usage.

    opened by alexbartisro 15
Releases(1.0.1)
  • 1.0.1(Mar 8, 2017)

  • 1.0(Mar 8, 2017)

  • 0.3(Apr 18, 2016)

  • 0.2.1(Mar 24, 2016)

    • Record backtrace is turned on per default now (thanks to better performance)
    • Some minor clean ups
    • Some internal method renaming
    • Some performance improvements
    Source code(tar.gz)
    Source code(zip)
  • 0.2.0(Mar 9, 2016)

    • HeapShot and backtrace recording performance has been improved
    • Now with Swift modules support. You can register now for Swift modules
    + (void)addSwiftModulesToRecord:(NSArray <NSString *> *)swiftModules;
    
    • Register multiple class prefixes (ObjC)
    + (void)addClassPrefixesToRecord:(NSArray <NSString *> *)classPrefixes;
    
    • Some bug fixes and clean ups
    Source code(tar.gz)
    Source code(zip)
Owner
Christian Menschel
Christian Menschel
Find common xib and storyboard-related problems without running your app or writing unit tests.

IBAnalyzer Find common xib and storyboard-related problems without running your app or writing unit tests. Usage Pass a path to your project to ibanal

Arek Holko 955 Oct 15, 2022
In-app memory usage monitoring for iOS

What's Stats Stats displays load statuses such as the memory usage, the CPU load, and the number of subviews in-app, and in realtime. How to use Just

Shuichi Tsutsumi 170 Sep 18, 2022
iOS tool that helps with profiling iOS Memory usage.

FBMemoryProfiler An iOS library providing developer tools for browsing objects in memory over time, using FBAllocationTracker and FBRetainCycleDetecto

Facebook Archive 3.4k Dec 7, 2022
Commit fully-formatted Objective-C as a team without even trying.

[ Space Commander] [ Space Commander] provides tools which enable a team of iOS developers to commit Objective-C code to a git repository using a unif

Square 1.1k Nov 17, 2022
decoupling between modules in your iOS Project. iOS模块化过程中模块间解耦方案

DecouplingKit 中文readme Podfile platform :ios, '7.0' pod 'DecouplingKit', '~> 0.0.2' DecouplingKit, decoupling between modules in your iOS Project. D

coderyi 139 Aug 23, 2022
An Xcode formatter plug-in to format your swift code.

Swimat Swimat is an Xcode plug-in to format your Swift code. Preview Installation There are three way to install. Install via homebrew-cask # Homebrew

Jintin 1.6k Jan 7, 2023
Asserts on roids, test all your assumptions with ease.

KZAsserts - Asserts on roids, test all your assumptions with ease. There are many ways in which we can improve quality of our code-base, Assertions ar

Krzysztof Zabłocki 101 Jul 1, 2022
An xcconfig (Xcode configuration) file for easily turning on a boatload of warnings in your project or its targets.

Warnings This is an xcconfig file to make it easy for you to turn on a large suite of useful warnings in your Xcode project. These warnings catch bugs

Peter Hosey 438 Nov 8, 2022
Skredvarsel app - an iOS, iPadOS, and macOS application that provides daily avalanche warnings from the Norwegian Avalanche Warning Service API

Skredvarsel (Avalanche warning) app is an iOS, iPadOS, and macOS application that provides daily avalanche warnings from the Norwegian Avalanche Warning Service API

Jonas Follesø 8 Dec 15, 2022
Simple iOS app blackbox assessment tool. Powered by frida.re and vuejs.

Discontinued Project This project has been discontinued. Please use the new Grapefruit #74 frida@14 compatibility issues frida@14 introduces lots of b

Chaitin Tech 1.6k Dec 16, 2022
Manipulates the undocumented interchange format for the Apple Notes app.

NotesArchive A Swift package for reading and writing an undocumented interchange format for the Apple Notes app in macOS 12 Monterey1. Enabling the De

Zachary Waldowski 7 Jul 5, 2022
iOS project bootstrap aimed at high quality coding.

iOS Project bootstrap How do you setup your iOS projects? Since we are approaching 2015 I’m working on refreshing my project bootstrap. I’ve decided t

Krzysztof Zabłocki 2k Dec 23, 2022
iOS library to help detecting retain cycles in runtime.

FBRetainCycleDetector An iOS library that finds retain cycles using runtime analysis. About Retain cycles are one of the most common ways of creating

Facebook 4.1k Dec 26, 2022
Awesome bug reporting for iOS apps

Buglife is an awesome bug reporting SDK & web platform for iOS apps. Here's how it works: User takes a screenshot, or stops screen recording User anno

Buglife 498 Dec 17, 2022
Makes it easier to support older versions of iOS by fixing things and adding missing methods

PSTModernizer PSTModernizer carefully applies patches to UIKit and related Apple frameworks to fix known radars with the least impact. The current set

PSPDFKit Labs 217 Aug 9, 2022
Flexible bug report framework for iOS

Clue is a simple smart-bug report framework for iOS, which allows your users to record full bug/crash report and send it to you as a single .clue file

Ahmed Sulaiman 279 Nov 3, 2022
The project used in the iOS Architect Crash Course lectures

iOS Architect Crash Course • August 2nd-8th • EssentialDeveloper.com https://www.essentialdeveloper.com/ios-architect-crash-course/aug-2021-a5220 It's

Aleksei Korolev 1 Jul 20, 2022
A library that enables dynamically rebinding symbols in Mach-O binaries running on iOS.

fishhook fishhook is a very simple library that enables dynamically rebinding symbols in Mach-O binaries running on iOS in the simulator and on device

Meta 4.9k Jan 8, 2023
Find memory leaks in your iOS app at develop time.

中文介绍 | FAQ中文 MLeaksFinder MLeaksFinder helps you find memory leaks in your iOS apps at develop time. It can automatically find leaks in UIView and UIV

Tencent 5.3k Dec 22, 2022
👷‍♀️ Closure-based delegation without memory leaks

Delegated 2.0 Delegated is a super small package that helps you avoid retain cycles when using closure-based delegation. New Medium post here. Origina

Oleg Dreyman 703 Oct 8, 2022