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.
Related
When i run my app on LG G4 or Note 3 which has 3GB of RAM, it shows that the device has 2GB of ram. Can someone explain where is the problem?
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
long freeMemory = mi.availMem / 1048576L;
long totalMemory = mi.totalMem / 1048576L;
long usedMemory = totalMemory - freeMemory;
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(Util.MyPREFERENCES, 0);
String status = sharedPreferences.getString(Util.KEY_STATUS, null);
if (status.equals("clean")) {
tvFree.setText(freeMemory + " MB");
tvTotal.setText(totalMemory + " MB");
tvUsed.setText(usedMemory + " MB");
stateImage.setImageResource(R.drawable.greenstate);
rippleBackground.stopRippleAnimation();
}
It does not seem to be a clear API for that but I used a simple hack:
Setup a lookup table with some of the power of 2s (like first 15-20) and find the nearest value to the "OS memory" retrieved from the API.
Had a similar problem to yours:
Get android device's all RAM memory programmatically and not only what is only allocated to user processes
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 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.
Currently, I'm working on to get a list of all running applications. I've been able to do this in the following way,
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(mInfo);
List<RunningAppProcessInfo> listOfRunningProcess = activityManager
.getRunningAppProcesses();
Log.d(TAG, "XXSize: " + listOfRunningProcess.size());
l1 = findViewById(R.id.Layout1);
for (RunningAppProcessInfo runningAppProcessInfo : listOfRunningProcess) {
if (runningAppProcessInfo.uid > 1026) {
uID = runningAppProcessInfo.uid;
Log.d(TAG, "ANS " + runningAppProcessInfo.processName + " Id :"
+ runningAppProcessInfo.pid + " UID: " + uID);
}
}
It gives a list of all running applications. Now I want to differentiate these applications on the basis of whether they are Internal or External apps. Is there any way to separate out internal and external apps.
Here is the thread which checks the whether the app is system app or not.
so that if you get the list of apps then you can check the apps whether the apps are system apps or not using the above thread. so that you can separate the apps.
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.