RockandRoll
RockandRoll

Reputation: 411

How to check heap memory using instruments in Xcode?

I am trying to analyse the leaks using instruments in Xcode. Here is a sample code I am trying with.

+(NSString *) getUniqueFileName
{
NSDate *time = [NSDate date];
NSDateFormatter* df = [NSDateFormatter new];
[df setDateFormat:@"MMddyyyyhhmmssSSS"];
NSString *timeString = [df stringFromDate:time];
NSString *fileName = [NSString stringWithFormat:@"%@", timeString ];
//[time release];
// [df release];
// [timeString release];
return  fileName ;
}

I am using this method every time to generate a filename whenever I capture a photo. All variable are local to his method. If I try to release time, df, timestring, after getting filename, I am getting message sent to deallocated instance. I was sure that if I don't release these variables, memory will be leaking. I tried with instruments and got the following result, and here it is.
instruments leaks]![screensshot from instruments showing memory leaks

Now my question is what is meaning of red color highlighted row with 79.6%? And 20.4% with yellow color row highlighted? Red in general would be dangerous. but what % shows? How it is getting calculated? and when I check call tree, I will get a column named "Leaks" for perticular methods and will contains 100, 234, 560, 2345, some number. What does this number speak about. Help me out. Will be thankful if anyone can provide me good links for understand about instruments.

Upvotes: 4

Views: 2008

Answers (2)

Swift Dev Journal
Swift Dev Journal

Reputation: 20098

To explain the percentages in your screenshot, 79.6% of the leaked memory allocated in getUniqueFileName: was allocated in the red line of code. 20.4% of the leaked memory allocated in getUniqueFileName: was allocated in the yellow line of code. Red indicates a high percentage. It may or may not be dangerous.

To explain the Leaks column, it tells you the number of leaks in the method and any functions that method calls. It does not necessarily mean there are that many leaks in the method itself. For example, Cocoa and Cocoa Touch apps start with a main() function. If the main() function has the value 100 in the Leaks column, it means the app has 100 memory leaks, not that main() has 100 leaks.

Upvotes: 1

Kepler
Kepler

Reputation: 705

If you use ARC, disable it to your class, when leaks are creating - now you can release your variables: enter image description here Or you can try create your date formatter like this:

NSDateFormatter *df = [[NSDateFormatter alloc] init];

Upvotes: 0

Related Questions