下面列出了android.app.ActivityManager#MemoryInfo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@ReactMethod(isBlockingSynchronousMethod = true)
public double getTotalMemorySync() {
ActivityManager actMgr = (ActivityManager) getReactApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
if (actMgr != null) {
actMgr.getMemoryInfo(memInfo);
} else {
System.err.println("Unable to getMemoryInfo. ActivityManager was null");
return -1;
}
return (double)memInfo.totalMem;
}
/**
* Print Memory info.
*/
@TargetApi(Build.VERSION_CODES.CUPCAKE)
public static ActivityManager.MemoryInfo printMemoryInfo(Context context) {
ActivityManager.MemoryInfo mi = getMemoryInfo(context);
if (LogZSDK.INSTANCE.levelOK(LogLevel.i)) {
StringBuilder sb = new StringBuilder();
sb.append("_______ Memory : ");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
sb.append("\ntotalMem :").append(mi.totalMem);
}
sb.append("\navailMem :").append(mi.availMem);
sb.append("\nlowMemory :").append(mi.lowMemory);
sb.append("\nthreshold :").append(mi.threshold);
LogZSDK.INSTANCE.i(sb.toString());
}
return mi;
}
private boolean isLowMemory() {
ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
if (activityManager != null) {
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
return memoryInfo.lowMemory;
} else {
return true;
}
}
public long getAvailMemory(Context context) {
// 获取android当前可用内存大小
ActivityManager.MemoryInfo memoryInfo
= new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
// 当前系统可用内存 ,将获得的内存大小规格化
return memoryInfo.availMem;
}
/**
* 获取系统当前可用内存大小
*
* @param context
* @return
*/
@TargetApi(Build.VERSION_CODES.CUPCAKE)
public static String getAvailMemory(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mi);
return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化
}
public static String getRamInfo(Context context) {
long totalSize;
long availableSize;
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
totalSize = memoryInfo.totalMem;
availableSize = memoryInfo.availMem;
return String.format("%s / %s", Formatter.formatFileSize(context, availableSize), Formatter.formatFileSize(context, totalSize));
}
/**
* Get available memory info.
*/
@TargetApi(Build.VERSION_CODES.CUPCAKE)
public static String getAvailMemory(Context context) {// 获取android当前可用内存大小
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mi);
// mi.availMem; 当前系统的可用内存
return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化
}
public long getTotalMemory() {
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
return memoryInfo.availMem;
}
private static long getFreeMemory(Context activity) {
try {
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
ActivityManager activityManager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
long availableMegs = mi.availMem / 1048576L; // in megabyte (mb)
return availableMegs;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
private boolean isLowMemory() {
ActivityManager activityManager = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
if (activityManager != null) {
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
return memoryInfo.lowMemory;
} else {
return true;
}
}
private ProcessManager(Context context) {
this.mContext = context;
mRunningProcessList = new ArrayList<>();
mPackageManager = mContext.getPackageManager();
mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
mMemoryInfo = new ActivityManager.MemoryInfo();
}
void updateStatus() {
final MemoryInfo mem_info = new ActivityManager.MemoryInfo();
final ActivityManager systemService = (ActivityManager)activity.getSystemService(Context.ACTIVITY_SERVICE);
systemService.getMemoryInfo(mem_info);
rightStatus.setText(String.format("Available memory: %s B", Util.nf.format((mem_info.availMem)) + "/" + Util.nf.format(mem_info.totalMem)));
//numProc_label.setText("Number of processes: " + display_process.size());
selectionStatusTV.setText(selectedInList1.size() + "/" + lpinfo.size() + "/" + display_process.size());
adapter.notifyDataSetChanged();
}
private static ActivityManager.MemoryInfo getMemoryInfo(ActivityManager activityManager) {
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
if (activityManager != null) {
activityManager.getMemoryInfo(mi);
}
return mi;
}
private static void checkMemoryConstraints() {
final ActivityManager actManager = (ActivityManager) xdrip.getAppContext().getSystemService(Context.ACTIVITY_SERVICE);
final ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
actManager.getMemoryInfo(memInfo);
final long totalMemory = memInfo.totalMem;
// TODO react to total memory
}
/**
* @param debugName The name supplied in name is used as a filename and will be displayed
* as the target of the corresponding symbolic link in the directory
* /proc/self/fd/. The displayed name is always prefixed with memfd:
* and serves only for debugging purposes. Names do not affect the
* behavior of the file descriptor, and as such multiple files can have
* the same name without any side effects.
* @param sizeEstimate An estimated upper bound on this file. This is used to check there will be
* enough RAM available and to register with a global counter of reservations.
* Use zero to avoid RAM check.
* @return MemoryFileDescriptor
* @throws MemoryLimitException If there is not enough available RAM to comfortably fit this file.
* @throws MemoryFileCreationException If fails to create a memory file descriptor.
*/
public static MemoryFileDescriptor newMemoryFileDescriptor(@NonNull Context context,
@NonNull String debugName,
long sizeEstimate)
throws MemoryFileException
{
if (sizeEstimate < 0) throw new IllegalArgumentException();
if (sizeEstimate > 0) {
ActivityManager activityManager = ServiceUtil.getActivityManager(context);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
synchronized (MemoryFileDescriptor.class) {
activityManager.getMemoryInfo(memoryInfo);
long remainingRam = memoryInfo.availMem - memoryInfo.threshold - sizeEstimate - sizeOfAllMemoryFileDescriptors;
if (remainingRam <= 0) {
NumberFormat numberFormat = NumberFormat.getInstance(Locale.US);
Log.w(TAG, String.format("Not enough RAM available without taking the system into a low memory state.%n" +
"Available: %s%n" +
"Low memory threshold: %s%n" +
"Requested: %s%n" +
"Total MemoryFileDescriptor limit: %s%n" +
"Shortfall: %s",
numberFormat.format(memoryInfo.availMem),
numberFormat.format(memoryInfo.threshold),
numberFormat.format(sizeEstimate),
numberFormat.format(sizeOfAllMemoryFileDescriptors),
numberFormat.format(remainingRam)
));
throw new MemoryLimitException();
}
sizeOfAllMemoryFileDescriptors += sizeEstimate;
}
}
int fileDescriptor = FileUtils.createMemoryFileDescriptor(debugName);
if (fileDescriptor < 0) {
Log.w(TAG, "Failed to create file descriptor: " + fileDescriptor);
throw new MemoryFileCreationException();
}
return new MemoryFileDescriptor(ParcelFileDescriptor.adoptFd(fileDescriptor), sizeEstimate);
}
public static long getFreeMemory() {
ActivityManager am = (ActivityManager) BlockCanaryEx.getConfig().getContext().getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mi);
return mi.availMem / 1024;
}
public ActivityManager.MemoryInfo getSystemMemInfo() {
return systemMemInfo;
}
@org.xwalk.core.JavascriptInterface
@android.webkit.JavascriptInterface
public String getDeviceInfo() {
try {
if (activityManager == null) {
return jsonError("ActivityManager not set. Cannot retrieve RAM info.");
}
if (connectivityManager == null) {
return jsonError("ConnectivityManager not set. Cannot retrieve network info.");
}
String versionName = parent.getPackageManager()
.getPackageInfo(parent.getPackageName(), 0)
.versionName;
JSONObject appObject = new JSONObject();
appObject.put("version", versionName);
String androidVersion = Build.VERSION.RELEASE;
int osApiLevel = Build.VERSION.SDK_INT;
String osVersion = System.getProperty("os.version") + "(" + android.os.Build.VERSION.INCREMENTAL + ")";
JSONObject softwareObject = new JSONObject();
softwareObject
.put("androidVersion", androidVersion)
.put("osApiLevel", osApiLevel)
.put("osVersion", osVersion);
String device = Build.DEVICE;
String model = Build.MODEL;
String manufacturer = Build.BRAND;
String hardware = Build.HARDWARE;
Map<String, String> cpuInfo = getCPUInfo();
JSONObject hardwareObject = new JSONObject();
hardwareObject
.put("device", device)
.put("model", model)
.put("manufacturer", manufacturer)
.put("hardware", hardware)
.put("cpuInfo", new JSONObject(cpuInfo));
File dataDirectory = Environment.getDataDirectory();
StatFs dataDirectoryStat = new StatFs(dataDirectory.getPath());
long dataDirectoryBlockSize = dataDirectoryStat.getBlockSizeLong();
long dataDirectoryAvailableBlocks = dataDirectoryStat.getAvailableBlocksLong();
long dataDirectoryTotalBlocks = dataDirectoryStat.getBlockCountLong();
long freeMemorySize = dataDirectoryAvailableBlocks * dataDirectoryBlockSize;
long totalMemorySize = dataDirectoryTotalBlocks * dataDirectoryBlockSize;
JSONObject storageObject = new JSONObject();
storageObject
.put("free", freeMemorySize)
.put("total", totalMemorySize);
MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
long totalRAMSize = memoryInfo.totalMem;
long freeRAMSize = memoryInfo.availMem;
long thresholdRAM = memoryInfo.threshold;
JSONObject ramObject = new JSONObject();
ramObject
.put("free", freeRAMSize)
.put("total", totalRAMSize)
.put("threshold", thresholdRAM);
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
JSONObject networkObject = new JSONObject();
if (netInfo != null && Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
int downSpeed = networkCapabilities.getLinkDownstreamBandwidthKbps();
int upSpeed = networkCapabilities.getLinkUpstreamBandwidthKbps();
networkObject
.put("downSpeed", downSpeed)
.put("upSpeed", upSpeed);
}
return new JSONObject()
.put("app", appObject)
.put("software", softwareObject)
.put("hardware", hardwareObject)
.put("storage", storageObject)
.put("ram", ramObject)
.put("network", networkObject)
.toString();
} catch(Exception ex) {
return jsonError("Problem fetching device info: ", ex);
}
}
public MemInfo(ActivityManager.MemoryInfo systemMemInfo,
Debug.MemoryInfo processMemInfo) {
this.systemMemInfo = systemMemInfo;
this.processMemInfo = processMemInfo;
}
public MemInfo(ActivityManager.MemoryInfo systemMemInfo,
Debug.MemoryInfo processMemInfo) {
}