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.
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...
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.
This question already has answers here:
Stresstest Memory on Android
(2 answers)
Closed 3 years ago.
I've been reading up on the Android dalvik, and I was curious as to how one would go about stress testing the Dalvik to evaluate its stability. I understand the Dalvik is meant for memory an processor constrained devices. So would allocating a lot of memory/increasing frequency of some CPU cores and then launching multiple applications be a way to test the stability?
I also understand that each independent process gets itsown instance of the Dalvik. So another possibility to stress the Dalvik would be to launch multiple applications that share a single process and a single instance of the Dalvik and see how stable the Dalvik is.
I would like to to know if either of these are good ways to measure the stability of the Dalvik. If both of them are good ways, which one would be a better test?
Thanks!
It's difficult to stress every part of a VM all at once.
You can write memory stress tests that exercise the heap and the garbage collector, synchronization stress tests (like the JSR-166 java.util.concurrent test suite), CPU stress tests that do lots of integer and floating-point computations on different cores simultaneously. And so on.
The trick is to write a test that does what you think it does -- a surprising number of "multi-core" tests end up single-threaded because of unexpected dependencies -- and whose results can be evaluated for correctness. A test that successfully causes instability isn't useful unless that fact is communicated to the user in some way. Making the VM crash is a pretty good way. :-)
Running multiple apps and services in the same process is theoretically possible but rare in practice. I don't think you'd actually be able to stress the system out any better this way, since only one app is in the foreground at a time, and if you're making requests of a service one thread will wait for the response while the other runs. You're better off just have one app with multiple threads, so you can control exactly what each does and how they interact.
Before you can do any of this, you need to define the scope of "stability". Simply running many apps isn't going to turn up anything, since there are hundreds of millions of devices running billions of instances of Dalvik with essentially no failures due to the VM itself (but any number due to bugs in apps, the framework, 3rd-party libraries, etc). Dalvik hasn't changed much since Android 4.0 (Ice Cream Sandwich) shipped two years back.
We want to test, in our android app, the behaviour when the VM closes the app due to other applications consuming too much memory.
Is there an app to simulate this sort of memory-hog behaviour that correctly induces the right actions in the android VM?
Alternatively: is there a way to cause the equivalent signal while using Eclipse to force the system to pretend like there's a huge memory hog of an app out there?
No, every application has its own heap size limit and will all be killed if OOM thinks that it is malfunctioning. If you want to seize the memory, try to do it in the native code, it is out side the DVM management.
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.