我在我的应用程序中使用导航控制器。在我推送并弹出视图控制器 3 次后,我的应用程序由于内存不足而崩溃。这是我下面的代码。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
HomePageVC *homePage = [[ViewController alloc] homePage = [[ViewController alloc] initWithNibName:@"MainView-iPad" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:homePage];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
当用户按下按钮时,我将他发送到另一个视图控制器。
-(IBAction)showMap:(id)sender
{
MapViewController *mapViewController = Nil;
mapViewController = [[MapViewController alloc] initWithNibName:@"MapView-iPad" bundle:nil];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.navigationController pushViewController:mapViewController animated:YES];
}
当他想回到 rootView Controller 时,我会
-(IBAction)goBack:(id)sender
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.navigationController popToRootViewControllerAnimated:YES];
}
现在做了几次之后,didReceiveLowMemory 逐渐被调用并且应用程序崩溃了。
为了调试更多,我在循环中打印了内存使用情况。
-(void) report_memory {
struct task_basic_info info;
mach_msg_type_number_t sizeM = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&info,
&sizeM);
if( kerr == KERN_SUCCESS ) {
NSLog(@"Memory usage: %.4lf MB", info.resident_size/1000000.0);
} else {
NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
}
}
输出是
When the app Launches : Memory usage: 137.8263 MB
After showMap First Time : Memory usage: 206.2172 MB
Gone Back Home : Memory usage: 223.6580 MB ==> MapVC didn't release
After showMap Second Time: Memory usage: 227.2172 MB
Press Go Back Home : Memory usage: 250.2172 MB ==> MapVC didn't release
After showMap Third Time : App Crashes
我的 lowMemory 写成
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
NSLog(@"Got Low Memory From %@",[self class]);
}
为了增加更多惊喜,我从 HomePageVC 和 MapVC 都收到了内存不足警告。如果我已经将 MapVC 大便了,我是如何从它那里收到 lowMemory 的?为什么 MapVC 消耗的内存没有被释放?我正在使用 ARC。