Using Java Mission Control is it possible for us to find heap size, time frame and memory stats for a particular method call. In method profiling section, I am unable for figure out those!!

3

There are 3 best solutions below

0
On

I've written a program to show you how to implement that in JAVA. Please note that I am on Mac and I've run the program on Netbeans IDE 16.

Now, in the code below I used System.nanoTime() to get the taken time to run the code inside yourFunc()

For more details System.currentTimeMillis vs System.nanoTime

To get the heap size for the executed code ManagementFactory class that has a function getMemoryMXBean() which returns the managed bean for the memory system of the Java virtual machine.

Then with getHeapMemoryUsage() and getUsed() that gets the size used to allocate objects in the program.

Also By using totalMemory() & freeMemory(), you can calculate the memory used by the function as shown below.

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;

/**
 *
 * @author ghaith
 */
public class JavaPerformance {

    public static void main(String[] args) {
        
        long totalMemoryBefore = Runtime.getRuntime().totalMemory();
        long freeMemoryBefore = Runtime.getRuntime().freeMemory();
        
        long timeBeforeCall = System.nanoTime();
        yourFunc();
        long timeAfterCall = System.nanoTime();
        
        long totalMemoryAfter = Runtime.getRuntime().totalMemory();
        long freeMemoryAfter = Runtime.getRuntime().freeMemory();
        long memoryUsed = (totalMemoryAfter - freeMemoryAfter) - (totalMemoryBefore - freeMemoryBefore);
        System.out.println("Memory used: " + memoryUsed / (1024 * 1024) + "MB");
        
        long timeSpentRunningTheFunc = timeAfterCall - timeBeforeCall;
        System.out.println("Elapsed Time: " + timeSpentRunningTheFunc + "ns");
        
        MemoryMXBean mBean = ManagementFactory.getMemoryMXBean();
        System.out.println("Heap size: " + 
                mBean.getHeapMemoryUsage().getUsed() / (1024 * 1024) + "MB");
    }
    
    public static void yourFunc() {
        for (int i = 0 ; i < 100000 ; i++) {
            System.out.println("Inside yourFunc with index " + i);
        }
    }
}

Output:

Inside yourFunc with index 0

...

Inside yourFunc with index 99999

Memory used: 34MB

Elapsed Time: 2603351891ns

Heap size: 37MB

0
On

To find heap size, you can use the totalMemory() and maxMemory() methods of the Runtime class.

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

To measure the time taken by a particular method call, you can use the System.nanoTime() method to get the current time before and after the method call and calculate the difference.

long startTime = System.nanoTime();
// method call here
long endTime = System.nanoTime();
long duration = (endTime - startTime);

You can also use the -XX:+PrintGCDetails JVM option to get more detailed information about garbage collection and memory usage.

0
On

in Memory section | Allocation Tab, you can find out which where specified objects are allocated memory

in Event section | Graph , and Thread section | Latencies there is some information about function call time

you can read this article and watch these slides , there are simple notes about JMC