All,
I would like to free up ram on my android for a data intensive operation and also be able to free up ram when the user wants to. The first thing I thought of doing was running system.gc(). However, that is not a guarantee that the garbage collector will work. Any suggestions on how to immediately and guarantee that ram is freed up when the code is called.? Also, how do the apps that free up ram when you click a widget accomplish this task?
I guess a better way to phrase this is: How do I manually free up memory like the Android OS?
Thx
You don't have to do this. Android will kill lower priorized apps to free memory if you need more than currently available.
My understanding is that the theory on Android is that running applications should relinquish memory through a callback mechanism when another process with higher priority (i.e., the current foreground process) needs more memory; additionally, the OS will start killing other applications if memory demands increase further.
Therefore, if you want your application to be able to use substantially all of the system's memory, you just need to make sure it stays in the foreground or use some other mechanism to ensure that its memory demands are higher priority than other memory demands (unfortunately, I couldn't say what that latter part would look like exactly).
There's not really much you can or should do manually to free up memory outside of your specific application. The OS is doing the right thing: as memory is needed, it tells applications "delete all non-essential data" and then after that if more memory is needed it tells them to get lost. Any manual approach to freeing up memory would basically do the same thing.
Edit: It sounds like you are wondering what other "memory-freeing" applications are doing. The answer is, they're taking the manual approach and doing the same thing the OS does -- sending signals to running processes. The reason this is unnecessary is that free ram doesn't actually do you any good before you need it. The OS will do this right before you need the extra space, which ensures no processes are killed before absolutely necessary.
Android has a garbage collector. iOS does not.
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 would like to know whether it is possible to trigger a system-wide garbage collection from a privileged system service in the Android Framework? I was thinking of something like a specialized signal that once trapped by the process running the dalvik VM will do a garbage collection in-place. Another alternative would be an API for AcitivityManager (or some other system service).
There is certainly nothing documented or supported for this.
If anything, I would expect them to have logic to prevent GC from occurring on 2+ processes at once. Triggering an immediate GC in all Dalvik processes would grind the device to a halt, particularly on single-core devices, as a couple of dozen processes all try to do GC at once.
Of course, you are welcome to download the Android source code, modify it to suit, and deploy a ROM mod with your changes on devices that you control.
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 :-)