Android 6:每次启动应用程序时,应用程序使用更多的内存

11

三星Galaxy S6使用Android 6.0.0和6.0.1。

我的应用程序运行良好,代码分析未显示任何内存泄漏。SmartManager显示该应用程序使用约40MB的内存。我杀掉了该应用程序(使用“当前运行的应用程序”概述---不确定这在英语中怎么称呼,抱歉。手机左下角的键,显示所有正在运行应用程序的迷你图像列表...),然后再次启动它。

现在SmartManager不再为我的应用程序显示约40MB的内存,而是更多。50.100.400。它会变化,但每次都会增长。一段时间后,我的应用程序实际上会崩溃,并出现OutOfMemoryException。有时甚至出现“尝试引发OutOfMemoryException时内存不足”的情况...

我必须卸载并重新安装应用程序以重置其内存消耗。相同的APK在我测试过的所有其他手机上都表现正常(例如带有Android 5.0的S5)。不知道是什么原因导致的。也不知道可能会导致这种情况!任何帮助/提示/建议...?

编辑:在Android 6.0.1上与S5上出现相同的行为,但在Android 5.0上没有

编辑2:感谢所有做出贡献的人。我仍然没有得到一个让我满意的答案,但提到了一些我将要探究的想法。大多数人都关注MemLeak问题,虽然这听起来合理,但我要强调的是,在Android 4或5上不会发生泄漏,只有在6上会发生。(我还没有测试过7)此外,泄漏仅在我强制杀死应用程序时发生(我有时需要这样做来测试适当的关闭和重新启动行为),而且仅在没有附加调试器的情况下(这使得测试/调试此行为很痛苦)。

该应用程序作为绑定服务启动一个服务,绑定到应用程序上下文---根据文档,当应用程序停止时,它应该自动清理自己。

很遗憾,这个赏金的时间快用完了。到目前为止,上述提到的服务似乎是我问题最有可能的来源。因此,我将授予Qamar这个赏金,因为他是第一个在评论和回答中提到这一点的人。对于其余的人,非常感谢并给你们点赞。

@savepopulation:不,目前我只有运行Android 5及更早版本的非三星设备。我将会获取更多设备,但这需要几周时间。 - Robin
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - timr
1
请问您能否发布堆栈跟踪信息?此外,您是否将所有可绘制对象都保存在同一个文件夹中? - Jagrut
2
我关闭了该应用程序(使用“当前正在运行的应用程序”概览...),但并没有像传统定义中那样“杀死了应用程序”。在标准的安卓系统中,它只是从概览屏幕的任务列表中移除了一个任务,尽管行为可能会因制造商而异。这可能会对您的进程产生影响。“SmartManager显示该应用程序使用了约40MB的内存”- 您可以考虑使用编程工具(堆转储、procstats等)来代替。 - CommonsWare
你显然面临着内存泄漏的问题,我们无法为你猜测。请捕获堆转储并进行分析。 - rds
显示剩余14条评论
4个回答

3
在Android Studio中,点击屏幕底部的 Android Monitor。点击 Monitors 选项卡。您将看到应用程序使用的内存图表。现在点击垃圾桶旁边的 Dump Java Heap 项目。系统开始收集信息并创建一个转储文件。您可以在 Android Studio 窗口右上角的 Analyze Tasks 选项卡中打开该文件。按下绿色箭头查找泄露内存的类。当您点击一个泄露的活动时,您将看到一个名为 Reference Tree 的窗口。浏览树形结构,找到深度为零的成员。那就是内存泄漏了。

感谢您提供详细的描述!不幸的是:没有检测到泄漏,只有一些重复的字符串... - Robin
1
@Robin,可能你并没有泄漏任何东西,只是无休止地积累一些数据。比如不断将一些数据放入内存缓存中等。 - Dmitry Zaytsev
@DmitryZaitsev,我也是在暗示同样的问题,@Robin你有没有调试代码并找到任何对资源的递归调用?请检查一下。 - khakishoiab

2
分析你的代码和图片,看看自己是否有任何错误。比如非常大的drawable图像,请确保正确实现了回收器或列表视图。 以下链接可能对你有所帮助。
  • 使用 leakcanary 库来检测确保没有内存泄漏

  • 检测用户从最近使用的应用程序中杀死应用程序

  • 从Android Studio附带的内存监视器中调用gc以获取正确的内存使用值,因为gc不会频繁运行。

  • 使用大堆大小。在清单文件中添加代码<application android:largeHeap="true"

  • 使用应用程序上下文来启动服务
    startService(new Intent(getApplicationContext(),MyService.class));


1

您的应用程序有一些定时器任务线程,它们一遍又一遍地创建,并且它们利用与网络相关的许多资源,因此您的堆大小不断增加,有时您的应用程序会因为内存不足而崩溃。


不,应用程序通常运行稳定。只有当我杀死它并重新启动它时才会出现这个问题。问题在于:一旦我杀掉一个应用程序,我想象操作系统会释放所有内存!关于服务的Qamar的想法看起来很有前途... - Robin

1

Some people think that task killers are important on Android. By closing apps running in the background, you’ll get improved performance and battery life – that’s the idea, anyway. In reality, task killers can reduce your performance.Android isn’t Windows and doesn’t manage processes like Windows does. Unlike on Windows, where there’s an obvious way to close applications, there’s no obvious way to “close” an Android application. This is by design and isn’t a problem.When you leave an Android app, going back to your home screen or switching to another app, the app stays “running” in the background. In most cases, the app will be paused in the background, taking up no CPU or network resources. Some apps will continue using CPU and network resources in the background, of course – for example, music players, file-downloading programs, or apps that sync in the background.When you go back to an app you were recently using, Android “unpauses” that app and you resume where you left off. This is fast because the app is still stored in your RAM and ready to be used again,hence consuming no further resources.

Task killers like smart manager think they know better than Android. They run in the background, automatically quitting apps and removing them from Android’s memory. They may also allow you to force-quit apps on your own, but you shouldn’t have to do this normally.If a task killer removes an app from your RAM and you open that app again, the app will be slower to load as Android is forced to load it from your device’s storage.Further s6 has Smart Manager and could be used as a widget or shortcut to battery and storage settings,however you should avoid using the Clean All optimization feature. This feature is said to improve device performance – as the Clean Master app aims to do – but its actual impact is questionable.

Meaning while still sometimes you kill an app it can still leave the traces on heap so next time when you start it ,it will show varyingly more amount of storage used which also fails the notion of default maxMemory() and the app ultimately might crash.Further such leaks are difficult to track as they aren't directly associated to app you killed in first instance.But still sometimes even after killing app, certain features like resources attained may keep running in background without even the app even knowing about them.i.e unnoticeable leaks.Thus you may want to use maxMemory()

which can be invoked (e.g., in your main activity's onCreate() method) as follows:

Runtime rt = Runtime.getRuntime();
long maxMemory = rt.maxMemory();
Log.v("onCreate", "maxMemory:" + Long.toString(maxMemory));

This method tells you how many total bytes of heap your app is allowed to use. Optimal Behaviour: An app can stay "running" in the background without any processes eating up your phone's resources. Android keeps the app in its memory so it launches more quickly and returns to its prior state. When your phone runs out of memory, Android will automatically start killing tasks on its own, starting with ones that you haven't used in awhile. What You Should Do Instead

That said, not all apps are created equal. Many of you have used task killers in the past and actually found that after freeing up memory, your phone works a bit better. It's more likely that this is because you've killed a bad app that was poorly coded, and (for example) keeps trying to connect to the internet even when it shouldn't. Any performance increase you experience is more likely because you killed the right app, not because you freed up loads of memory (or, in many cases, it's just placebo). Instead of killing all those apps, find out which ones are actually causing the problems.using a task killer to deal with a misbehaving app is like using a shotgun to kill a fly – you may fix your problem, but you’re inflicting a lot of other damage in the process.Now since S6 has 3 gigs of ram,it should'nt have problems,however implementation of smart manager can cause problems with some apps and isnt necessarily android version dependent.Also bare that heapsize tends to be based more on screen resolution, as higher-resolution screens tend to want to manipulate larger bitmaps. Instead of using a task killer in this situation, you should identify the bad app and uninstall it,or debug it, replacing it with an app that works properly. To pin down the app that’s misbehaving, you can try the

Watchdog Task Manager app

– it will show you which apps are actually using CPU in the background, not which apps are harmlessly being stored in memory.

Captivating Fact:

CyanogenMod, the popular community-developed Android ROM, won’t even accept bug reports from users using task killers, saying that they cause more problems than they solve.

A probable cause of issue

if you can try following at once,first of all go to settings >sounds and notifications > App notifications > select smart manager and select hide content on lock screen

then again back to settings go to lockscreen and security >Device security >Disable KNOX active protection and deactivate the anti malware option below KNOX active protection

in lock screen and security go to other security settings > disable send security reports and go down to ussage data access> disable smart manager so after that restart your device.Now see if the app works Also see performace concerns with s6


1
抱歉,我不明白这如何回答原帖的问题。 - Dmitry Zaytsev
1
@DmitryZaitsev,感谢您的回复,答案有两个方面需要注意。1-Android堆管理机制;2-与应用程序杀手、误解和可能的解决方法相关的低效率问题。 - khakishoiab

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接