在活动周期的Andr​​oid内存管理 [英] android memory management in activity lifecycle

查看:107
本文介绍了在活动周期的Andr​​oid内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是有点复杂的。

My question is a little bit complicated.

我想明白是怎么对待应用程序资源时,活动启动(为背景,按钮等特别影像),然后暂停。

I want to understand how the app treats resources (especially images for backgrounds, buttons, etc.) when Activity is started, then suspended.

例如,从我做起活动A,它在屏幕上显示的所有图像,吃它的内存,然后又b活动已启动,A被暂停。碰巧的所有图像,资源等什么?当他们被释放?我怎么可以把对它们的控制?如果我不能保持活性的内存中,并从活动堆栈中删除呢?

For example, I start Activity A, it shows all the images on the screen, eats its memory, then another Activity B is started and A is suspended. What happens to all the images, resources, etc.? When they are freed? How can I take control over them? Should I not keep the Activity A in the memory and remove it from the Activity stack?

如果您需要任何澄清我的问题,请写信给我!

If you need any clarification for my questions, pls write me!

在此先感谢! Danail

Thanks in advance! Danail

推荐答案

,直到它完成活动不免费资源。但在大多数情况下,它必须不会有问题为您服务。在我看来,你不应该添加自己的资源管理,使您的code复杂,在多数情况下。

Activity doesn't free resources until it's finished. But in most cases it mustn't be a problem for you. In my opinion, you shouldn't add your own resource management and make your code complicated in most cases.

但如果你真的认为你的应用程序能进能出的内存,你应该像 MAT 检查。内存问题可能是由内存泄漏引起的,而不是沉重的内存使用情况。

But if you really think that your app can be out of memory, you should check it with something like MAT. Problems with memory may be caused by memory leaks, not heavy memory usage.

最后,当你绝对相信,你必须做些什么来减少内存使用,你可以做一些内存优化。例如,您可以节省内存消耗的对象(如大型图片)到本地存储在的onStop()并加载它们在 ONSTART()。我认为使用的onPause() / onResume()用于此目的是一个坏主意,因为活动是部分甚至完全可见。

Eventually, when you're absolutely sure that you have to do something to reduce memory usage, you can do some memory optimization. For example, you can save memory-consuming objects (e.g. large images) to local storage in onStop() and load them in onStart(). I think using onPause()/onResume() for this purpose is a bad idea, because Activity is partially or even fully visible.

在理论上,你甚至可以摧毁你的的onStop所有部件() ONSTART恢复它们() ,但它可以使您的应用程序太慢。当然,在这种情况下,状态保存,必须由你来实现。

In theory, you can even destroy all your widgets in onStop() and restore them in onStart(), but it can make your app too slow. And, of course, in this case state saving must be implemented by you.

整理活动,似乎是个好主意,但我认为事实并非如此。首先,它使您的工作更慢。其次,你必须管理自己活动活动堆栈和状态。 例如,活动A开始活动B.因此,活动B必须知道该怎么办时,用户presses后退按钮。当用户presses后退按钮,你应该开始活动A和恢复其状态。但是,如果用户终止这个程序。在这种情况下,你必须初始化活动为它的默认状态。所以,你必须实现很多额外的逻辑。

Finishing activities may seem to be a good idea, but I think it's not. Firstly it makes your work slower. Secondly you must manage activity stack and state of activities yourself. For example, Activity A starts Activity B. So, Activity B must know what to do when user presses Back button. When user presses back button you should start Activity A and restore its state. But what if user terminates this app. In this case you must initialize Activity A with its default state. So, you have to implement a lot of additional logic.

在最后,我要重申主要的想法更多的时间:不优化内存使用情况,如果你不是很确定,你必须

In conclusion I'll repeat the main idea one more time: don't optimize memory usage if you aren't absolutely sure you have to!

这篇关于在活动周期的Andr​​oid内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆