When an app gets put in the background and the app takes up too much memory. Is it possible for the operating system to corrupt the memory so that when the app is launched again unexpected behavior might ensue? Or will it completely kill the app when it needs the memory so that when I relaunch I'm in a clean state?
The short answer is no.
For more detail on how Android manages memory and how it affects applications, services, etc, see http://mobworld.wordpress.com/2010/07/05/memory-management-in-android/ and http://android-developers.blogspot.com/2010/04/multitasking-android-way.html
Related
If the memory or CPU usage of an android device is unusually high,will android take any kind of actions in that case? Like killing apps, reboot etc? Is there an in-built service in android that continuously monitors the CPU and memory usage and take necessary actions in case of any abnormal behavior? If available, at what threshold values of memory and cpu usage will it take action? I did perform a thorough google search but couldn't find any answers. I am talking about inbuilt feature of android and not any third party apps.
I quote this from a good article on android processes
Android does a good job of automatically managing these processes, which is why you don’t need a task killer on Android.
When Android needs more system resources, it will start killing the least important processes first. Android will start to kill empty and background processes to free up memory if you’re running low. If you need more memory — for example, if you’re playing a particularly demanding game on a device without much RAM, Android will then start to kill service processes, so your streaming music and file downloads may stop.
In most cases, Android does this all without you needing to worry about it. Android intelligently uses your device’s RAM for caching apps and other data, because there’s no point in leaving your RAM empty.
You should also read the official documentation on memory management by android operating system.
I think this should clear your doubts. Feel free to ask if you have more...
I am getting a message like "your application restarted 9 times in 5 minutes()", when I tried to clean Junk Files using "Clean Master" application.
Please find the screenshot for this issue. I am not able to figure out why this message is coming. Any idea on this???
These apps may have once been useful but the Android has progressed far enough that they are now outdated, unnecessary and rather harmful.
On Windows, you want to keep as much RAM as possible as you can so that programs have enough room to operate. When RAM fills up, Windows is forced to start using hard drive space as virtual RAM and hard drives are much slower than physical RAM.
This is not true for Android.
Android’s operating system has its own native handler for assigning RAM to apps and making sure that all of it is being used in the most optimal way. In fact, Android purposely tries to keep apps loaded into RAM for better performance. RAM is fast, remember? On mobile devices, every bit of speed is critical for a good user experience, so keeping apps in RAM is actually a good thing.
Not only does Android handle RAM assignment, but it also keeps track of background apps so they don’t use up unnecessary processor resources. There’s no noticeable performance hit for leaving apps loaded in RAM.
App killers, memory boosters, performance enhancers, etc all claim wiping memory will speed up the phone. With current versions of Android, that's simply not true. It's actually the opposite. What will happen is the task killer app kills other apps which use resources to process. Then the OS restarts those, or other apps to fill the memory again, which takes even more resources. The task killer app kills again and the process repeats over and over. So in reality, those kinds of apps reduce performance and increase battery usage by restarting apps again and again. Remember, those dormant apps don't consume any additional battery or CPU in the first place.
Once android application terminates (closed either by user or by OS), is leaked memory and all other memory resources being freed? Or we need to do it manually? What will be the best way to track and handle memory issues in android?
a memory leak in a situation where some objects are not used by the application any more, but GC fails to recognize them as unused.
GC is automatically done periodically by the JVM.
An android application can only be terminated by the OS. (safely at least)
if the app is closed by the user, it still runs in the background, once the os decides that it needs to close the application, either to free up some memory or the application stack is full, it will terminate the application and the memory will be freed.
If the application is terminated, all resources used by the application is freed.
99.9% of the time you do not need to call garbage collection on android. The OS takes care of itself. Would probably cause more harm to manually call GC
There are some cases where memory is leaked, but there are workarounds to dispose things in these cases.
If process terminates then Yes, but that does not happen very often. Android is designed to keep processes in background to start them quickly once user want to go back to you app.
You should not rely that your app will be terminated to fix some memory leaks. There are tools to fix them, like dumping HPROF file and using memory analyzer, also using weak references, and using good programming practicies - mostly not leaking activities.
[edit] - there are resources that are not always freed on process end, while working with android TTS apis, I found that after few app crashes I have to reset device to be able to use svox voices.
I am working on a project of 3rd-party Android Launcher.
I find it being killed often by OS when running in background, even it is the default Launcher.
OS will kill many apps when available memory is not enough, I guest that the reason why my Launcher is killed.
So my question is what should I do to decrease the chance of being killed ?
Nothing. You should find a way to handle the event where the OS kills your process, using a hacky approach to keep it in memory is bad, bad design and could lead to all sorts of issues on different devices.
You really shouldn't even care that the system is killing your process, the launcher should handle an intent and either resume if present or start if killed.
I've seen that if you kill some process, it restarts immediately and keep running. How this implemented? How to inform system that my service should not be killed and if that happened - restart it.
Android just does it. As an OS, Android is specifically designed to run with ram full at all times. So if you kill, or an app force closes it will, if their is room in ram for it be restarted by Android to fill the ram back up.
It does this because even the "fastest" phones are snails to even the most average of desktops and keeping as many programs that you use loaded in ram as possible enables it to simply "resume" the program instead of having to go through the slooooow, time wasting process of having to reload it back into ram and then running it from the beginning.
Android kernels have their own task manager. This means that it will be more efficient than any app-based task manager as it is run at the kernel level, and it should be left up to that task killer to decide when to free up memory or not free up memory. Let Android do what it was designed to do. Anything you do to try and force it to rerun a program or stop a program will, in the long run, slow it down more and possibly even cause stability issues.
However the short answer to your question is that there is no way to tell the "system" that your process should not be killed or to restart when it is closed. That is a choice made entirely by the kernel level task manager.
BTW, why would you want to? I ask this because I don't think you have thought this through very well. Remember, unlike IOS owners who are used to and expect total control over their device to be in the hands of Apple(for good or ill) Android owners expect, and will have control over their device. If you try to take that away from them, you will likely find most people uninstalling your service. And demanding their money back if you charge a fee for it.
I hope this has helped.
Not sure this is something that's good, but I've seen malware processes that have "buddy" processes that revive each other when one or more go down.
I hope whatever you're doing is ethical :-)