Giter Site home page Giter Site logo

tapwork / heapinspector-for-ios Goto Github PK

View Code? Open in Web Editor NEW
1.9K 61.0 140.0 33.81 MB

Find memory issues & leaks in your iOS app without instruments

License: MIT License

Objective-C 72.20% Ruby 2.90% Shell 24.03% Swift 0.87%
memory-heap heapinspector heap-snapshot arc backtrace ios retain-cycles leaks instruments memory-allocation

heapinspector-for-ios's Introduction

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

heapinspector-for-ios's People

Contributors

allenhsu avatar haifengkao avatar jeffreyjackson avatar onmyway133 avatar readmecritic avatar tapwork avatar yanjunz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

heapinspector-for-ios's Issues

crash

[UINavigationBar frame]: unrecognized selector sent to class 0x1b03fcd60

Pod update?

The pod doesn't have the latest code. When will it be updated?

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

Crashes with Masonry

Just crashed when using Masonry with blocks.

Crashed line looks like:

[self.currentFrame mas_updateConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.currentFrame.superview).with.offset(3.0 + pageSize * 2);
}];

I'm not sure if the implementation of objc_retainBlock is all right in HeapInspector.

id objc_retainBlock(id value)
{
    if (value) {
        recordAndRegisterIfPossible(value,"retainBlock");
    }
    [value copy];

    return value;
}

According to http://clang.llvm.org/docs/AutomaticReferenceCounting.html objc_retainBlock should do more than just copy?

If value is null, this call has no effect. Otherwise, if the block pointed to by value is still on the stack, it is copied to the heap and the address of the copy is returned. Otherwise a retain operation is performed on the block exactly as if it had been sent the retain message.

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?

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().

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!

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.

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.

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.

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.