1 Reply Latest reply: Jun 17, 2009 10:08 AM by 843810 RSS

    JDI - get heap and non-heap memory usage

    843810
      Hi,

      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
      or
      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
          843810
          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.getUsed()/(1024*1024),
                                   (memoryUsage.getMax()-memoryUsage.getUsed())/(1024*1024),
                                   memoryUsage.getMax()/(1024*1024)));
                         
                         memoryUsage = memoryMXBean.getNonHeapMemoryUsage();
                         miscInfoMap.put("NonHeap memory usage", String.format("NonHeap memory Used: %sMB, free: %sMB, max: %sMB ",
                                   memoryUsage.getUsed()/(1024*1024),
                                   (memoryUsage.getMax()-memoryUsage.getUsed())/(1024*1024),
                                   memoryUsage.getMax()/(1024*1024)));
                         
                         // 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 ",
                                   runtime.totalMemory()/(1024*1024),
                                   runtime.freeMemory()/(1024*1024),
                                   runtime.maxMemory()/(1024*1024)));