How do I discover memory usage of my application in Android?

前端 未结 9 2299
清酒与你
清酒与你 2020-11-21 06:27

How can I find the memory used on my Android application, programmatically?

I hope there is a way to do it. Plus, how do I get the free memory of the phone too?

相关标签:
9条回答
  • 2020-11-21 06:58

    There are a lot of answer above which will definitely help you but (after 2 days of afford and research on adb memory tools) I think i can help with my opinion too.

    As Hackbod says : Thus if you were to take all of the physical RAM actually mapped in to each process, and add up all of the processes, you would probably end up with a number much greater than the actual total RAM. so there is no way you can get exact amount of memory per process.

    But you can get close to it by some logic..and I will tell how..

    There are some API like android.os.Debug.MemoryInfo and ActivityManager.getMemoryInfo() mentioned above which you already might have being read about and used but I will talk about other way

    So firstly you need to be a root user to get it work. Get into console with root privilege by executing su in process and get its output and input stream. Then pass id\n (enter) in ouputstream and write it to process output, If will get an inputstream containing uid=0, you are root user.

    Now here is the logic which you will use in above process

    When you get ouputstream of process pass you command (procrank, dumpsys meminfo etc...) with \n instead of id and get its inputstream and read, store the stream in bytes[ ] ,char[ ] etc.. use raw data..and you are done!!!!!

    permission :

    <uses-permission android:name="android.permission.FACTORY_TEST"/>
    

    Check if you are root user :

    // su command to get root access
    Process process = Runtime.getRuntime().exec("su");         
    DataOutputStream dataOutputStream = 
                               new DataOutputStream(process.getOutputStream());
    DataInputStream dataInputStream = 
                               new DataInputStream(process.getInputStream());
    if (dataInputStream != null && dataOutputStream != null) {
       // write id to console with enter
       dataOutputStream.writeBytes("id\n");                   
       dataOutputStream.flush();
       String Uid = dataInputStream.readLine();
       // read output and check if uid is there
       if (Uid.contains("uid=0")) {                           
          // you are root user
       } 
    }
    

    Execute your command with su

    Process process = Runtime.getRuntime().exec("su");         
    DataOutputStream dataOutputStream = 
                               new DataOutputStream(process.getOutputStream());
    if (dataOutputStream != null) {
     // adb command
     dataOutputStream.writeBytes("procrank\n");             
     dataOutputStream.flush();
     BufferedInputStream bufferedInputStream = 
                         new BufferedInputStream(process.getInputStream());
     // this is important as it takes times to return to next line so wait
     // else you with get empty bytes in buffered stream 
     try {
           Thread.sleep(10000);
     } catch (InterruptedException e) {                     
           e.printStackTrace();
     }
     // read buffered stream into byte,char etc.
     byte[] bff = new byte[bufferedInputStream.available()];
     bufferedInputStream.read(bff);
     bufferedInputStream.close();
     }
    }
    

    logcat :

    You get a raw data in a single string from console instead of in some instance from any API,which is complex to store as you will need to separate it manually.

    This is just a try, please suggest me if I missed something

    0 讨论(0)
  • 2020-11-21 07:00

    In android studio 3.0 they have introduced android-profiler to help you to understand how your app uses CPU, memory, network, and battery resources.

    https://developer.android.com/studio/profile/android-profiler

    0 讨论(0)
  • 2020-11-21 07:04

    1) I guess not, at least not from Java.
    2)

    ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    MemoryInfo mi = new MemoryInfo();
    activityManager.getMemoryInfo(mi);
    Log.i("memory free", "" + mi.availMem);
    
    0 讨论(0)
提交回复
热议问题