i m writing a java code and i want run some performance tests . I want to get the heap memory used by only one of the methods in my class.
public AccessControl {
public boolean Allowed () {
code
}
public void print () {
code }
}
i want to get the heap memory used in java everytime the method Allowed is called at runtime. i read i can do it through HPROf but i noticed that HPROf doesnt provide memory calculations for methods but only for classes is there a code i can write inside the method to get the available memory and then the used memory? thanks
There is no such thing as "heap memory used by a method". Methods don't take up heap memory, objects do.
If you're interested in the objects created within a specific method (and, of course, the methods it calls directly and indirectly), you could compare heap snapshots created before and after the method call (doable by running in a debugger and setting breakpoints).
But what actual problem are you trying to solve? Memory leaks are usually diagnosed by first finding the GC roots for apparently-unnecessary objects and the using a debugger to find out where and why these references are set.
InMemProfiler can be used to profile memory allocations. In trace mode this tool can be used to identify the source of memory allocations.
You could use the option #tracetarget-org.yourcode.YourClass to get the "Allocating Classes" output to show the per method memory allocation within YourClass.
However, you will only be able to profile memory allocations for each allocated class separately. There is currently no summary given across all the allocated classes. If you were really keen you could git fork InMemProfiler and try and add this functionality.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With