#import <Foundation/Foundation.h>
#import <stdio.h>
void memoryTestWithAutoreleasePool();
void memoryTestWithoutAutoreleasePool();
void memoryTestWithRelease();
void memoryTestWithoutRelease();
int main(int argc, char* argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Please input a number in [1, 2, 3, 4] to select method of memorytesting.");
NSLog(@"1: memoryTestWithAutoreleasePool: No memory leak");
NSLog(@"2: memoryTestWithoutAutoreleasePool: Memory leak");
NSLog(@"3: memoryTestWithRelease: No memory leak");
NSLog(@"4: memoryTestWithoutRelease: Memory leak");
int n;
scanf("%d", &n);
void (*func)();
switch(n) {
case 1: func = memoryTestWithAutoreleasePool;
break;
case 2: func = memoryTestWithoutAutoreleasePool;
break;
case 3: func = memoryTestWithRelease;
break;
case 4: func = memoryTestWithoutRelease;
break;
default: NSLog(@"Unvalid number.");
return 0;
}
fflush(stdin); // No definition by GCC.
getchar();
func();
getchar();
func();
getchar();
func();
getchar();
func();
[pool drain];
NSLog(@"--------Program is finished--------");
return 0;
}
void memoryTestWithAutoreleasePool() {
NSLog(@"...........Memory Test With AutoreleasePool Start...........");
// 把pool放在方法中, 而不是由主函数的pool来管理.
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSMutableArray* array = [[NSMutableArray alloc] init];
int length = 1000000;
int i = 0;
// Allocate memory.
for (i = 0; i < length; ++i) {
NSNumber* number = [NSNumber numberWithInt:i];
[array addObject:number];
}
// Release memory.
[array release];
[pool drain];
NSLog(@"...........Memory Test With AutoreleasePool End...........");
}
void memoryTestWithoutAutoreleasePool() {
NSLog(@"...........Memory Test Without AutoreleasePool Start...........");
NSMutableArray* array = [[NSMutableArray alloc] init];
int length = 1000000;
int i = 0;
// Allocate memory.
for (i = 0; i < length; ++i) {
NSNumber* number = [NSNumber numberWithInt:i];
[array addObject:number];
}
// Release memory.
[array release];
NSLog(@"...........Memory Test Without AutoreleasePool End...........");
}
void memoryTestWithRelease() {
NSLog(@"...........Memory Test With Release Start...........");
NSMutableArray* array = [[NSMutableArray alloc] init];
int length = 1000000;
int i = 0;
// Allocate memory.
for (i = 0; i < length; ++i) {
NSNumber* number = [[NSNumber alloc] initWithInt:i];
[array addObject:number];
}
// Release memory.
for (i = 0; i < length; ++i) {
NSNumber* number = [array objectAtIndex:i];
[number release];
}
[array release];
NSLog(@"...........Memory Test With Release End...........");
}
void memoryTestWithoutRelease() {
NSLog(@"...........Memory Test Without Release Start...........");
NSMutableArray* array = [[NSMutableArray alloc] init];
int length = 1000000;
int i = 0;
// Allocate memory.
for (i = 0; i < length; ++i) {
NSNumber* number = [[NSNumber alloc] initWithInt:i];
[array addObject:number];
}
// Release memory.
[array release];
NSLog(@"...........Memory Test Without Release End...........");
}