简体   繁体   English

Android 6:应用每次启动时使用更多内存

[英]Android 6: App uses more memory each time it's started

Samsung Galaxy S6 with Android 6.0.0 and 6.0.1搭载 Android 6.0.0 和 6.0.1 的三星 Galaxy S6

My app runs fine, code analysis shows no mem leaks.我的应用程序运行良好,代码分析显示没有内存泄漏。 SmartManager shows that the app uses ~40MB of memory. SmartManager 显示该应用程序使用了大约 40MB 的内存。 I kill the app (using the "currently running apps" overview--- not sure how that's called in English, sorry. The key on the bottom left of the phone, that shows a list of miniatures of all running apps...), then start it again.我杀死了该应用程序(使用“当前正在运行的应用程序”概述——不知道英文是如何称呼的,抱歉。手机左下角的键,显示所有正在运行的应用程序的缩影列表......) ,然后重新开始。

Now SmartManager no longer shows ~40MB of memory for my app, but ... more.现在 SmartManager 不再为我的应用程序显示大约 40MB 的内存,而是......更多。 50. 100. 400. It varies, but growing each time. 50. 100. 400. 它有所不同,但每次都在增长。 After awhile, my app actually crashes with an OutOfMemoryException .过了一会儿,我的应用程序实际上崩溃OutOfMemoryException Sometimes even with "out-of-memory while trying to throw an OutOfMemoryException"...有时甚至“尝试抛出 OutOfMemoryException 时内存不足”......

I have to de-install and re-install the app in order to reset its memory consumption.我必须卸载并重新安装该应用程序才能重置其内存消耗。 The same APK behaves correctly on all other phones I've tested with (eg S5 with Android 5.0).相同的 APK 在我测试过的所有其他手机上都能正常运行(例如带有 Android 5.0 的 S5)。 No idea what's causing this.不知道是什么原因造成的。 No idea what could be causing this!不知道是什么原因造成的! Any help/hints/suggestions...?任何帮助/提示/建议......?

Edit: Same behaviour on S5 with Android 6.0.1, but not on S5 with Android 5.0!编辑:在S5采用Android 6.0.1相同的行为,但与Android 5.0 S5!

Edit2: Thanks, everyone who contributed. Edit2:谢谢,所有贡献者。 I still don't have an answer that I'm satisfied with, but a few ideas were mentioned that I will look into.我仍然没有满意的答案,但提到了一些我会研究的想法。 Most people were focusing on the MemLeak issue, and while that sounds reasonable I'd like to stress that there is no leak occuring on Android 4 or 5, only 6. (I have yet to test 7.) Also, the leak only occurs when I forcefully kill the app (which I need to do at times to test proper shutdown and re-start behaviour), and only without a debugger attached (which makes testing/debugging this behaviour a pain in the backside).大多数人都关注 MemLeak 问题,虽然这听起来很合理,但我想强调的是,Android 4 或 5 上没有发生泄漏,只有 6 个。(我还没有测试 7。)此外,泄漏只发生当我强行终止应用程序时(我有时需要这样做以测试正确的关闭和重新启动行为),并且仅在没有附加调试器的情况下(这使得测试/调试此行为成为一种痛苦)。

The app starts one service as a bound service .该应用程序将一项服务作为绑定服务启动。 Bound to the application context--- which according to the docs should clean itself up when the app stops.绑定到应用程序上下文——根据文档,当应用程序停止时,它应该自行清理。 Unfortunetely, time for this bounty is running out.不幸的是,这个赏金的时间已经不多了。 So far, the above mentioned service seems to be the most likely source of my problem.到目前为止,上述服务似乎是我问题的最可能来源。 As such, I will award Qamar the bounty, as he was the first to mention this both in the comments and his answer.因此,我将奖励 Qamar,因为他是第一个在评论和回答中提到这一点的人。 The rest of you, a big THANK YOU and up-vote.其余的人,非常感谢并投票。

In Android Studio, click Android Monitor at the bottom of your screen.在 Android Studio 中,单击屏幕底部的Android Monitor Click the tab Monitors .单击选项卡Monitors You see a graph of the memory that your app uses.您会看到应用程序使用的内存图表。 Now click the item Dump Java Heap which is next to the garbage van.现在单击垃圾车旁边的项目Dump Java Heap The system starts collecting the info and creates a dump file.系统开始收集信息并创建转储文件。 You can open the file in the Analyze Tasks tab in the upper right of your Android Studio window.您可以在 Android Studio 窗口右上角的Analyze Tasks选项卡中打开该文件。 Press the green arrow to find classes that leak memory.按绿色箭头查找泄漏内存的类。 When you click a leaking activity, you'll see a window Reference Tree .当您单击泄漏活动时,您将看到一个窗口Reference Tree Go through the tree, finding the member that has depth zero.遍历树,找到深度为零的成员。 That's the memory leak.这就是内存泄漏。

Analyse your code and images if anything wrong you are doing itself.如果您自己做错了什么,请分析您的代码和图像。 Like very large images of drawables.喜欢非常大的可绘制图像。 make sure you correctly implement recycler or list views.确保您正确实施回收站或列表视图。 The following links may helpful to you.以下链接可能对您有所帮助。

  • Use leakcanary library to detect and make sure there is not leaks使用leakcanary库检测并确保没有泄漏

  • Detect when user kill app from reccent apps 检测用户何时从最近的应用中杀死应用

  • Invoke gc from memory monitor comes with Android Studio for correct value of memory usage cause gc not run frequently.从内存监视器调用 gc 随 Android Studio 一起提供正确的内存使用值,导致 gc 不经常运行。

  • Use large heap size.使用大堆大小。 <application android:largeHeap="true" in manifest <application android:largeHeap="true"在清单中

  • Use app context to start services使用应用上下文启动服务
    startService(new Intent(getApplicationContext(),MyService.class));

您的应用程序有一些线程是一次又一次创建的计时器任务,它们正在利用许多与网络相关的资源,因此您的堆大小不断增加,有时您的应用程序内存不足并崩溃。

Some people think that task killers are important on Android.有些人认为任务杀手在 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.实际上,任务杀手会降低您的性能。Android 不是 Windows,也不像 Windows 那样管理进程。 Unlike on Windows, where there's an obvious way to close applications, there's no obvious way to “close” an Android application.与在 Windows 上有明显的关闭应用程序的方法不同,没有明显的方法来“关闭”Android 应用程序。 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.这是设计使然,不是问题。当您离开 Android 应用程序、返回主屏幕或切换到另一个应用程序时,该应用程序会在后台保持“运行”状态。 In most cases, the app will be paused in the background, taking up no CPU or network resources.大多数情况下,应用程序会在后台暂停,不占用 CPU 或网络资源。 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.当然,有些应用程序会继续在后台使用 CPU 和网络资源,例如音乐播放器、文件下载程序或在后台同步的应用程序。当您返回最近使用的应用程序时,Android “会取消暂停” ”那个应用程序,然后您从上次中断的地方继续。 This is fast because the app is still stored in your RAM and ready to be used again,hence consuming no further resources.这很快,因为该应用程序仍存储在您的 RAM 中并准备再次使用,因此不会消耗更多资源。

Task killers like smart manager think they know better than Android.像聪明的经理这样的任务杀手认为他们比 Android 更了解。 They run in the background, automatically quitting apps and removing them from Android's memory.它们在后台运行,自动退出应用程序并将它们从 Android 的内存中删除。 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.它们也可能允许您自行强制退出应用程序,但您通常不必这样做。 Android 被迫从您设备的存储中加载它。另外 s6 具有智能管理器,可以用作电池和存储设置的小部件或快捷方式,但是您应该避免使用“全部清除”优化功能。 This feature is said to improve device performance – as the Clean Master app aims to do – but its actual impact is questionable.据说此功能可以提高设备性能 - 正如 Clean Master 应用程序旨在做到的那样 - 但其实际影响值得怀疑。

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.ie unnoticeable leaks.Thus you may want to use maxMemory()这意味着虽然有时你杀死一个应用程序它仍然可以在堆上留下痕迹,所以下次当你启动它时,它会显示不同程度的使用的存储量,这也失败了默认maxMemory()的概念,应用程序最终可能会崩溃。此外,此类泄漏很难跟踪,因为它们与您首先杀死的应用程序没有直接关联。不明显的泄漏。因此您可能需要使用maxMemory()

which can be invoked (eg, in your main activity's onCreate() method) as follows:可以按如下方式调用(例如,在您的主要活动的onCreate()方法中):

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. Android 将应用程序保存在其内存中,因此它可以更快地启动并返回到之前的状态。 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.当您的手机内存不足时,Android 会自动开始自行终止任务,从您一段时间未使用的任务开始。 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.与其杀死所有这些应用程序,不如找出哪些应用程序实际上导致了问题。使用任务杀手来处理行为不端的应用程序就像使用霰弹枪杀死一只苍蝇——你可以解决你的问题,但你造成了很多过程中的其他损坏。现在因为 S6 有 3 个内存,它应该不会有问题,但是智能管理器的实现可能会导致某些应用程序出现问题,并且不一定依赖于 android 版本。另外,heapsize 往往更多地基于在屏幕分辨率上,因为更高分辨率的屏幕往往想要操作更大的位图。 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. – 它将显示哪些应用程序实际上在后台使用 CPU,而不是哪些应用程序无害地存储在内存中。

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. CyanogenMod 是流行的社区开发的 Android ROM,它甚至不会接受来自使用任务杀手的用户的错误报告,称它们造成的问题多于解决的问题。

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然后再次返回设置转到锁屏和安全 > 设备安全 > 禁用 KNOX 主动保护并停用 KNOX 主动保护下方的反恶意软件选项

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在锁定屏幕和安全中,转到其他安全设置> 禁用发送安全报告并转到使用数据访问> 禁用智能管理器,然后重新启动您的设备。现在查看应用程序是否有效同时查看 s6 的性能问题

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

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