導航:首頁 > 操作系統 > android獲取可用內存

android獲取可用內存

發布時間:2023-09-14 01:27:59

1. android 高通cpu怎麼用adb查看mem

.一、利用Android API函數查看
1.1 ActivityManager查看可用內存。
ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
am.getMemoryInfo(outInfo);
outInfo.availMem即為可用空閑凱畝睜內存。
1.2、android.os.Debug查詢PSS,VSS,USS等單個進程使用內存信息
MemoryInfo[] memoryInfoArray = am.getProcessMemoryInfo(pids);
MemoryInfo pidMemoryInfo=memoryInfoArray[0];
pidMemoryInfo.getTotalPrivateDirty();

getTotalPrivateDirty()
Return total private dirty memory usage in kB. USS

getTotalPss()
Return total PSS memory usage in kB.
PSS
getTotalSharedDirty()
Return total shared dirty memory usage in kB. RSS

二、直接對Android文件進行解析查詢,
/proc/cpuinfo系統CPU的類型等多種信息。
/proc/meminfo 系統內存使用信息

/proc/meminfo
MemTotal: 16344972 kB
MemFree: 13634064 kB
Buffers: 3656 kB
Cached: 1195708 kB
我們查看機器內存時,會發現MemFree的值很小。這主要是因為,在linux中有這么一種思想,內存不用白不用,因此它盡可能的cache和buffer一些數據,以方便下次使用。但實際上這些內存也是可以立刻拿來使用的。
所以 空閑內存=free+buffers+cached=total-used
通過讀取文件/proc/meminfo的信息獲取Memory的總量。
ActivityManager. getMemoryInfo(ActivityManager.MemoryInfo)獲取當前的可用Memory量。

三、通過Android系統提供的Runtime類,執行adb 命令(top,procrank,ps...等命令)查詢
通過對執行結果的標准控制台輸出進行解析。這樣大大的擴展了Android查詢功能.例如:
final Process m_process = Runtime.getRuntime().exec("/system/bin/top -n 1");
final StringBuilder sbread = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(m_process.getInputStream()), 8192);

# procrank
Runtime.getRuntime().exec("/system/xbin/procrank");
內存耗用:VSS/RSS/PSS/USS
Terms
• VSS - Virtual Set Size 虛擬耗用內存(包含共享庫佔用的耐孝內存)
• RSS - Resident Set Size 實際使用物理內存(包含共享庫佔用的內存)
• PSS - Proportional Set Size 實際使用的物理內存(比例分配共享庫佔用的內存)
• USS - Unique Set Size 進程獨自佔用的物理內存(不包含共享庫佔用的內存)
一般來說內存佔用大小有盯歲如下規律:VSS >= RSS >= PSS >= USS
USS is the total private memory for a process, i.e. that memory that is completely unique to that process.USS is an extremely useful number because it indicates the true incremental cost of running a particular process. When a process is killed, the USS is the total memory that is actually returned to the system. USS is the best number to watch when initially suspicious of memory leaks in a process.
轉載

2. android 怎樣獲取當前apk所佔用的內存

這個方法有很多,常用的是 adb shell mpsys meminfo <package_name> 這可以看到比較全面的信息,由於Android是有內存共享的,所以通常有 VSS,RSS,PSS,USS等不同的內存表述,比較常用的是PSS,會將共享庫按照比例分配給當前內存

3. Android獲取系統cpu信息,內存,版本,電量等信息

1、CPU頻率,CPU信息:/proc/cpuinfo和/proc/stat

通過讀取文件/proc/cpuinfo系統CPU的類型等多種信息。

讀取/proc/stat 所有CPU活動的信息來計算CPU使用率

下面我們就來講講如何通過代碼來獲取CPU頻率:

復制代碼 代碼如下:

package com.orange.cpu;

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStream;

public class CpuManager {

// 獲取CPU最大頻率(單位KHZ)

// "/system/bin/cat" 命令行

// "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" 存儲最大頻率的文件的.路徑

public static String getMaxCpuFreq() {

String result = "";

ProcessBuilder cmd;

try {

String[] args = { "/system/bin/cat",

"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };

cmd = new ProcessBuilder(args);

Process process = cmd.start();

InputStream in = process.getInputStream();

byte[] re = new byte[24];

while (in.read(re) != -1) {

result = result + new String(re);

}

in.close();

} catch (IOException ex) {

ex.printStackTrace();

result = "N/A";

}

return result.trim();

}

// 獲取CPU最小頻率(單位KHZ)

public static String getMinCpuFreq() {

String result = "";

ProcessBuilder cmd;

try {

String[] args = { "/system/bin/cat",

"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq" };

cmd = new ProcessBuilder(args);

Process process = cmd.start();

InputStream in = process.getInputStream();

byte[] re = new byte[24];

while (in.read(re) != -1) {

result = result + new String(re);

}

in.close();

} catch (IOException ex) {

ex.printStackTrace();

result = "N/A";

}

return result.trim();

}

// 實時獲取CPU當前頻率(單位KHZ)

public static String getCurCpuFreq() {

String result = "N/A";

try {

FileReader fr = new FileReader(

"/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");

BufferedReader br = new BufferedReader(fr);

String text = br.readLine();

result = text.trim();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return result;

}

// 獲取CPU名字

public static String getCpuName() {

try {

FileReader fr = new FileReader("/proc/cpuinfo");

BufferedReader br = new BufferedReader(fr);

String text = br.readLine();

String[] array = text.split(":s+", 2);

for (int i = 0; i < array.length; i++) {

}

return array[1];

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

}

2、內存:/proc/meminfo

復制代碼 代碼如下:

public void getTotalMemory() {

String str1 = "/proc/meminfo";

String str2="";

try {

FileReader fr = new FileReader(str1);

BufferedReader localBufferedReader = new BufferedReader(fr, 8192);

while ((str2 = localBufferedReader.readLine()) != null) {

Log.i(TAG, "---" + str2);

}

} catch (IOException e) {

}

}

3、Rom大小

復制代碼 代碼如下:

public long[] getRomMemroy() {

long[] romInfo = new long[2];

//Total rom memory

romInfo[0] = getTotalInternalMemorySize();

//Available rom memory

File path = Environment.getDataDirectory();

StatFs stat = new StatFs(path.getPath());

long blockSize = stat.getBlockSize();

long availableBlocks = stat.getAvailableBlocks();

romInfo[1] = blockSize * availableBlocks;

getVersion();

return romInfo;

}

public long getTotalInternalMemorySize() {

File path = Environment.getDataDirectory();

StatFs stat = new StatFs(path.getPath());

long blockSize = stat.getBlockSize();

long totalBlocks = stat.getBlockCount();

return totalBlocks * blockSize;

}

4、sdCard大小

復制代碼 代碼如下:

public long[] getSDCardMemory() {

long[] sdCardInfo=new long[2];

String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {

File sdcardDir = Environment.getExternalStorageDirectory();

StatFs sf = new StatFs(sdcardDir.getPath());

long bSize = sf.getBlockSize();

long bCount = sf.getBlockCount();

long availBlocks = sf.getAvailableBlocks();

sdCardInfo[0] = bSize * bCount;//總大小

sdCardInfo[1] = bSize * availBlocks;//可用大小

}

return sdCardInfo;

}

5、電池電量

復制代碼 代碼如下:

private BroadcastReceiver batteryReceiver=new BroadcastReceiver(){

@Override

public void onReceive(Context context, Intent intent) {

int level = intent.getIntExtra("level", 0);

// level加%就是當前電量了

}

};

registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

6、系統的版本信息

復制代碼 代碼如下:

public String[] getVersion(){

String[] version={"null","null","null","null"};

String str1 = "/proc/version";

String str2;

String[] arrayOfString;

try {

FileReader localFileReader = new FileReader(str1);

BufferedReader localBufferedReader = new BufferedReader(

localFileReader, 8192);

str2 = localBufferedReader.readLine();

arrayOfString = str2.split("s+");

version[0]=arrayOfString[2];//KernelVersion

localBufferedReader.close();

} catch (IOException e) {

}

version[1] = Build.VERSION.RELEASE;// firmware version

version[2]=Build.MODEL;//model

version[3]=Build.DISPLAY;//system version

return version;

}

7、mac地址和開機時間

復制代碼 代碼如下:

public String[] getOtherInfo(){

String[] other={"null","null"};

WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);

WifiInfo wifiInfo = wifiManager.getConnectionInfo();

if(wifiInfo.getMacAddress()!=null){

other[0]=wifiInfo.getMacAddress();

} else {

other[0] = "Fail";

}

other[1] = getTimes();

return other;

}

private String getTimes() {

long ut = SystemClock.elapsedRealtime() / 1000;

if (ut == 0) {

ut = 1;

}

int m = (int) ((ut / 60) % 60);

int h = (int) ((ut / 3600));

return h + " " + mContext.getString(R.string.info_times_hour) + m + " "

+ mContext.getString(R.string.info_times_minute);

}

4. 如何查看Android手機的內存

打開手機找到設置點擊進入

閱讀全文

與android獲取可用內存相關的資料

熱點內容
黑上宏命令 瀏覽:644
mac解壓壓縮包有密碼 瀏覽:704
命令與征服知乎 瀏覽:561
小時代pdf 瀏覽:221
化工設備第三版答案pdf 瀏覽:465
防火卷簾控制器單片機程序 瀏覽:16
rdlcpdf 瀏覽:109
鏈表實現快速排序python 瀏覽:590
php輸出命令 瀏覽:987
d站app叫什麼名字 瀏覽:172
oppor系列如何解除應用加密 瀏覽:601
程序員那麼可愛姜逸城初戀 瀏覽:499
modbustcp編程 瀏覽:493
實況為什麼安卓看不了 瀏覽:129
Java多線程Queue 瀏覽:95
雲伺服器499元三年 瀏覽:980
nbd源碼 瀏覽:847
x86在arm上編譯 瀏覽:8
linux怎麼配置網路 瀏覽:307
程序員想要的小禮物 瀏覽:188