Can I "profile" using Instruments for memory leaks using a Test Case? (i.e. focused run on a defined area of code) - iphone

Can I "profile" for memory leaks using a Test Case? (i.e. focused run on a defined area of code) I tried changing my profile for my iPhone app to Unit Tests and then noted the "profile" option disappeared.
My goal (requirement) I guess is to be able to isolate a specific method, and then run the Instruments memory leak profiling across this specific method and observe results.

The easiest way to accomplish this is to launch your application in Instrument, wait a little and tap a button that will run the specific method you're looking for.
You can then select the timespan where the method ran by dragging the time bar with the option key.

You can also set a breakpoint in your tests, use Instruments to attach to the process, and then resume. Don't forget to hit Record like I did :)

Related

IPhone app Memory Issue

I check my Iphone App in Instruments and it's using 392.00kb of memory which does not decrease and keeps on increasing.
So although I am not using a UITableView there is an entry for [UISectionRowData refreshWithSection:tableView:tableViewRowData:].
Try using HeapShot, see this tutorial by bbum.
Basically in the Allocations instrument click Heapshot, run an iteration of your app, repeat 3 or four times and look for items that are persisting.
Another quick check is Analyze. Just run Analyze and it may find places where you are not handling memory correctly.

cocos2d code slow down after some time

all
I am making a game in cocos2d, and I am moving an object from one place to another , throught CCTouchBegan , CCTouchMoved, CCTouchEneded (ccp function) and after that I take the action on it.
Any thoughts on why this code runs slow on device but fast on simulator in iphone.
Show us the code then we can say something particular.
But I think you just forgot to stop the action. [object stopAction];
or can use this method [self removeChild:(CCSprite*)sender cleanup:YES] It will also cleanup all running actions depending on the cleanup parameter
Code often runs slower on the device than it does in the simulator. The simulator is not accurate with respect to performance. In order to gauge how fast something executes, you have to try it on a device.
check your memory allocations.
am also having same problem. bcoz of memory management . now i solved.
check your memory leakage using performance tool in your xcode.

How do I see every method called when I run my application in the iPhone Simulator?

I would really like to see every method, delegate, notification, etc. which is called / sent while I run my app in the iPhone Simulator. I thought the right place for this would be in the debugger, but I can't find the right setting.
My target is to see all that is happening in the background while I, for example, add a row to a UITableView or push the 'back'-button from my UINavigationController.
This would be a big help to figure out what delegate to use when something in the app is happening or when the user is pushing a button, changing a view, etc.
Is it possible to get this information?
You can log out everything that is happening when your application is running using DTrace, a framework that lets you probe the inner workings of anything running on a modern Mac. We don't yet have DTrace on iOS, but it will work while you're running within the Simulator.
I describe the fundamentals of DTrace in this article for MacResearch, then provide an example of a custom instrument you can build in Instruments using DTrace near the end of this article. That instrument logs out all methods called on all objects (even internal system ones) from the moment your application starts until it hits the end of -applicationDidFinishLaunching:.
To simplify this, you can simply create a custom instrument using the Instrument | Build New Instrument menu item in instruments. Set up one of the probe descriptors to look like the following:
only ignore the isInApplicationStart and timestamp logging options. A simple probe responding to any Objective-C method in any class will log all of those messages to the Instruments console, which sounds like what you want for your debugging.
Assuming you're sure you want absolutely everything...
Breakpoint objc_msgSend and objc_msgSend_stret. Almost all method calls use those two functions (mumble mumble IMP-cacheing).
Okay, now your app drops into the debugger all the time. So click the auto-continue box.
But now you don't see very much happening, so edit the breakpoints and add the command "bt" to get a backtrace.
Drown in debug spam.
This won't catch other C functions, of course.
If you just want to catch notifications, you can do something like this (much less spammy):
+(void)load
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleEveryNotification:) name:nil object:nil];
}
+(void)handleEveryNotification:(NSNotification*)notification
{
CFShow(notification);
}
Of course, notifications are done with normal method calls, so the first method will also show them (albeit in a big pile of spam).
Delegates are not called or sent; they are just normal Obj-C method calls (strictly "message-sends", but that doesn't have the same ring to it).
If you put a breakpoint into your app, you can watch the call stack change while you step through the code. That's probably as close as you're going to come to what you have in mind.

Memory Warning Level Indicator - iPhone SDK

I am using a ton of timers that update images in my game, and whenever I run it, the debugger somehow gives me a memory warning indicator level. I try adding some timers through a background thread, but that made a very small difference. Is there anyway I can reduce the memory usage of my app without having to get rid of my timers?
Thanks,
Kevin
In XCode, select from the menus: Run -> Run with Performance Tool -> Leaks. This will open an app called Instruments that will help you find the problem. See the Instruments documentation for more details.

Slow startup on iPhone

I'm debugging slow startup of an iPhone application (Xcode, Objective C++). It's a tabbar-based app with three tabs. All three tabs are loaded in a single NIB - about 20 objects total.
The first round of significant initialization takes place in the viewDidLoad handler of the first tab's view controller. However, it takes about 1 second between main() and that method's start time - about 2/3 of the total loading time. Question - what is going on during that time, and how do I investigate that (short of stepping through the disassembly)? To the best of my knowledge, there's no my code in between the two moments - the delay happens entirely in the system code.
Maybe some kind of Instrument that can give me per-function time profile?
The bundle is ~4 MB total, but I'm loading the biggest file (~3.5 MB) later than that, in the applicationDidFinishLaunching handler. Removing that file from the bundle and commenting out the relevant code does nothing for that 1-second delay.
UPDATE: there was debug interference after all. If I run it on the device while watching the console, the startup time us considerably shorter, and the proportion of delay - system code to my code - is skewed, too. But still, there's a noticeable delay between main and viewDidLoad, and it's about 50% of total loading time.
By the way, of all ways of loading a largish file from the bundle completely into memory, the fastest one was direct memory-mapping (using POSIX mmap()).
If you really are curious about what's executing during startup, and the relative times each method takes to run, you can create a custom DTrace script to track this. I describe how to do that towards the end of this article. This script will show you every method executed by your application in order from startup to the end of -applicationDidFinishLaunching:, along with the time spent in that method. You can run this script as a custom instrument in Instruments or as a standalone script (the standalone script tends to be more accurate on a system under load).
The major caveat of this approach is that it will only run in the Simulator, given the current lack of support for DTrace in the iPhone OS itself. However, you should be able to extract the order in which things are executed in your application's startup, as well as relative times that your application spends in each method. This will even show behind-the-scenes private API calls being made as your application starts, which might provide some additional clues as to what's going on.
For additional startup tuning suggestions, I'd recommend reading James Thomson's article "How To Make Your iPhone App Launch Faster".
There are two things that could be going on here. If you're debugging your app from within XCode, there's a good chance that the application is waiting at startup to attach to the GDB debugger. In my experience, that takes about a second. Try running your app without saying "Build and Go" in XCode and see if the results are any different. (Just click it from the home screen instead)
Your NIB file might also be the issue. 20 objects isn't too many, but you might consider breaking each tab into a separate NIB file if all else fails. The contents of NIB files referenced from your primary NIB file are lazily loaded, so the app will not load views for the two tabs that are invisible until they are selected. That might give you a performance boost at startup, though I don't think it could account for a full second.
Apple's got some great performance analysis tools in the iPhone SDK, but they're a bit hard to find. In the Run menu, select "Run with Performance Tool" -> "CPU Sampler." That will launch a separate application called Instruments which allows you to do all sorts of great runtime analysis. When you have the CPU instrument selected, the bottom half of the Instruments window provides a breakdown of what was taking CPU time in your app. You can double click functions to dive into them, and get a line by line breakdown of % of cycles used. It should give you more information about what specifically is causing your problem.
I'd recommend splitting up your app into three NIBS; the tab bar and the tab bar controller displayed on launch in the first, then lazily load the other two the first time the user switches to them.
I believe you can use the File >> Decompose Interface function in Interface Builder to accomplish this.
If you found your xib files are too large, I advice you build your UI with pure code.
large xib files will surely slow your startup time, and also slow your app when you first use an object in your xib.
I don't use xib in my projects, cause when somebody changed the xib in svn, you can hardly find what is changed. That's to say, xib is not going well with SVN.