堆内存不断增长

发布于 2025-01-05 00:12:26 字数 1004 浏览 2 评论 0原文

今天我做了一些测试,我很好奇结果。我制作了一个应用程序(ARC),其中有 UINavigationController 和两个 UIViewController。在第一个视图中有一个按钮,按下该按钮时会加载第二个视图。在第二个视图中,当检测到摇动手势时,将加载第一个视图,依此类推。我在仪器中注意到的是,每次加载视图时堆都会增长。这是一些代码

AppDelegate.m

self.navigationController = [[UINavigationController alloc]init];
self.window setRootViewController:self.navigationController];
FirstViewController *firstview = [FirstViewController alloc]init];
[self.navigationController pushViewController:FirstViewController animated:YES]; 

FirstViewController.m

-(IBAction)loadSecondView
{
  SecondViewController *secondview = [SecondViewController alloc]init];
  [self.navigationController pushViewController:secondview animated:YES];
}

SecondViewController.m

-(IBAction)loadFirstView
{
  FirstViewController *firstview = [FirstViewController alloc]init];
  [self.navigationController pushViewController:first view animated:YES];
}

我不明白为什么会发生这种情况。在这种情况下如何避免堆增长?

Today I made some tests and I am curious of the results. I made an app (ARC) which have UINavigationController and two UIViewControllers. In the first view there is a button and when that button is pressed the second view is loaded. In the second view when shake gesture is detected the first view is loaded and so on. What I notice in instruments is that the heap grows every time when a view is loaded. Here is some code

AppDelegate.m

self.navigationController = [[UINavigationController alloc]init];
self.window setRootViewController:self.navigationController];
FirstViewController *firstview = [FirstViewController alloc]init];
[self.navigationController pushViewController:FirstViewController animated:YES]; 

FirstViewController.m

-(IBAction)loadSecondView
{
  SecondViewController *secondview = [SecondViewController alloc]init];
  [self.navigationController pushViewController:secondview animated:YES];
}

SecondViewController.m

-(IBAction)loadFirstView
{
  FirstViewController *firstview = [FirstViewController alloc]init];
  [self.navigationController pushViewController:first view animated:YES];
}

I can't figure out why that happens. How to avoid heap of growing in that case ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诠释孤独 2025-01-12 00:12:26

实际上,每次创建新的视图控制器对象时......都不应该这样做。

因此,每次分配一个新对象并推送该视图时,它将被添加到导航堆栈中,因此内存会增加。

相反,当您在第一个视图中并点击按钮时,您可以弹出当前视图控制器并通知 AppDelegate 类显示第二个视图。

类似地,在第二个视图中,当您想要显示第一个视图时,弹出当前视图并通知 AppDelegate 类推送第一个视图控制器。

Actually every time you are creating a new view controller object.. That should not be done.

So every time you allocate a new object and pushed that view, it will be added to the navigation stack and so, the memory grows.

Instead, when you are in first view and tapped the button, you can pop the current view controller and inform the AppDelegate class to show the second view.

Similarly while in second view, when you want to show the first view, pop the current view and inform the AppDelegate class to push the first view controller.

z祗昰~ 2025-01-12 00:12:26
SecondViewController *secondview = [[[SecondViewController alloc]init] autorelease];

FirstViewController *firstview = [[[FirstViewController alloc]init] autorelease];

你应该自动释放viewcontrollers(对于非ARC)

如果第二个控制器首先打开,你应该做popViewController。如果你不返回,堆就会增长

SecondViewController *secondview = [[[SecondViewController alloc]init] autorelease];

FirstViewController *firstview = [[[FirstViewController alloc]init] autorelease];

you should autorelease viewcontrollers (for not ARC)

if second controller opens first, you should do popViewController. If you wont return back, heap will grow

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文