Debug iPhone memory

I am using tools to try and determine if there are places in my application that might be more memory efficient. I took the time to mess around with the tools a bit, but I'm generally a newbie with hunting memory management issues coming from a Java background. I seem to be using about 1.82mb across calls to this method:

+ (NSString *)stringFromDateWithFormat:(NSDate *)date withFormat:(NSString *)format
{
    NSDateFormatter *dateFormatter;
    NSString *result;

    if (nil == date || nil == format)
        return nil;

    result = nil;
    if (nil != (dateFormatter = [[NSDateFormatter allocWithZone:[self zone]] init])) {
        [dateFormatter setDateFormat:format];   

        if (nil != (result = [dateFormatter stringFromDate:date])) {
            [dateFormatter release];
            return result;
        } 

        [dateFormatter release]; 
    } 
    return nil;
}

      

Since I am releasing the date format I am wondering if the NSString result is my problem. It seems to me that the call to the stringFromDate library will return an auto-implementable object, so there is nothing I can do to manually manipulate it. Slightly unsure how to optimize this method.

+1


source to share


3 answers


Is this method called many times in a loop? Autorelease objects are released only when the NSAutoreleasePool is released. As I understand it, a default autostart pool is created and released every event loop. You may be creating too many auto-implemented objects in a single event loop. The solution is to create a custom NSAutoreleasePool in the appropriate location and release it to clean up the autoreleasePool. An extreme example to illustrate the point:

int i;
NSAutoreasePool* pool = nil;
for (i = 0; i < 1000000; ++i) {
    /* Create a new pool every 10000 iterations */
    if ((i % 10000) == 0) {
        if (pool) [pool release];
        pool = [[NSAutoreleasePool alloc] init];
    }
    [someObj someMethodThatCreatesAutoreleasedObjects];
}
[pool release];

      



In this example, the current pool is released every 10,000 iterations and a new one is created. You can read more about memory management in the Programming Guide for Memory Management in Autodetect Pools.

+4


source


You need to return an auto-implemented object anyway, so you really have nothing to do with the resulting string. I don't see any memory related errors, but your code is definitely more verbose than necessary. Keep in mind that in Objective-C, if you call a method on nil, you return zero (or 0 for an integer, but not for floating point values). You can take out all those if statements and two return paths and your code will work the same. Also, I would just use alloc instead of allocWithZone.



0


source


I'm not 100% with this. I am also just learning Mac / Iphone development. But you can use an auto-release pool to help with memory management. It is used to troubleshoot release issues.

Here is an article with lots on memory management . Check the left one-way menu.

0


source







All Articles