2
votes
NSMutableArray *fontsDetails=[[NSMutableArray alloc] init];
[fontsDetails addObject:[UIFont systemFontOfSize:28]];

NSArray *fontFamilies = [UIFont familyNames];

for (int i = 0; i < [fontFamilies count]; i++)
{
    NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
    @autoreleasepool {
        for (NSString *fontName  in fontNames) {
            [fontsDetails addObject:[UIFont fontWithName:fontName size:28]];
        }
    }


}

I am using this code in viewDidLoad of a controller, with this code the heap shot difference between the first and second run increases by 5mb and never comes down. (subsequent heap shots differences are lower). I am finding [UIFont fontWithName:] in the backtrace of heap shot, i ran the leak profiler and there are no leaks. All the fonts loaded are kept at heap and never destroyed. Please help me to solve this.

1
This may be just an optimization, you never know how these classes are implemented, they may handle a sort of cache to reuse fonts.Ramy Al Zuhouri
will this memory be freed by the system in case of low memory warnings?pradeepa
I really don't know that, it's hard to say, I don't know how the framework is implemented. Probably an Apple engineer will know it.Ramy Al Zuhouri

1 Answers

3
votes

More likely than not, it is a cache in the system that is hanging on to the UIFont with the expectation that it will be used again.

Given that it grows once, and never again, it isn't a leak.

If you use the reference event tracker in the Allocations instrument, you can see what is retaining the font. I'd bet that you'll find an extra retain or two somewhere in the UIFont machinery.