Commit 8eccde29 authored by Makoto Kato's avatar Makoto Kato
Browse files

Bug 1821004 - Add summary.java-heap memory report. r=geckoview-reviewers,owlish

Android's memory profiler can show current java-heap size. Android M+
can get this usages from API. So this fix adds this entry to
about:memory.

Differential Revision: https://phabricator.services.mozilla.com/D172211
parent d9c9e6a2
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Debug;
import android.os.LocaleList;
import android.os.Looper;
import android.os.PowerManager;
@@ -1595,6 +1596,26 @@ public class GeckoAppShell {
    return id == 0 ? info.nonLocalizedLabel.toString() : context.getString(id);
  }

  @WrapForJNI(calledFrom = "gecko")
  private static int getMemoryUsage(final String stateName) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
      // No API to get Java heap usages.
      return -1;
    }

    final Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
    Debug.getMemoryInfo(memInfo);
    final String usage = memInfo.getMemoryStat(stateName);
    if (usage == null) {
      return -1;
    }
    try {
      return Integer.parseInt(usage);
    } catch (final NumberFormatException e) {
      return -1;
    }
  }

  @WrapForJNI
  public static native boolean isParentProcess();

+38 −0
Original line number Diff line number Diff line
@@ -43,6 +43,11 @@
#include "mozilla/ipc/UtilityProcessManager.h"
#include "mozilla/ipc/FileDescriptorUtils.h"

#ifdef MOZ_WIDGET_ANDROID
#  include "mozilla/java/GeckoAppShellWrappers.h"
#  include "mozilla/jni/Utils.h"
#endif

#ifdef XP_WIN
#  include "mozilla/MemoryInfo.h"

@@ -1606,6 +1611,35 @@ NS_IMPL_ISUPPORTS(DMDReporter, nsIMemoryReporter)

#endif  // MOZ_DMD

#ifdef MOZ_WIDGET_ANDROID
class AndroidMemoryReporter final : public nsIMemoryReporter {
 public:
  NS_DECL_ISUPPORTS

  AndroidMemoryReporter() = default;

  NS_IMETHOD
  CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData,
                 bool aAnonymize) override {
    if (!jni::IsAvailable() || jni::GetAPIVersion() < 23) {
      return NS_OK;
    }

    int32_t heap = java::GeckoAppShell::GetMemoryUsage("summary.java-heap"_ns);
    if (heap > 0) {
      MOZ_COLLECT_REPORT("java-heap", KIND_OTHER, UNITS_BYTES, heap * 1024,
                         "The private Java Heap usage");
    }
    return NS_OK;
  }

 private:
  ~AndroidMemoryReporter() = default;
};

NS_IMPL_ISUPPORTS(AndroidMemoryReporter, nsIMemoryReporter)
#endif

/**
 ** nsMemoryReporterManager implementation
 **/
@@ -1695,6 +1729,10 @@ nsMemoryReporterManager::Init() {
  RegisterStrongReporter(new WindowsAddressSpaceReporter());
#endif

#ifdef MOZ_WIDGET_ANDROID
  RegisterStrongReporter(new AndroidMemoryReporter());
#endif

#ifdef XP_UNIX
  nsMemoryInfoDumper::Initialize();
#endif