Java Code Examples for android.app.activitymanager#MemoryInfo

The following examples show how to use android.app.activitymanager#MemoryInfo . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MemoryUtil.java    From zone-sdk with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: SkiaPooledImageRegionDecoder.java    From PdfViewPager with Apache License 2.0 6 votes vote down vote up
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;
    }
}
 
Example 3
Source File: RNDeviceModule.java    From react-native-device-info with MIT License 6 votes vote down vote up
@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;
}
 
Example 4
Source File: CompatibleApps.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
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
}
 
Example 5
Source File: EnvironmentInfo.java    From android-perftracking with MIT License 5 votes vote down vote up
private static ActivityManager.MemoryInfo getMemoryInfo(ActivityManager activityManager) {
  ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
  if (activityManager != null) {
    activityManager.getMemoryInfo(mi);
  }
  return mi;
}
 
Example 6
Source File: CoreService.java    From MemoryCleaner with Apache License 2.0 5 votes vote down vote up
public long getAvailMemory(Context context) {
    // 获取android当前可用内存大小
    ActivityManager.MemoryInfo memoryInfo
            = new ActivityManager.MemoryInfo();
    activityManager.getMemoryInfo(memoryInfo);
    // 当前系统可用内存 ,将获得的内存大小规格化

    return memoryInfo.availMem;
}
 
Example 7
Source File: ProcessFragment.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
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();
}
 
Example 8
Source File: DeviceUtils.java    From BookReader with Apache License 2.0 5 votes vote down vote up
/**
 * 获取系统当前可用内存大小
 *
 * @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);// 将获取的内存大小规格化
}
 
Example 9
Source File: DeviceUtils.java    From Box with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example 10
Source File: MemoryUtil.java    From AndroidBase with Apache License 2.0 5 votes vote down vote up
/**
 * 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);// 将获取的内存大小规格化
}
 
Example 11
Source File: ProcessManager.java    From FileManager with Apache License 2.0 5 votes vote down vote up
private ProcessManager(Context context) {
    this.mContext = context;
    mRunningProcessList = new ArrayList<>();
    mPackageManager = mContext.getPackageManager();
    mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    mMemoryInfo = new ActivityManager.MemoryInfo();
}
 
Example 12
Source File: SkiaPooledImageRegionDecoder.java    From PictureSelector with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 13
Source File: MainActivity.java    From ForgePE with GNU Affero General Public License v3.0 5 votes vote down vote up
public long getTotalMemory() {
    ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
    activityManager.getMemoryInfo(memoryInfo);

    return memoryInfo.availMem;
}
 
Example 14
Source File: DeviceInfo.java    From EasyFeedback with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 15
Source File: MemInfo.java    From DebugOverlay-Android with Apache License 2.0 4 votes vote down vote up
public ActivityManager.MemoryInfo getSystemMemInfo() {
    return systemMemInfo;
}
 
Example 16
Source File: MemInfo.java    From DebugOverlay-Android with Apache License 2.0 4 votes vote down vote up
public MemInfo(ActivityManager.MemoryInfo systemMemInfo,
               Debug.MemoryInfo processMemInfo) {
    this.systemMemInfo = systemMemInfo;
    this.processMemInfo = processMemInfo;
}
 
Example 17
Source File: MedicAndroidJavascript.java    From medic-android with GNU Affero General Public License v3.0 4 votes vote down vote up
@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);
	}
}
 
Example 18
Source File: PerformanceUtils.java    From BlockCanaryEx with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 19
Source File: MemoryFileDescriptor.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @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);
}
 
Example 20
Source File: MemInfo.java    From DebugOverlay-Android with Apache License 2.0 2 votes vote down vote up
public MemInfo(ActivityManager.MemoryInfo systemMemInfo,
               Debug.MemoryInfo processMemInfo) {

}