Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How much memory is my app using?

Memory seems to be a big topic and I cant find the specific answer. I've got the answers on how much is available in the heap and I know how much should I use. I need the answer how to code to programatically determine how much memory is my app using of the heap? And how much total memory am I using?

like image 523
user1445716 Avatar asked Jul 09 '12 05:07

user1445716


2 Answers

This works:

  Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
  Debug.getMemoryInfo(memoryInfo);

  String memMessage = String.format("App Memory: Pss=%.2f MB\nPrivate=%.2f MB\nShared=%.2f MB",
  memoryInfo.getTotalPss() / 1024.0,
  memoryInfo.getTotalPrivateDirty() / 1024.0,
  memoryInfo.getTotalSharedDirty() / 1024.0);

  Toast.makeText(this,memMessage,Toast.LENGTH_LONG).show();
  Log.i("log_tag", memMessage);
like image 84
user1445716 Avatar answered Sep 20 '22 18:09

user1445716


use top -d 1 -n 1 Android Shell Command for getting all process list with process names or used memory by processes and then extract your process info from return string from System:

BufferedReader in = null;

        try {
            Process process = null;
            process = Runtime.getRuntime().exec("top -n 1 -d 1");

            in = new BufferedReader(new 
            InputStreamReader(process.getInputStream()));

            String line ="";
            String content = "";

            while((line = in.readLine()) != null) {
                content += line + "\n";
            }
            System.out.println(content);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            if(in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }


you will get String as:
PID  PPID USER   STAT VSZ  %MEM  %CPU COMMAND
9673 9672 root    R   712   0.1   0.0 top -d 1 -n 1
2489 2386 system  S   369m  87.7  0.0 system_server
3101 2386 app_23  S   304m  72.3  0.0 com.android.browser
2581 2386 radio   S   279m  66.3  0.0 com.android.phone
2585 2386 app_15  S   271m  64.4  0.0 com.android.launcher
like image 35
ρяσѕρєя K Avatar answered Sep 21 '22 18:09

ρяσѕρєя K