到目前为止如何在没有探查器的情况下在JVM中分配总内存?

时间:2018-01-02 13:10:36

标签: java memory profiling

我希望使用java.lang.management API在Java程序中的某个点上分配总内存。到目前为止,这就是我所拥有的:

ManagementFactory.getMemoryPoolMXBeans()
        .stream()
        .map(MemoryPoolMXBean::getPeakUsage)
        .filter(usage -> usage != null)
        .mapToLong(MemoryUsage::getUsed)
        .sum();

我想知道这段代码是否符合我的要求,或者结果会产生误导和/或错误。

3 个答案:

答案 0 :(得分:0)

您将获得有关此代码当前内存使用情况的准确结果。

以下是供您参考的示例代码:

 

    long heapSize = Runtime.getRuntime().totalMemory();
    long max = Runtime.getRuntime().maxMemory();

    StringBuilder message = new StringBuilder();

    message.append("Heap Size = ").append(formatSize(heapSize)).append("\n");
    message.append("Max Heap Size = ").append(formatSize(max)).append("\n");

    for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
      String name = pool.getName();
      MemoryType type = pool.getType();
      MemoryUsage usage = pool.getUsage();
      MemoryUsage peak = pool.getPeakUsage();
      message.append("Heap named '").append(name);
      message.append("' (").append(type.toString()).append(") ");
      message.append("uses ").append(usage.getUsed());
      message.append(" of ").append(usage.getMax());
      message.append(". The max memory used so far is ");
      message.append(peak.getUsed()).append(".\n");
    }
    System.out.println(message.toString());

希望这会对你有所帮助。

答案 1 :(得分:0)

为什么不使用" Runtime"获取分配的内存,例如请查看下面的代码? 有没有具体的理由使用java.lang.management?

    Runtime rt = Runtime.getRuntime();
    long totalMemory = rt.totalMemory();
    long freeMemory = rt.freeMemory();
    long usedMemory = totalMemory - freeMemory;

编辑:下面的代码为您提供了几个与内存相关的统计数据

public static void main(String[] args)
 {
   Iterator beans = ManagementFactory.getMemoryPoolMXBeans().iterator();
   while (beans.hasNext())
   {
  MemoryPoolMXBean bean = (MemoryPoolMXBean) beans.next();
  System.out.println("Bean: " + bean);
  System.out.println("Name: " + bean.getName());
  System.out.println("Collection usage: " + bean.getCollectionUsage());
  boolean collectionUsage = bean.isCollectionUsageThresholdSupported();
  System.out.println("Collection usage threshold supported: "
                     + collectionUsage);
  if (collectionUsage)
    {
      System.out.println("Collection usage threshold: "
                         + bean.getCollectionUsageThreshold());
      System.out.println("Setting collection usage threshold to 1MB ("
                         + MB + " bytes)");
      bean.setCollectionUsageThreshold(MB);
      System.out.println("Collection usage threshold: "
                         + bean.getCollectionUsageThreshold());
      System.out.println("Collection usage threshold count: "
                         + bean.getCollectionUsageThresholdCount());
      System.out.println("Collection usage threshold exceeded: "
                         + (bean.isCollectionUsageThresholdExceeded()
                            ? "yes" : "no"));
    }
     System.out.println("Memory manager names: "
                     + Arrays.toString(bean.getMemoryManagerNames()));
  System.out.println("Peak usage: " + bean.getPeakUsage());
  System.out.println("Current usage: " + bean.getUsage());
  System.out.println("Resetting peak usage...");
  bean.resetPeakUsage();
  System.out.println("Peak usage: " + bean.getPeakUsage());
  System.out.println("Current usage: " + bean.getUsage());
  boolean usage = bean.isUsageThresholdSupported();
  System.out.println("Usage threshold supported: " + usage);
  if (usage)
    {
      System.out.println("Usage threshold: "
                         + bean.getUsageThreshold());
      System.out.println("Setting usage threshold to 1MB ("
                         + MB + " bytes)");
      bean.setUsageThreshold(MB);
      System.out.println("Usage threshold: "
                         + bean.getUsageThreshold());
      System.out.println("Usage threshold count: "
                         + bean.getUsageThresholdCount());
      System.out.println("Usage threshold exceeded: "
                         + (bean.isUsageThresholdExceeded()
                            ? "yes" : "no"));
    }
  System.out.println("Valid: " + (bean.isValid() ? "yes" : "no"));
  }
  }

答案 2 :(得分:0)

long allocatedMemory = Runtime.getRuntime().maxMemory() - Runtime.getRuntime().freeMemory();

有关详细信息,请参阅链接: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html