Java Code Examples for android.os.Debug#getNativeHeapAllocatedSize()

The following examples show how to use android.os.Debug#getNativeHeapAllocatedSize() . 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: PerformanceHelper.java    From Trojan with Apache License 2.0 6 votes vote down vote up
public static void recordMemory() {
    Runtime runtime = Runtime.getRuntime();
    float dalvikMem = (float) ((runtime.totalMemory() - runtime.freeMemory()) * 1.0 / TrojanConstants.FORMAT_MB);
    float nativeMem = (float) (Debug.getNativeHeapAllocatedSize() * 1.0 / TrojanConstants.FORMAT_MB);

    List<String> msgList = new LinkedList<>();
    msgList.add(String.valueOf(decimalFormat.format(dalvikMem + nativeMem)) + TrojanConstants.MB);
    msgList.add(String.valueOf(decimalFormat.format(dalvikMem)) + TrojanConstants.MB);
    msgList.add(String.valueOf(decimalFormat.format(nativeMem)) + TrojanConstants.MB);
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
            Debug.getMemoryInfo(memoryInfo);
            float stackSize = Float.parseFloat(memoryInfo.getMemoryStat("summary.stack"));
            msgList.add(String.valueOf(decimalFormat.format(stackSize / TrojanConstants.FORMAT_KB)) + TrojanConstants.MB);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    Trojan.log(LogConstants.MEMORY_TAG, msgList);
}
 
Example 2
Source File: MemoryMetricsCollector.java    From Battery-Metrics with MIT License 5 votes vote down vote up
@Override
@ThreadSafe(enableChecks = false)
public synchronized boolean getSnapshot(MemoryMetrics snapshot) {
  checkNotNull(snapshot, "Null value passed to getSnapshot!");
  if (!mIsEnabled) {
    return false;
  }

  /* this helps to track the latest snapshot, diff/sum always picks latest as truth */
  snapshot.sequenceNumber = mCounter.incrementAndGet();

  snapshot.javaHeapMaxSizeKb = getRuntimeMaxMemory() / KB;
  snapshot.javaHeapAllocatedKb = (getRuntimeTotalMemory() - getRuntimeFreeMemory()) / KB;

  snapshot.nativeHeapSizeKb = Debug.getNativeHeapSize() / KB;
  snapshot.nativeHeapAllocatedKb = Debug.getNativeHeapAllocatedSize() / KB;

  snapshot.vmSizeKb = -1;
  snapshot.vmRssKb = -1;

  try {
    ProcFileReader reader = mProcFileReader.get();
    if (reader == null) {
      reader = new ProcFileReader(getPath());
      mProcFileReader.set(reader);
    }

    reader.reset();

    if (reader.isValid()) {
      snapshot.vmSizeKb = readField(reader);
      snapshot.vmRssKb = readField(reader);
    }
  } catch (ProcFileReader.ParseException pe) {
    SystemMetricsLogger.wtf(TAG, "Unable to parse memory (statm) field", pe);
  }

  return true;
}
 
Example 3
Source File: MemoryLogger.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
public static void showMemoryStats(String message) {
    Log.i("memory", message
            + "----------------------------------------------------------------------------------------");
    double nativeUsage = Debug.getNativeHeapAllocatedSize();
    Log.i("memory", "nativeUsage: " + (nativeUsage / 1048576d));
    // current heap size
    double heapSize = Runtime.getRuntime().totalMemory();
    // Log.i("memory", "heapSize: " + (heapSize / 1048576d));
    // amount available in heap
    double heapRemaining = Runtime.getRuntime().freeMemory();
    // Log.i("memory", "heapRemaining: " + (heapRemaining / 1048576d));
    double memoryAvailable = Runtime.getRuntime().maxMemory() - (heapSize - heapRemaining) - nativeUsage;
    Log.i("memory", "memoryAvailable: " + (memoryAvailable / 1048576d));

    if (first) {
        initavail = memoryAvailable;
        first = false;
    }
    if (lastavail > 0) {
        Log.i("memory", "consumed since last: " + ((lastavail - memoryAvailable) / 1048576d));
    }
    Log.i("memory", "consumed total: " + ((initavail - memoryAvailable) / 1048576d));

    lastavail = memoryAvailable;

    Log.i("memory",
            "-----------------------------------------------------------------------------------------------");
}
 
Example 4
Source File: SdlTrace.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressWarnings("deprecation")
   public static String getLogHeader(String dumpReason, int seqNo) {
	final String Sep = "-";
	StringBuilder write = new StringBuilder("<?xml version=\"1.0\"?>" + "<logs>");
	write.append("<info>");
	StringBuilder infoBlock = new StringBuilder();
	String hostInfo = Build.BRAND + Sep + Build.MANUFACTURER + Sep + Build.MODEL + "(" + Build.HOST + ")";
	infoBlock.append("<host>").append(SdlTrace.B64EncodeForXML(hostInfo)).append("</host>");
	String osv = Build.VERSION.RELEASE + " (" + Build.VERSION.CODENAME + ")";
	infoBlock.append("<osv>").append(SdlTrace.B64EncodeForXML(osv)).append("</osv>");
	infoBlock.append(TraceDeviceInfo.getTelephonyHeader());

	long heapSize = Debug.getNativeHeapFreeSize() / 1024;
	long heapAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
	infoBlock.append("<mem><hf>").append(heapSize).append("KB</hf><ha>").append(heapAllocated).append("KB</ha></mem>");
	infoBlock.append("<np>").append(Runtime.getRuntime().availableProcessors()).append("</np>");
	infoBlock.append("<pid>").append(Process.myPid()).append("</pid>");
	infoBlock.append("<tid>").append(Thread.currentThread().getId()).append("</tid>");

	// String dateStamp = (String)
	// DateFormat.format("yy-MM-dd hh:mm:ss SSS", new Timestamp(baseTics));
	Timestamp stamp = new Timestamp(SdlTrace.getBaseTics());
	String GMTtime = stamp.toGMTString().substring(0, 19);
	long fracSec = stamp.getNanos() / 1000000; // divide by a million
	String fracSecStr = String.format("%03d", fracSec);
	infoBlock.append("<utc>").append(GMTtime).append(".").append(fracSecStr).append("</utc>");

	infoBlock.append(TraceDeviceInfo.getLogHeaderBluetoothPairs());
	infoBlock.append(getSmartDeviceLinkTraceRoot(dumpReason, seqNo));

	write.append(infoBlock);

	write.append("</info>" + "<msgs>");
	return write.toString();
}
 
Example 5
Source File: Log.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
private static StringBuilder getAppInfo(Context context) {
    StringBuilder sb = new StringBuilder();

    // Get version info
    String installer = context.getPackageManager().getInstallerPackageName(BuildConfig.APPLICATION_ID);
    sb.append(String.format("%s: %s/%s %s/%s%s%s%s\r\n",
            context.getString(R.string.app_name),
            BuildConfig.APPLICATION_ID,
            installer,
            BuildConfig.VERSION_NAME,
            Helper.hasValidFingerprint(context) ? "1" : "3",
            BuildConfig.PLAY_STORE_RELEASE ? "p" : "",
            BuildConfig.DEBUG ? "d" : "",
            ActivityBilling.isPro(context) ? "+" : ""));
    sb.append(String.format("Android: %s (SDK %d)\r\n", Build.VERSION.RELEASE, Build.VERSION.SDK_INT));
    sb.append("\r\n");

    // Get device info
    sb.append(String.format("uid: %s\r\n", android.os.Process.myUid()));
    sb.append(String.format("Brand: %s\r\n", Build.BRAND));
    sb.append(String.format("Manufacturer: %s\r\n", Build.MANUFACTURER));
    sb.append(String.format("Model: %s\r\n", Build.MODEL));
    sb.append(String.format("Product: %s\r\n", Build.PRODUCT));
    sb.append(String.format("Device: %s\r\n", Build.DEVICE));
    sb.append(String.format("Host: %s\r\n", Build.HOST));
    sb.append(String.format("Display: %s\r\n", Build.DISPLAY));
    sb.append(String.format("Id: %s\r\n", Build.ID));
    sb.append("\r\n");

    sb.append(String.format("Processors: %d\r\n", Runtime.getRuntime().availableProcessors()));

    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    sb.append(String.format("Memory class: %d\r\n", am.getMemoryClass()));

    sb.append(String.format("Storage space: %s/%s\r\n",
            Helper.humanReadableByteCount(Helper.getAvailableStorageSpace(), true),
            Helper.humanReadableByteCount(Helper.getTotalStorageSpace(), true)));

    Runtime rt = Runtime.getRuntime();
    long hused = (rt.totalMemory() - rt.freeMemory()) / 1024L;
    long hmax = rt.maxMemory() / 1024L;
    long nheap = Debug.getNativeHeapAllocatedSize() / 1024L;
    sb.append(String.format("Heap usage: %s/%s KiB native: %s KiB\r\n", hused, hmax, nheap));

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    float density = context.getResources().getDisplayMetrics().density;
    sb.append(String.format("Density %f resolution: %.2f x %.2f dp %b\r\n",
            density,
            size.x / density, size.y / density,
            context.getResources().getConfiguration().isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_NORMAL)));

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    boolean ignoring = true;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        ignoring = pm.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID);
    sb.append(String.format("Battery optimizations: %b\r\n", !ignoring));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        UsageStatsManager usm = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
        int bucket = usm.getAppStandbyBucket();
        sb.append(String.format("Standby bucket: %d\r\n", bucket));
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        boolean saving = (cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED);
        sb.append(String.format("Data saving: %b\r\n", saving));
    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    boolean reporting = prefs.getBoolean("crash_reports", false);
    if (reporting) {
        String uuid = prefs.getString("uuid", null);
        sb.append(String.format("UUID: %s\r\n", uuid == null ? "-" : uuid));
    }

    sb.append("\r\n");

    sb.append(new Date(Helper.getInstallTime(context))).append("\r\n");
    sb.append(new Date()).append("\r\n");

    sb.append("\r\n");

    return sb;
}
 
Example 6
Source File: Util.java    From Eye-blink-detector with MIT License 4 votes vote down vote up
public static String getNativeMemoryStats() {
    return "(free/alloc'd/total)" + Debug.getNativeHeapFreeSize() + "/"
            + Debug.getNativeHeapAllocatedSize() + "/" + Debug.getNativeHeapSize();
}
 
Example 7
Source File: MEMUtils.java    From GTTools with MIT License 4 votes vote down vote up
public static long[] getHeapNative() {
	long[] value = new long[2];
	value[0] = Debug.getNativeHeapSize() >> 10;
	value[1] = Debug.getNativeHeapAllocatedSize() >> 10;
	return value;
}
 
Example 8
Source File: GdxDemoActivity.java    From thunderboard-android with Apache License 2.0 4 votes vote down vote up
public long getNativeHeap() {
    return Debug.getNativeHeapAllocatedSize();
}
 
Example 9
Source File: SystemUtils.java    From tilt-game-android with MIT License 4 votes vote down vote up
public static long getNativeHeapAllocatedSize() {
	return Debug.getNativeHeapAllocatedSize() / DataConstants.BYTES_PER_KILOBYTE;
}
 
Example 10
Source File: AndroidMini2DxGame.java    From mini2Dx with Apache License 2.0 4 votes vote down vote up
@Override
public long getNativeHeap() {
	return Debug.getNativeHeapAllocatedSize();
}