Memory
|
Modified: |
Downloads
Overview
Xcode provides tools for monitoring memory use and detecting some memory leaks. The following provides some guidance to using Xcode.
Leaky1
#import "LeakTestViewController.h"
@interface Leaky: NSObject {}
-(void) print;
@end
@implementation Leaky
-(void) print {
NSLog(@"Leaky print\n");
}
- (void)dealloc {
[super dealloc];
NSLog(@"Leaky dealloc\n");
}
@end
@implementation LeakTestViewController
-(IBAction) click: (id) sender {
[l print];
} @end |
Build | Build and Analyze
You should be rewarded with a potential leak.

Run | Run with Performance Tool
| Leaks
Set Sampling Options to 1.0
Open the Simulator and click the button a few times.
You should see something similar to below.

Question - 16 bytes per allocation - why?
Scroll down to locate Leaky allocations - 4 allocations are currently living.

Fixing leaks
The leak pointed out by Build and Analyze can be plugged by releasing the allocated object as below.
LeakTestViewController.m #import "LeakTestViewController.h" @interface Leaky: NSObject {} -(void) print; @end @implementation Leaky -(void) print { NSLog(@"Leaky print\n"); } - (void)dealloc { [super dealloc]; NSLog(@"Leaky dealloc\n"); } @end @implementation LeakTestViewController -(IBAction) click: (id) sender {Leaky *l = [Leaky alloc]; [l print];
[l release]; } @end
Build | Build and Analyze
There should be no leaks detected.
Run | Run with Performance Tool | Leaks
In the Simulator, click the button a few times. No leaks should be
detected and Leaky allocations should be 0.
Deallocation
When an object reference count is 0 the object's dealloc method is called.
LeakTestViewController.m #import "LeakTestViewController.h" @interface Leaky: NSObject {} -(void) print; @end @implementation Leaky -(void) print { NSLog(@"Leaky print\n"); }
- (void)dealloc {
[super dealloc];
NSLog(@"Leaky dealloc\n");
}@end @implementation LeakTestViewController -(IBAction) click: (id) sender {Leaky *l = [Leaky alloc]; [l print]; [l release]; } @endAfter clicking the Click button a few times, to see the NSLog output:
Run | Console
Over-deallocating
Releasing an object that no longer exist crashes the application.
- Add another [ l release ]
- Build and Run
- Click
- Run | Console
Question - Can you explain what happened?
LeakTestViewController.m #import "LeakTestViewController.h" @interface Leaky: NSObject {} -(void) print; @end @implementation Leaky -(void) print { NSLog(@"Leaky print\n"); }- (void)dealloc {
[super dealloc];
NSLog(@"Leaky dealloc\n");
}@end @implementation LeakTestViewController -(IBAction) click: (id) sender {Leaky *l = [Leaky alloc]; [l print]; [l release];
[l release]; } @end
Leaky2

To more closely observe alloc and release operations.
#import "LeakTestViewController.h"
@interface Leaky: NSObject {}
-(void) print;
@end
@implementation Leaky
-(void) print {
NSLog(@"Leaky print\n");
}
- (void)dealloc {
[super dealloc];
NSLog(@"Leaky dealloc\n");
}
@end
@implementation LeakTestViewController
Leaky * l;
-(IBAction) allocLeaky: (id) sender {
|
Build | Build and Analyze
With alloc and release in separate methods, it fails to
detect a potential leak.
Run | Run with Performance Tool
| Leaks
Set Sampling Options to 1.0
Open the Simulator and click allocLeaky button once.
Question - Why no leaks?
Click allocLeaky button once. Question - What happens and why?
Question - You should see something similar to below after how many clicks?

Over-deallocating
Build and Run
Run | Console
allocLeaky 4 times.
releaseLeaky 2 times.
Question - Explain what happened and why.
#import "LeakTestViewController.h"
@interface Leaky: NSObject {}
-(void) print;
@end
@implementation Leaky
-(void) print {
NSLog(@"Leaky print\n");
}
- (void)dealloc {
[super dealloc];
NSLog(@"Leaky dealloc\n");
}
@end
@implementation LeakTestViewController
Leaky * l;
-(IBAction) allocLeaky: (id) sender {
|