Testing for memory usage
Sometimes, memory consumption is an important factor to measure the good behavior of the test target, be it an Activity, Service, Content Provider, or another component.
To test for this condition, we can use a utility test that you can invoke from other tests mainly after having run a test loop:
public void assertNotInLowMemoryCondition() { //Verification: check if it is in low memory ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); ((ActivityManager)getActivity() .getSystemService(Context.ACTIVITY_SERVICE)).getMemoryInfo(mi); assertFalse("Low memory condition", mi.lowMemory); }
This assertion can be called from other tests. At the beginning, it obtains MemoryInfo
from ActivityManager
using getMemoryInfo()
, after getting the instance using getSystemService()
. The lowMemory
field is set to true
if the system considers itself to currently be in a low memory situation.
In some cases, we want to dive even deeper into the resource usage and can obtain more...