when I open new activities, I need to know my actual memory allocation (app can have huge memory allocation and I need to test real numbers). Is there any API for that?
Thanks
Edit code is not running... there is error on last line:
ActivityManager actvityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
ActivityManager.MemoryInfo mInfo = new ActivityManager.MemoryInfo ();
actvityManager.getMemoryInfo( mInfo );
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
for(int i = 0; i < procInfos.size(); i++)
{
if(procInfos.get(i).processName.equals("cz.process.a")) {
int field[]=new int []{procInfos.get(i).pid};
android.os.Debug.MemoryInfo[] info = actvityManager.getProcessMemoryInfo(field);
Still not solved. There was someone, who was an expert. But he offered me a code and wasn't able to describe it. He suggested code above, but on the last line there is a critical error.
I believe Debug and Debug.MemoryInfo classes can help you.
Related
i want to log ram usage of a given application at a given time rate. i wrote the code to get the full memory value used but do not know how to get the memory usage of a given PID.
please help me out
this is the code i used to get the memory
ActivityManager localActivityManager = (ActivityManager)getSystemService("activity");
ActivityManager.MemoryInfo localMemoryInfo = new ActivityManager.MemoryInfo();
localActivityManager.getMemoryInfo(localMemoryInfo);
Log.i("",String.valueOf(localMemoryInfo.availMem));
i think i can get it using public MemoryInfo[] getProcessMemoryInfo (int[] pids), but do not know how to code for it since im a android beginer
ActivityManager localActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); // use Context.ACTIVITY_SERVICE not the literal "activity"
List<ActivityManager.RunningAppProcessInfo> procsInfo = localActivityManager.getRunningAppProcesses();
int[] pids = new int[procsInfo.size()];
for (int i = 0; i < procsInfo.size(); i++) {
ActivityManager.RunningAppProcessInfo info = procsInfo.get(i);
pids[i] = info.pid;
}
Debug.MemoryInfo[] procsMemInfo = localActivityManager.getProcessMemoryInfo(pids);
// now walk the procsMemInfo array
If you schedule a recurring timer to periodically re-query for running pids and query for memory info you can use timestamps to compute the memory usage over time.
I'm working on a productivity app and I would like to be able to launch the users most recent app when a gesture is detected, however, I can't figure out why this code below isn't launching my most recent app.
ActivityManager m = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
RecentTaskInfo task = m.getRecentTasks(1, 0).get(0);
startActivity(task.baseIntent);
I also have this permission in my manifest
android.permission.GET_TASKS
Thank you for any help as to why this isn't working
RecentTaskInfo task = m.getRecentTasks(1, 0).get(0);
Since you are setting max number of results to 1, you are getting your own task with get(0). To get the result you are looking for, try setting the max # of results to 2 and use the second task from the returned list:
ActivityManager m = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
RecentTaskInfo task = m.getRecentTasks(2, 0).get(1);
startActivity(task.baseIntent);
Below code works for me to get recent 30 apps:
private static final int MAX_TASKS = 30;
final ActivityManager am = (ActivityManager)
mContext.getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RecentTaskInfo> recentTasks =
am.getRecentTasks(MAX_TASKS, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
i want to log ram usage of a given application at a given time rate. i wrote the code to get the full memory value used but do not know how to get the memory usage of a given PID.
please help me out
this is the code i used to get the memory
ActivityManager localActivityManager = (ActivityManager)getSystemService("activity");
ActivityManager.MemoryInfo localMemoryInfo = new ActivityManager.MemoryInfo();
localActivityManager.getMemoryInfo(localMemoryInfo);
Log.i("",String.valueOf(localMemoryInfo.availMem));
i think i can get it using public MemoryInfo[] getProcessMemoryInfo (int[] pids), but do not know how to code for it since im a android beginer
ActivityManager localActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); // use Context.ACTIVITY_SERVICE not the literal "activity"
List<ActivityManager.RunningAppProcessInfo> procsInfo = localActivityManager.getRunningAppProcesses();
int[] pids = new int[procsInfo.size()];
for (int i = 0; i < procsInfo.size(); i++) {
ActivityManager.RunningAppProcessInfo info = procsInfo.get(i);
pids[i] = info.pid;
}
Debug.MemoryInfo[] procsMemInfo = localActivityManager.getProcessMemoryInfo(pids);
// now walk the procsMemInfo array
If you schedule a recurring timer to periodically re-query for running pids and query for memory info you can use timestamps to compute the memory usage over time.
I'm stuck in this trouble and I can't figure it out. I googled a lot, but nothing gave me an answer.
I explain:
I have a BroadCastReceiver which runs in a different process (android:process=":anotherProcess")
I want to get this process name inside my BroadcastReceiver.
In this case for example, it could be: com.my.package:anotherProcess
Is it possible? How can I do it?
I've found a way. For anyone who will need this:
int id = Process.myPid();
String myProcessName =context.getPackageName();
ActivityManager actvityManager = (ActivityManager)context.getSystemService( context.ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
for(RunningAppProcessInfo procInfo : procInfos) {
if (id == procInfo.pid)
{
myProcessName = procInfo.processName;
}
}
I am using this block of code for determing, actual allocated memory.
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
MemoryInfo memoryInfo = new MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
long one=memoryInfo.availMem;
Log.i("memory free1", "" + memoryInfo.availMem);
While creating and asigning String[100] field, I get value 61440. Is it in bytes or kB? Documentation says nothing about it.
Thanks
ActivityManager.MemoryInfo:
availMem, totalMem, threshold are all in bytes.