How can we know the memory required to execute a method
I want to know how much memory
is allocated on execution method
..
Is there pre-defined
method
to compute dedicated memory
or any process to compute ...
Note: method
can contain any number of lines ...
Example: To calculate the time taken to execute method
, calculated by calling System.currentTimeMillis();
before and after calling a specific method
. So is there any way to compute memory
required
Thank...
source to share
You can use a profiler.
They usually offer information about the heap (and many other interesting things), so you can compare allocations before and after executing the method, which is also the standard approach to finding memory leaks besides static analysis.
Additional tools give you more detailed information and analysis options.
First, take a look at visualvm, which comes free with all of Oracle's current JDKs.
source to share
You can use Runtime.totalMemory()
that (per Javadoc) returns the total memory in the Java VM.
System.out.println(Runtime.getRuntime().totalMemory());
source to share
Your method will not be sized in java, your objects.
I suggest using the sizeofag java agent . Before calling your method on an object, measure the size of the object, then, after calling the method, measure the size of the object again. See example below.
When calling your program, add the following line to your java call.
-javaagent:sizeofag.jar
While your program is running, you can use SizeOfAgent as shown below.
YourObject b = new YourObject();
System.out.println(b + ": " + SizeOfAgent.sizeOf(b));
b.method();
System.out.println(b + ": " + SizeOfAgent.sizeOf(b));
source to share