Brandon
Brandon

Reputation: 2171

Xcode Allocations Instrument - Listening to Memory Warnings

I am using the Allocations Instrument to enhance the performance of my app. I want to pay attention to memoryWarnings to make sure my app doesn't use up too much memory or crash.

I would like my entire app to listen to memoryWarings. I know I can use this to listen to certain warnings, but will the code below listen to everything? Also, where do I need to implement it? Do I need to put it in each View Controller or can I put it in the App Delegate?

- (id)init {
if ((self = [super init])) 
    {
    _cache = [NSMutableDictionary new];
    [[NSNotificationCenter defaultCenter] addObserver:self 
    selector:@selector(memoryWarning:) 
    name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
    }
return self;
}

I know I need to implement the method that listens to the memoryWarnings. Will this listen to all memoryWarnings? Also, do I need to place this in every viewController? Or can I place this somehow in the AppDelegate?

- (void)memoryWarning:(NSNotification*)note {
    [_cache removeAllObjects];
}

Any guidance would be great! Thank you!

Upvotes: 0

Views: 359

Answers (1)

Anil Varghese
Anil Varghese

Reputation: 42977

Your view controller already have a method to listen to memory warnings

 - (void)didReceiveMemoryWarning
 {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

Upvotes: 1

Related Questions