Sometimes memory consumption is an important factor to measure the good behavior of the test target, be it an Activity, Service, ContentProvider, or other 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 final 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 the MemoryInfo
from ActivityManager
using getMemoryInfo()
, after getting the instance using getSystemService()
. The field lowMemory
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 in the resource usage...