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!!
Java Performance: In Java Machine Control How to find heap size , time frame and memory stats for a particular method call
329 Views Asked by User27854 AtThere are 3 best solutions below
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
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
To find heap size, you can use the
totalMemory()andmaxMemory()methods of the Runtime class.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.
You can also use the
-XX:+PrintGCDetailsJVM option to get more detailed information about garbage collection and memory usage.