1 Reply Latest reply on Jun 17, 2009 3:08 PM by 843810

    JDI - get heap and non-heap memory usage


      I'm trying to understand the process of creating a resource profiller using JDI.
      Right now I'm stuck with memory information retrieval.

      When not using JPDA there are two ways I'm aware of to retrieve such information:
      1 - Using Runtime
      2 - Using MemoryMXBean

      Most profillers provide memory analysis tools, it should be possible to get such information using JDI.

      Is there a way of retrieve such information either by getting the Runtime or MemoryMXBean of the target VM or by other means?

      Thank you in advance.
        • 1. ManagementFactory.getMemoryMXBean and RunTime give different results
          I am looking for an answer for the exact same question.

          MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
                         MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
                         miscInfoMap.put("Heap memory usage - ManagementFactory", String.format("Heap memory Used: %sMB, free: %sMB, max: %sMB ",
                         memoryUsage = memoryMXBean.getNonHeapMemoryUsage();
                         miscInfoMap.put("NonHeap memory usage", String.format("NonHeap memory Used: %sMB, free: %sMB, max: %sMB ",
                         // Heap memory usage statistics Using runTime
                         Runtime runtime = Runtime.getRuntime();
                         /* runtime.totalMemory(): Returns the total amount of memory in the Java virtual machine. The value returned by
                         * this method may vary over time, depending on the host environment. Note that the amount of memory required to
                         * hold an object of any given type may be implementation-dependent. The total amount of memory currently
                         * available for current and future objects, measured in bytes.
                         * runtime.freeMemory(): Returns the amount of free memory in the Java Virtual Machine. Calling the gc method
                         * may result in increasing the value returned by freeMemory. returns an approximation to the total amount
                         * of memory currently available for future allocated objects, measured in bytes.
                         * runtime.maxMemory(): Returns the maximum amount of memory that the Java virtual machine will attempt to use.
                         * If there is no inherent limit then the value Long.MAX_VALUE will be returned.
                         miscInfoMap.put("Heap memory usage - RunTime", String.format("Heap memory Used: %sMB, free: %sMB, max: %sMB ",