Starting too many activities? - android

Hello is it possible to start like too many activities during the runtime of an application?
I'm developing a schedule application where you can slide from day to day where each day is a new activity. Now I'm wondering if there is a limit somehow of the number of activities you should start max. As when sliding a bit around you are soon to about 20 activities which are active.
I know activities will be killed when other applications need memory but while testing and closing the application by pressing the home button with about 20 activities 'running' htc sense seems to be restarting like it was closed due to a lack of free memory. When I just have a few activities running this doesn't happen. (htc desire)
Can this be caused by starting too many activities? If so, is there a 'limit' of amount of activities you should start max?
Edit: I read somewhere that the twitter for android app is developed by google. But in that application you can have like unlimited number of activities running also.

Even there is no such limitation in activity number, it is really a bad design to create too many activities. This is mainly because each activity is assigned specified minimum memory in VM and if it has IPC call, it will spend binder buffer.
We can re-design our app to reduce the number of activity. In your case, you can try to use one activity to show the day to day schedule. If it is hard to re-use, then we can close the previous activity as soon as possible to reduce the memory usage.

While there is technically no hard limit, some devices will have problems with a larger number of activities. Some of the devices I did tests on started having problems around 8 activities or so.

There is no such limit to my knowledge. Typically applications have like 5 open activities.
It is good programming practice to keep them to a lower number

You can create as many activity in an application as you want, and how much is the device can keep running. There isn't any limit of this, but sometimes the devices can't run them. Usually you can create your application, with lower number activities, what's the better solution.

Related

Flutter App does not start on android device due to exceeding memory usage

problem:
My flutter app did not launch or more precise was stuck in launch screen because I had to many other apps in the background e.g. Gallery, Camera, Browser etc. and so on. After closing those sleeping background tasks my flutter app launched like expected.
Questions:
A) Is it possible to e.g. open a dialog and inform the user of the nature of the problem. E.g. close some apps, please. Your cell phone ran out of Memory? If so, how? There are alot „change splash screen“ tutorials, would this be the correct place to implement such logic or is another widget preferable?
B) I know closing other apps is not possible due to android OS restrictions. But may be someone has another idea to atleast inform the user about the issue?
Best regards
Hugo

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

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.
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.
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 servicesstartService(new Intent(getApplicationContext(),MyService.class));
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.
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

android collapses so we restart everyday

Guys I have an application that works on the tablet.That is a smart house application.I root the tablet because I want to forbid the customers to use anything else but application on the tablet.Application is always visible and working non stop on the screen whenever tablet is on.But there is a problem.The problem is when application running about 1 day sometimes 2 days tablet collapses.That's why it needs to be restarted each time when that happens.I think it might be a problem of ram aka main memory.I just wonder about your thought.If I use garbage collector or some ram cleaner or ram expander, will it be a solution for me ?
Thanks in advance.
Your problem appears to be a memory leak in your application.
Since this is a complex issue to identify and/or resolve, you could try some common tools to aid you, such as:
https://medium.com/freenet-engineering/memory-leaks-in-android-identify-treat-and-avoid-d0b1233acc8#.z178mon4s
http://android-developers.blogspot.ch/2009/01/avoiding-memory-leaks.html
https://github.com/square/leakcanary

Don't keep activities - What is it for?

The title is pretty self-explanatory. I understand what this developer option does.
What I don't understand are the following points:
Why was that option introduced, in the first place?
After all the changes that the framework has seen throughout the years, is it still useful?
I am eager to know the reasons behind this option.
I believe it's a feature used for debugging purpose.
From the Titanium doc:
Don't keep activities under the Developer Options menu. When this
option is enabled, the Android OS will destroy an activity as soon as
it is stopped. It is intended to help developers debug their apps. For
example, it can simulate the case that Android will kill an activity
in the background due to memory pressure. In normal use, it is not
recommended to turn this option on because this may lead to unexpected
issues on the apps, such as freezes, force closes and reboots.
It sounds like it basically helps testing deterministically how your app behaves when the OS shuts it down due to any reason (out of memory and so on).
So, this replied to point 1. Point 2 is: Yes, I guess :)
EDIT: further references
On SO How to know "Don't keep activities" is enabled in ICS?
an interesting thread about that on androidcentral (reply from MagouyaWare)
The Android framework may destroy your activity any time it's in the background or backstack, and you should write your activities so they behave correctly when this happens. Exactly what that entails varies depending on what the activity does, but it generally includes implementing onSaveInstanceState(...) and restoring any previous state in onCreate(...).
The "don't keep activities" developer option simply changes the framework's behavior so it will always destroy your activity when it goes into the background or backstack. This allows you to test how your activity responds to what is normally a rare occurrence.
A link cited in another answer says:
In normal use, it is not recommended to turn this option on because this may lead to unexpected issues on the apps, such as freezes, force closes and reboots.
This is incorrect. If your activities are written properly, the only effect of having "don't keep activities" turned on should be (possibly) slightly higher battery and CPU usage from constantly saving and restoring persistent state. Any apps that exhibit "unexpected issues" or force closes when this option is on are broken and need to be fixed. As a developer, I habitually leave "don't keep activities" turned on all the time. I've seen a lot of buggy apps, even some of Google's own. But it's never caused a reboot, and I don't think there's any way it could.
I had at least two issues when this was enabled:
1) I was getting an error "Unable to complete previous operation due to low memory" that prevented uploading of any attachment to a website using Chrome, but not with Firefox. I found elsewhere that enabling the "Don't keep activities open" option could have been the problem - it was.
2) I was unable to move files to the SD card. Disabling the "Don't keep activities open" option got round this problem.
Also found elsewhere it can slow things up generally because caching is compromised.
Sometimes apps that I had moved to the SD card suddenly 'greyed out' and became inaccessible. Rebooting solved this, but was becoming an irritation. Too soon to tell if that was another problem caused by this 'feature'.
These problems (and issues with the microphone in Zoom) all started within the last month. I don't know how DKAO ever got enabled, but it's a no-no as far as I'm concerned.

Non related activities

Is there any way to suppress or kill all activities and non crucial applications and background processes not related to a current activity?
I have an app that runs in real time and there are occasional intermittent timing hiccups which I am attributing to this.
Android will do this automatically as it needs the resources. Plus, that's kind of a jerk move to other applications.
There is also other things that can cause "hiccups" such as a really large garbage collection.

Categories

Resources