简体   繁体   English

运行应用程序时内存消耗不断增加

[英]Memory consumption keeps increasing while running application

I have multiple views in my application with respective view controllers. 我的应用程序中有多个视图,并带有各自的视图控制器。 What I am doing is as follows. 我在做什么如下。

Here is the more illustrative code: 这是更说明性的代码:

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
  View1Controller *viewController1 = [[View1Controller alloc] initWithnibName:@"View1"];
  View2Controller *viewController2 = [[View2Controller alloc] initWithnibName:@"View2"];
  View3Controller *viewController3 = [[View3Controller alloc] initWithnibName:@"View3"];

  [window addSubview:viewController1.view];
  [window makeKeyAndVisible];
}

In View1Controller file: 在View1Controller文件中:

For Some Button Action 对于某些按钮动作

- (IBAction) goTOView2:(id)sender
{
  iPhoneApplicationAppDelegate *appDelegate = (iPhoneApplicationAppDelegate*) [[UIApplication sharedApplication] delegate];
  [appDelegate.window.superView removeFromSuperview];
  [appDelgate.window addSubview: appDelgate.viewController2.view];
}

Similarly for view3 对于view3同样

I am retaining all this three view controller in my application delegate. 我将所有这三个视图控制器保留在我的应用程序委托中。 When I want to switch to other view I have the following code. 当我想切换到其他视图时,我有以下代码。

Don't go on the syntax errors of the code. 不要继续执行代码的语法错误。

This three view controller has multiple view with their own navigation controller which for pushing and popping different views. 该三视图控制器具有多视图,并带有自己的导航控制器,用于推和弹出不同的视图。

My problem is when I run this application using instrument, I see as I switch from one view to another the memory consumption keeps increasing. 我的问题是,当我使用仪器运行该应用程序时,看到从一个视图切换到另一个视图时,内存消耗一直在增加。

Please help and thanks for that in advance. 请帮助,并提前感谢您。

Adding and removing views from your app's window is not the recommended way of switching between view controllers. 在应用程序的窗口中添加和删除视图不是在视图控制器之间进行切换的推荐方法。 You should be using a UITabBar and letting the UITabBarController handle your view controllers. 您应该使用UITabBar并让UITabBarController处理您的视图控制器。

The fact that your memory consumption keeps increasing means that you are allocating more objects as your application runs. 内存消耗持续增加的事实意味着您将在应用程序运行时分配更多对象。 It probably doesn't have anything to do with the way you're switching views. 它与切换视图的方式可能没有任何关系。 You're most likely allocating objects and forgetting to release them - causing memory leaks. 您最有可能分配对象而忘记释放它们-导致内存泄漏。 You should try using the Leaks tool in Instruments to determine which lines of code are causing your problems. 您应该尝试使用Instruments中的“泄漏”工具来确定引起问题的代码行。 (Or post more here!) (或在此处发布更多信息!)

The "leaks" tool usually does not help in cases where you see memory slowly grow, these often are not leaks but objects retained unexpectedly. 在您看到内存缓慢增长的情况下,“泄漏”工具通常无济于事,这些通常不是泄漏,而是对象意外保留。

In the Object Allocation tool, select the "created and still living" option. 在对象分配工具中,选择“创建并仍然存在”选项。

Then select a region in the graph where you see more memory being used, when you expect no new memory to be created. 然后,当您预计不会创建新的内存时,在图形中选择一个区域,在该区域中您将看到更多的内存正在使用。 Follow that information to see just what is creating objects you are not expecting.. 遵循该信息以查看正在创建的对象不是您所期望的。

Also, you say you are setting these view controllers as properties. 另外,您说您正在将这些视图控制器设置为属性。 So that means you set them exactly like this in applicationDidFinishLaunching: 因此,这意味着您可以在applicationDidFinishLaunching中完全按照以下方式设置它们:

self.viewController1 = viewController1;

Right? 对?

This line looks wrong: 这行看起来是错误的:

[appDelegate.window.superView removeFromSuperview];

What you want is something like: 您想要的是这样的:

[viewController1.view removeFromSuperview];

depending on which view you're moving away from. 根据您要移开的视图而定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM