Android 6: App uses more memory each time it's started
Asked Answered
P

4

11

Samsung Galaxy S6 with Android 6.0.0 and 6.0.1

My app runs fine, code analysis shows no mem leaks. SmartManager shows that the app uses ~40MB of memory. 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. 50. 100. 400. It varies, but growing each time. After awhile, my app actually crashes with an OutOfMemoryException. Sometimes even with "out-of-memory while trying to throw an 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 (e.g. S5 with Android 5.0). 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!

Edit2: Thanks, everyone who contributed. 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).

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. The rest of you, a big THANK YOU and up-vote.

Plain answered 23/11, 2016 at 11:54 Comment(19)
did you checked your problem in a non-samsung device?Rhabdomancy
@savepopulation: No, right now the only non-Samsung devices I have run Android 5 and older. I'm going to get some more devices, but that's going to take a few weeks.Plain
Actually I'm interested in your issue. you can use an emulator to answer my question because it's an important detail to focus on to solve your issue.Rhabdomancy
@savepopulation: I'm dependent on the phone's BlueTooth LE interface, but I'll try to get it running in the emulator. Might also be worth a try to strip away the BTLE code and see if that's part of the problem. Will do - on Monday. Have a good weekend! :)Plain
I'm following this issue since we had a very similar problem. Our app also crashed only on a S6. In the end we were so desperate we set a large heap true when we detected a S6 device.Verified
Can you post the stacktrace please? Also, do you keep all your drawables in same folder?Carroty
"I kill the app (using the "currently running apps" overview..." -- that does not "kill the app", for any conventional definition of that phrase. It removes a task the overview screen's list of tasks, at least in standard Android, though the behavior might be tweaked by manufacturers. It may have an impact on your process. "SmartManager shows that the app uses ~40MB of memory" -- you might consider using programming tools (heap dumps, procstats, etc.) instead.Knapsack
You obviously are facing a memory leak, and we can't guess for you. Capture a heap dump and analyze.Harpoon
@Knapsack Swiping a task from the recents almost entirely kills the app (with the exception of background services). See howtogeek.com/169549/…Harpoon
can you share more code to dig this problemMilkwhite
@Plain are you using any background sticky servicesDimpledimwit
Regarding stacktrace: The crash can happen anywhere in the code where I allocate memory... because some other part of the app eats all the RAM (over the course of several re-runs of the app).Plain
@rds: As soon as I connect the debugger, everything seems to run fine! Heap dump shows no leaks.Plain
@Qamar: I don't create any sticky services... that is: not intentionally. I create one service, and so far this seems to be the most promissing lead. I'll investigate!Plain
If "something eats all the ram" as you say, then you have a memory leak. Depending on the phone, you'll notice the leak sooner or later. The best way to find the leak is to make a heap dump, one way or the other, and analyze that. If you create the dump using Android Studio, make sure you analyze the right process.Flag
Try loading just your main activity, comment the java code for the time being, do you still see the issue?Vasya
are you storing anything to saved instance?Northey
Are you sure its your app with the problem or maybe smart manager itself. Try to kill your app the ordinary way from the settings and see whether the issue will be repeatedDunderhead
@NizaSiwale: I kill the app by swiping it from the recents list. I only used SmartManager to check memory consumption (and also because initially I suspected SmartManager's power-saving "feature" to wreak havoc).Plain
D
2

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

  • 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.

  • Use large heap size. <application android:largeHeap="true" in manifest

  • Use app context to start services
    startService(new Intent(getApplicationContext(),MyService.class));

Dimpledimwit answered 1/12, 2016 at 10:31 Comment(0)
F
3

In Android Studio, click Android Monitor at the bottom of your screen. Click the tab 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. 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. Press the green arrow to find classes that leak memory. When you click a leaking activity, you'll see a window Reference Tree. Go through the tree, finding the member that has depth zero. That's the memory leak.

Flag answered 1/12, 2016 at 14:31 Comment(3)
Thanks for the detailed description! Unfortunately: no leaks detected, just some duplicated strings...Plain
@Plain it might be that you are not leaking anything per se, but just accumulating some data endlessly. Like continuously putting some data to in-memory cache, etc.Hooghly
@DmitryZaitsev,I am also implicating same,@Plain did you debug and located any recursive calls in code to a resource.plz check.Hsining
D
2

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

  • 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.

  • Use large heap size. <application android:largeHeap="true" in manifest

  • Use app context to start services
    startService(new Intent(getApplicationContext(),MyService.class));

Dimpledimwit answered 1/12, 2016 at 10:31 Comment(0)
H
1

Your application has some threads are timer tasks that are creating again and again and they are utilizing many resources related to networks so your heap size is consistently increasing and at sometime your app is out of memory and crashes.

Homogamy answered 1/12, 2016 at 14:23 Comment(1)
No, the app runs stable - usually. Only when I kill it and start it again does this problem occur. Thing is: Once I kill an app, I would imagine the OS freeing all it's memory! Qamar's idea regarding services seems promising...Plain
H
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

Hsining answered 1/12, 2016 at 17:2 Comment(2)
Sorry, I fail to see how this is an answer to the OP's question.Hooghly
@DmitryZaitsev,thanks for responding ,the answer has two contexts if you follow.1-android heap management mechanism -2 Inefficiencies related to application killers,misconceptions and possible remedies.Hsining

© 2022 - 2024 — McMap. All rights reserved.